#ifndef __MARSHALSUPPORT_MARSHALINOUT_H__ #define __MARSHALSUPPORT_MARSHALINOUT_H__ #include "MarshalPreprocessorMacros.h" #include "MarshalSource.h" #include "MarshalTarget.h" #include "IsLastNestedContainerElement.h" #include "IsThisOrNestedElementLastNestedContainerElement.h" #ifdef MARSHALSUPPORT_CPP11SUPPORTED #include #include "IsAssignable.h" #endif namespace MarshalSupport { namespace Marshal { //---------------------------------------------------------------------------------------------------------------------- template::value, bool IsThisOrNextElementLastElement = MarshalSupport::Internal::is_this_or_nested_element_last_nested_container_element::value> class InOut { public: // Friend declarations template friend class In; template friend class Out; public: // Constructors inline InOut(ContainerType& target) :_sourceReference(_source), _targetReference(_target), _source(target), _target(target) { #ifdef MARSHALSUPPORT_CPP11SUPPORTED static_assert(!MarshalSupport::Internal::is_only_movable::value, "Target of Marshal::InOut can't be move-only"); static_assert(MarshalSupport::Internal::is_assignable::value, "Target of Marshal::InOut must be assignable"); #endif } // Marshal methods inline ContainerType Get() const { return _sourceReference.MarshalToWithoutMove(); } inline void Get(ContainerType& targetObject) const { // Note that we use a reference to the marshaller object here. This is done to ensure that the compiler can't // optimize away any virtual function calls we're using to enforce code boundaries between assemblies. If the // compiler could be sure of the actual type of the target marshaller object, it could attempt to do this. Since // this class is designed to be passed by reference across assembly boundaries, the compiler can't assume // anything about the existing contents of the object when it is passed as an argument to a method in the target // assembly, and therefore cannot optimize away our virtual function boundaries. _sourceReference.MarshalToWithoutMove(targetObject); } inline void Set(const ContainerType& sourceObject) const { // See notes on Get method _targetReference.MarshalFrom(sourceObject); } inline const InOut& operator=(const ContainerType& sourceObject) const { _targetReference.MarshalFrom(sourceObject); return *this; } #ifdef MARSHALSUPPORT_CPP11SUPPORTED inline void Set(ContainerType&& sourceObject) const { _targetReference.MarshalFrom(std::move(sourceObject)); } inline const InOut& operator=(ContainerType&& sourceObject) const { _targetReference.MarshalFrom(std::move(sourceObject)); return *this; } #endif // Implicit conversions inline operator ContainerType() const { return _sourceReference.MarshalToWithoutMove(); } protected: // This protected constructor is never actually used by anything, nor should it ever be. The only purpose of this // constructor is to introduce a path by which the _sourceReference and _targetReference members could be initialized // with a value of anything other than the addresses of the _source and _target members. Since this type could be // derived from elsewhere, and this constructor could be called by a derived type, and we explicitly set the value of // _sourceReference and _targetReference here to an externally provided value, the optimizer cannot assume what the // contents of these members could be based on the other provided constructors, making it impossible to optimize away // virtual function calls within the referenced object. inline InOut(bool, ContainerType& target, const ContainerType& source) :_sourceReference(source), _targetReference(target), _source(source), _target(target) { } private: // Disable copying and moving InOut(const InOut& sourceObject) MARSHALSUPPORT_DELETEMETHOD; InOut& operator=(const InOut& source) MARSHALSUPPORT_DELETEMETHOD; #ifdef MARSHALSUPPORT_CPP11SUPPORTED InOut(InOut&& sourceObject) MARSHALSUPPORT_DELETEMETHOD; InOut& operator=(InOut&& source) MARSHALSUPPORT_DELETEMETHOD; #endif private: // Note that these data members MUST be listed in this order. The layout and size of the MarshalSource and // MarshalTarget classes is permitted to vary on either side of an assembly boundary, so we need to ensure our // reference to the interface for this object can be reliably retrieved, even if the size of the target object is // different to what is expected. const IMarshalSource& _sourceReference; const IMarshalTarget& _targetReference; MarshalSource _source; MarshalTarget _target; }; //---------------------------------------------------------------------------------------------------------------------- template class InOut, false, true> { public: // Friend declarations template friend class In; template friend class Out; public: // Constructors inline InOut(std::vector& target) :_sourceReference(_source), _targetReference(_target), _source(target), _target(target) { } // Marshal methods inline std::vector Get() const { return _sourceReference.MarshalToWithoutMove(); } inline void Get(std::vector& targetObject) const { // Note that we use a reference to the marshaller object here. This is done to ensure that the compiler can't // optimize away any virtual function calls we're using to enforce code boundaries between assemblies. If the // compiler could be sure of the actual type of the target marshaller object, it could attempt to do this. Since // this class is designed to be passed by reference across assembly boundaries, the compiler can't assume // anything about the existing contents of the object when it is passed as an argument to a method in the target // assembly, and therefore cannot optimize away our virtual function boundaries. _sourceReference.MarshalToWithoutMove(targetObject); } inline void Set(const std::vector& sourceObject) const { // See notes on Get method _targetReference.MarshalFrom(sourceObject); } inline const InOut& operator=(const std::vector& sourceObject) const { _targetReference.MarshalFrom(sourceObject); return *this; } #ifdef MARSHALSUPPORT_CPP11SUPPORTED inline void Set(std::vector&& sourceObject) const { _targetReference.MarshalFrom(std::move(sourceObject)); } inline const InOut& operator=(std::vector&& sourceObject) const { _targetReference.MarshalFrom(std::move(sourceObject)); return *this; } #endif // Capacity methods inline typename std::vector::size_type size() const { return _sourceReference.size(); } inline void resize(typename std::vector::size_type newSize, const ElementType& insertElement = ElementType()) const { _targetReference.resize(newSize, insertElement); } inline typename std::vector::size_type capacity() const { return _sourceReference.capacity(); } inline bool empty() const { return _sourceReference.empty(); } inline void reserve(typename std::vector::size_type newSize) const { _targetReference.reserve(newSize); } // Element access methods inline void GetElement(typename std::vector::size_type index, ElementType& element) const { _sourceReference.GetElement(index, element); } inline void SetElement(typename std::vector::size_type index, const ElementType& element) const { _targetReference.SetElement(index, element); } #ifdef MARSHALSUPPORT_CPP11SUPPORTED inline void SetElement(typename std::vector::size_type index, ElementType&& element) const { _targetReference.SetElement(index, std::move(element)); } #endif // Modifier methods inline void push_back(const ElementType& element) const { _targetReference.push_back(element); } #ifdef MARSHALSUPPORT_CPP11SUPPORTED inline void push_back(ElementType&& element) const { _targetReference.push_back(std::move(element)); } #endif inline void pop_back() const { _targetReference.pop_back(); } inline void clear() const { _targetReference.clear(); } inline void assign(typename std::vector::size_type newSize, const ElementType& element) const { _targetReference.assign(newSize, element); } inline void AssignFrom(const ElementType* data, size_t count) const { _targetReference.AssignFrom(data, count); } #ifdef MARSHALSUPPORT_CPP11SUPPORTED inline void AssignFromWithMove(ElementType* data, size_t count) const { _targetReference.AssignFromWithMove(data, count); } #endif // Implicit conversions inline operator std::vector() const { return _sourceReference.MarshalToWithoutMove(); } protected: // This protected constructor is never actually used by anything, nor should it ever be. The only purpose of this // constructor is to introduce a path by which the _sourceReference and _targetReference members could be initialized // with a value of anything other than the addresses of the _source and _target members. Since this type could be // derived from elsewhere, and this constructor could be called by a derived type, and we explicitly set the value of // _sourceReference and _targetReference here to an externally provided value, the optimizer cannot assume what the // contents of these members could be based on the other provided constructors, making it impossible to optimize away // virtual function calls within the referenced object. inline InOut(bool, std::vector& target, const std::vector& source) :_sourceReference(source), _targetReference(target), _source(source), _target(target) { } private: // Disable copying and moving InOut(const InOut& sourceObject) MARSHALSUPPORT_DELETEMETHOD; InOut& operator=(const InOut& source) MARSHALSUPPORT_DELETEMETHOD; #ifdef MARSHALSUPPORT_CPP11SUPPORTED InOut(InOut&& sourceObject) MARSHALSUPPORT_DELETEMETHOD; InOut& operator=(InOut&& source) MARSHALSUPPORT_DELETEMETHOD; #endif private: // Note that these data members MUST be listed in this order. The layout and size of the MarshalSource and // MarshalTarget classes is permitted to vary on either side of an assembly boundary, so we need to ensure our // reference to the interface for this object can be reliably retrieved, even if the size of the target object is // different to what is expected. const IMarshalSource>& _sourceReference; const IMarshalTarget>& _targetReference; MarshalSource> _source; MarshalTarget> _target; }; #ifdef MARSHALSUPPORT_CPP11SUPPORTED //---------------------------------------------------------------------------------------------------------------------- template class InOut, false, true> { public: // Friend declarations template friend class In; template friend class Out; public: // Constructors inline InOut(std::array& target) :_sourceReference(_source), _targetReference(_target), _source(target), _target(target) { } // Marshal methods inline std::array Get() const { return _sourceReference.MarshalToWithoutMove(); } inline void Get(std::array& targetObject) const { // Note that we use a reference to the marshaller object here. This is done to ensure that the compiler can't // optimize away any virtual function calls we're using to enforce code boundaries between assemblies. If the // compiler could be sure of the actual type of the target marshaller object, it could attempt to do this. Since // this class is designed to be passed by reference across assembly boundaries, the compiler can't assume // anything about the existing contents of the object when it is passed as an argument to a method in the target // assembly, and therefore cannot optimize away our virtual function boundaries. _sourceReference.MarshalToWithoutMove(targetObject); } inline void Set(const std::array& sourceObject) const { // See notes on Get method _targetReference.MarshalFrom(sourceObject); } inline const InOut& operator=(const std::array& sourceObject) const { _targetReference.MarshalFrom(sourceObject); return *this; } inline void Set(std::array&& sourceObject) const { _targetReference.MarshalFrom(std::move(sourceObject)); } inline const InOut& operator=(std::array&& sourceObject) const { _targetReference.MarshalFrom(std::move(sourceObject)); return *this; } // Capacity methods inline typename std::array::size_type size() const { return _sourceReference.size(); } inline bool empty() const { return _sourceReference.empty(); } // Element access methods inline void GetElement(typename std::array::size_type index, ElementType& element) const { _sourceReference.GetElement(index, element); } inline void SetElement(typename std::array::size_type index, const ElementType& element) const { _targetReference.SetElement(index, element); } inline void SetElement(typename std::array::size_type index, ElementType&& element) const { _targetReference.SetElement(index, std::move(element)); } // Modifier methods inline void AssignFrom(const ElementType* data) const { _targetReference.AssignFrom(data); } inline void AssignFromWithMove(ElementType* data) const { _targetReference.AssignFromWithMove(data); } // Implicit conversions inline operator std::array() const { return _sourceReference.MarshalToWithoutMove(); } protected: // This protected constructor is never actually used by anything, nor should it ever be. The only purpose of this // constructor is to introduce a path by which the _sourceReference and _targetReference members could be initialized // with a value of anything other than the addresses of the _source and _target members. Since this type could be // derived from elsewhere, and this constructor could be called by a derived type, and we explicitly set the value of // _sourceReference and _targetReference here to an externally provided value, the optimizer cannot assume what the // contents of these members could be based on the other provided constructors, making it impossible to optimize away // virtual function calls within the referenced object. inline InOut(bool, std::array& target, const std::array& source) :_sourceReference(source), _targetReference(target), _source(source), _target(target) { } private: // Disable copying and moving InOut(const InOut& sourceObject) MARSHALSUPPORT_DELETEMETHOD; InOut& operator=(const InOut& source) MARSHALSUPPORT_DELETEMETHOD; InOut(InOut&& sourceObject) MARSHALSUPPORT_DELETEMETHOD; InOut& operator=(InOut&& source) MARSHALSUPPORT_DELETEMETHOD; private: // Note that these data members MUST be listed in this order. The layout and size of the MarshalSource and // MarshalTarget classes is permitted to vary on either side of an assembly boundary, so we need to ensure our // reference to the interface for this object can be reliably retrieved, even if the size of the target object is // different to what is expected. const IMarshalSource>& _sourceReference; const IMarshalTarget>& _targetReference; MarshalSource> _source; MarshalTarget> _target; }; #endif } // Close namespace Marshal } // Close namespace MarshalSupport #include "MarshalInOutStringOperators.inl" #endif