////////////////////////////////////////////////////////////////////////// // Name: actMove.h // Product: cv act library // Purpose: template<>'s to implement move semantics // // Copyright: (c) 2010 cv cryptovision GmbH // all rights reserved // Licence: The conditions for the use of this software are regulated // in the cv act library licence agreement. // // Autor: Markus Tesche // Date: 07/25/2010 ////////////////////////////////////////////////////////////////////////// #ifndef ACT_Move_h #define ACT_Move_h namespace act { // // type_of used to dismantle T resulting in T' // -------------------------------------------------------------------------------- template struct type_of { typedef T type; }; template struct type_of { typedef T type; }; template struct type_of { typedef T type; }; template struct type_of { typedef T type; }; template struct type_of { typedef T type; }; template struct type_of { typedef T type; }; // // is_const // -------------------------------------------------------------------------------- template struct is_const { enum { value = 0 }; }; template struct is_const { enum { value = 1 }; }; template struct is_const { enum { value = 0 }; }; template struct is_const { enum { value = 1 }; }; template struct is_const { enum { value = 0 }; }; template struct is_const { enum { value = 1 }; }; // // /brief move_from<...> used to implement move semantics // -------------------------------------------------------------------------------- template class move_from; // // /brief move_from default spcialization, holds reference to type T // -------------------------------------------------------------------------------- template class move_from { public: typedef typename type_of::type type_t; explicit move_from(type_t& ref) : m_ref(ref) { } template move_from(const move_from& other) : m_ref(other.source()) { } inline type_t& source() const { return m_ref; } template O& source() const { return m_ref; } template void swap(O& other) { m_ref.swap(other); } template O& move(O& other) { m_ref.swap(other); return other; } private: type_t& m_ref; private: template friend class move_from; }; // // /brief move_from spcialization takes ownership of 'swapped-in' source data // -------------------------------------------------------------------------------- template class move_from : public move_from { public: typedef typename type_of::type type_t; explicit move_from(type_t& ref) : move_from(m_value) { ref.swap(m_value); } template move_from(move_from other) : move_from(m_value) { other.swap(m_value); } private: typename move_from::type_t m_value; }; // // /brief move_empty used as default move_from<...> function parameter // -------------------------------------------------------------------------------- template class move_empty : public move_from { public: move_empty() : move_from(m_empty) { } move_empty(const move_empty&) : move_from(m_empty) { } inline move_empty& operator=(const move_empty&) { return *this; } private: T m_empty; }; // // /brief move<> helper function // -------------------------------------------------------------------------------- template move_from move(T& ref) { return move_from(ref); } } // namespace act #endif // ACT_Move_h