diff --git a/synfig-core/src/synfig/value.cpp b/synfig-core/src/synfig/value.cpp index f6b57f4..98acf8f 100644 --- a/synfig-core/src/synfig/value.cpp +++ b/synfig-core/src/synfig/value.cpp @@ -83,11 +83,48 @@ ValueBase::ValueBase(Type &x): create(x); } +ValueBase::ValueBase(const ValueBase& x) + : ValueBase(*x.type) +{ + if(data != x.data) + { + Operation::CopyFunc copy_func = + Type::get_operation( + Operation::Description::get_copy(type->identifier, type->identifier) ); + if (copy_func) + { + copy_func(data, x.data); + } + else + { + data = x.data; + ref_count = x.ref_count; + } + } + + loop_ = x.loop_; + static_ = x.static_; + interpolation_ = x.interpolation_; +} + +ValueBase::ValueBase(ValueBase&& x) + : ValueBase() +{ + swap(*this, x); +} + ValueBase::~ValueBase() { clear(); } +ValueBase& +ValueBase::operator=(ValueBase x) +{ + swap(*this, x); + return *this; +} + #ifdef _DEBUG String ValueBase::get_string() const @@ -169,35 +206,6 @@ ValueBase::get_contained_type()const return get_list().front().get_type(); } -ValueBase& -ValueBase::operator=(const ValueBase& x) -{ - if(data!=x.data) - { - Type ¤t_type = *type; - Type &new_type = *x.type; - Operation::CopyFunc func = - Type::get_operation( - Operation::Description::get_copy(current_type.identifier, new_type.identifier) ); - if (func) - { - create(current_type); - func(data, x.data); - } - else - { - clear(); - type=x.type; - data=x.data; - ref_count=x.ref_count; - } - } - loop_=x.loop_; - static_=x.static_; - interpolation_=x.interpolation_; - return *this; -} - void ValueBase::clear() { @@ -214,7 +222,6 @@ ValueBase::clear() type=&type_nil; } - Type& ValueBase::ident_type(const String &str) { diff --git a/synfig-core/src/synfig/value.h b/synfig-core/src/synfig/value.h index 3d88da9..a7b88a8 100644 --- a/synfig-core/src/synfig/value.h +++ b/synfig-core/src/synfig/value.h @@ -123,11 +123,17 @@ public: set_list_of(x); } - //! Copy constructor. The data is not copied, just the type. + //! Constructor with the type set. ValueBase(Type &x); + //! Copy constructor + ValueBase(const ValueBase &x); + + //! Move constructor + ValueBase(ValueBase&& x); + //! Default destructor - ~ValueBase(); + virtual ~ValueBase(); /* -- ** -- O P E R A T O R S --------------------------------------------------- @@ -140,8 +146,8 @@ public: template ValueBase& operator=(const T& x) { set(x); return *this; } - //!Operator asignation for ValueBase classes. Does a exact copy of \x - ValueBase& operator=(const ValueBase& x); + //!Copy/Move assignment operator for ValueBase classes + ValueBase& operator=(ValueBase x); //! Equal than operator. Segment, Gradient and Bline Points cannot be compared. bool operator==(const ValueBase& rhs)const; @@ -172,6 +178,16 @@ public: //! Deletes the data only if the ref count is zero void clear(); + //! Swap object contents + friend void swap(ValueBase& first, ValueBase& second) { + std::swap(first.type, second.type); + std::swap(first.data, second.data); + std::swap(first.ref_count, second.ref_count); + std::swap(first.loop_, second.loop_); + std::swap(first.static_, second.static_); + std::swap(first.interpolation_, second.interpolation_); + } + //! Gets the loop option. bool get_loop()const { return loop_; }