Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 9 additions & 17 deletions include/c2pa.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include <vector>
#include <optional>
#include <memory>
#include <utility>

#include "c2pa.h"

Expand Down Expand Up @@ -638,21 +639,17 @@ namespace c2pa
Reader& operator=(const Reader&) = delete;

Reader(Reader&& other) noexcept
: c2pa_reader(other.c2pa_reader),
: c2pa_reader(std::exchange(other.c2pa_reader, nullptr)),
owned_stream(std::move(other.owned_stream)),
cpp_stream(std::move(other.cpp_stream)) {
other.c2pa_reader = nullptr;
}

Reader& operator=(Reader&& other) noexcept {
if (this != &other) {
if (c2pa_reader != nullptr) {
c2pa_free(c2pa_reader);
}
c2pa_reader = other.c2pa_reader;
c2pa_free(c2pa_reader);
c2pa_reader = std::exchange(other.c2pa_reader, nullptr);
owned_stream = std::move(other.owned_stream);
cpp_stream = std::move(other.cpp_stream);
other.c2pa_reader = nullptr;
}
return *this;
}
Expand Down Expand Up @@ -754,15 +751,13 @@ namespace c2pa

/// @brief Move constructor.
/// @param other Signer to move from.
Signer(Signer&& other) noexcept : signer(other.signer) {
other.signer = nullptr;
Signer(Signer&& other) noexcept : signer(std::exchange(other.signer, nullptr)) {
}

Signer& operator=(Signer&& other) noexcept {
if (this != &other) {
c2pa_free(signer);
signer = other.signer;
other.signer = nullptr;
signer = std::exchange(other.signer, nullptr);
}
return *this;
}
Expand Down Expand Up @@ -810,16 +805,13 @@ namespace c2pa

Builder& operator=(const Builder&) = delete;

Builder(Builder&& other) noexcept : builder(other.builder) {
other.builder = nullptr;
Builder(Builder&& other) noexcept : builder(std::exchange(other.builder, nullptr)) {
}

Builder& operator=(Builder&& other) noexcept {
if (this != &other) {
if (builder != nullptr)
c2pa_free(builder);
builder = other.builder;
other.builder = nullptr;
c2pa_free(builder);
builder = std::exchange(other.builder, nullptr);
}
return *this;
}
Expand Down
53 changes: 16 additions & 37 deletions src/c2pa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,13 @@ inline std::vector<unsigned char> to_byte_vector(const unsigned char* data, int6
/// This class is used to throw exceptions for errors encountered by the C2PA library via c2pa_error().

C2paException::C2paException()
: message_([]{
auto result = c2pa_error();
std::string msg = result ? std::string(result) : std::string();
c2pa_free(result);
return msg;
}())
{
auto result = c2pa_error();
message_ = result ? std::string(result) : std::string();
c2pa_free(result);
}

C2paException::C2paException(std::string message) : message_(std::move(message))
Expand Down Expand Up @@ -378,44 +381,24 @@ inline std::vector<unsigned char> to_byte_vector(const unsigned char* data, int6
}
}

Context::Context(const Settings& settings) : context(nullptr) {
if (!settings.is_valid()) {
throw C2paException("Settings object is invalid");
}
auto builder = c2pa_context_builder_new();
if (!builder) {
throw C2paException("Failed to create Context builder");
}
if (c2pa_context_builder_set_settings(builder, settings.c_settings()) != 0) {
c2pa_free(builder);
throw C2paException();
}
// The C API consumes the builder on build
context = c2pa_context_builder_build(builder);
if (!context) {
throw C2paException("Failed to build context");
}
Context::Context(const Settings& settings)
: Context(ContextBuilder().with_settings(settings).create_context()) {
}

Context::Context(Context&& other) noexcept : context(other.context) {
other.context = nullptr;
Context::Context(Context&& other) noexcept
: context(std::exchange(other.context, nullptr)) {
}

Context& Context::operator=(Context&& other) noexcept {
if (this != &other) {
if (context) {
c2pa_free(context);
}
context = other.context;
other.context = nullptr;
c2pa_free(context);
context = std::exchange(other.context, nullptr);
}
return *this;
}

Context::~Context() noexcept {
if (context) {
c2pa_free(context);
}
c2pa_free(context);
}

C2paContext* Context::c_context() const noexcept {
Expand Down Expand Up @@ -443,18 +426,14 @@ inline std::vector<unsigned char> to_byte_vector(const unsigned char* data, int6

Context::ContextBuilder& Context::ContextBuilder::operator=(ContextBuilder&& other) noexcept {
if (this != &other) {
if (context_builder) {
c2pa_free(context_builder);
}
c2pa_free(context_builder);
context_builder = std::exchange(other.context_builder, nullptr);
}
return *this;
}

Context::ContextBuilder::~ContextBuilder() noexcept {
if (context_builder) {
c2pa_free(context_builder);
}
c2pa_free(context_builder);
}

bool Context::ContextBuilder::is_valid() const noexcept {
Expand Down Expand Up @@ -497,7 +476,7 @@ inline std::vector<unsigned char> to_byte_vector(const unsigned char* data, int6
throw C2paException("ContextBuilder is invalid (moved from)");
}

// The C API consumes the builder on build
// The C API consumes the context builder on build
C2paContext* ctx = c2pa_context_builder_build(context_builder);
if (!ctx) {
throw C2paException("Failed to build context");
Expand Down