// @HEADER // *********************************************************************** // // Teuchos: Common Tools Package // Copyright (2004) Sandia Corporation // // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive // license for use of this work by or on behalf of the U.S. Government. // // This library is free software; you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as // published by the Free Software Foundation; either version 2.1 of the // License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, but // WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 // USA // Questions? Contact Michael A. Heroux (maherou@sandia.gov) // // *********************************************************************** // @HEADER #ifndef TEUCHOS_SERIALIZATION_TRAITS_HPP #define TEUCHOS_SERIALIZATION_TRAITS_HPP #include "Teuchos_ConfigDefs.hpp" namespace Teuchos { template struct UndefinedSerializationTraits { //! This function should not compile if there is an attempt to instantiate! static inline T notDefined() {return(T::this_type_is_missing_a_specialization());} }; /** \brief Serialization traits class for types that use value semantics. * * There are one of two modes associated with serialization. * * ToDo: Finish documenation! */ template class SerializationTraits { public: //! @name Seialization type selection //@{ /** \brief Determines if the type supports direct serialization. */ static const bool supportsDirectSerialization = false; //@} //! @name Direct serialization functions (not defined if supportsDirectSerialization==false) //@{ /** \brief Return the number of bytes for count objects. */ static Ordinal fromCountToDirectBytes(const Ordinal count) { UndefinedSerializationTraits::notDefined(); return 0; } /** \brief Convert the pointer type to char*. */ static char* convertToCharPtr( T* ptr ) { UndefinedSerializationTraits::notDefined(); return 0; } /** \brief Convert the pointer type to const char*. */ static const char* convertToCharPtr( const T* ptr ) { UndefinedSerializationTraits::notDefined(); return 0; } /** \brief Return the number of objects for bytes of storage. */ static Ordinal fromDirectBytesToCount(const Ordinal bytes) { UndefinedSerializationTraits::notDefined(); return 0; } /** \brief Convert the pointer type from char*. */ static T* convertFromCharPtr( char* ptr ) { UndefinedSerializationTraits::notDefined(); return 0; } /** \brief Convert the pointer type from char*. */ static const T* convertFromCharPtr( const char* ptr ) { UndefinedSerializationTraits::notDefined(); return 0; } //@} //! @name Indirect serialization functions (always defined and supported) //@{ /** \brief Return the number of bytes for count objects. */ static Ordinal fromCountToIndirectBytes(const Ordinal count) { UndefinedSerializationTraits::notDefined(); return 0; } /** \brief Serialize to an indirect char[] buffer. * * \param count * [in] The number of objects to serialize. * \param buffer * [in] The objects to serialize. * \param bytes * [in] Number of bytes in charBuffer[] * \param charBuffer * [out] Array (length bytes) containing the serialized objects. * * Preconditions:
    *
  • bytes==fromCountToIndirectBytes(count) *
*/ static void serialize( const Ordinal count, const T buffer[], const Ordinal bytes, char charBuffer[] ) { UndefinedSerializationTraits::notDefined(); } /** \brief Return the number of objects for bytes of storage. */ static Ordinal fromIndirectBytesToCount(const Ordinal bytes) { UndefinedSerializationTraits::notDefined(); return 0; } /** \brief Deserialize from an indirect char[] buffer. * * \param bytes * [in] Number of bytes in charBuffer[] * \param charBuffer * [in] Array (length bytes) containing the serialized objects. * \param count * [in] The number of objects to deserialize. * \param buffer * [out] The deserialized objects. * Preconditions:
    *
  • count==fromIndirectBytesToCount(bytes) *
*/ static void deserialize( const Ordinal bytes, const char charBuffer[], const Ordinal count, T buffer[] ) { UndefinedSerializationTraits::notDefined(); } //@} }; template class DirectSerializationTraits { public: static const bool supportsDirectSerialization = true; // Direct serialization static Ordinal fromCountToDirectBytes(const Ordinal count) { return sizeof(T)*count; } static char* convertToCharPtr( T* ptr ) { return reinterpret_cast(ptr); } static const char* convertToCharPtr( const T* ptr ) { return reinterpret_cast(ptr); } static Ordinal fromDirectBytesToCount(const Ordinal count) { return count/sizeof(T); } static T* convertFromCharPtr( char* ptr ) { return reinterpret_cast(ptr); } static const T* convertFromCharPtr( const char* ptr ) { return reinterpret_cast(ptr); } // Indirect serialization static Ordinal fromCountToIndirectBytes(const Ordinal count) { return fromCountToDirectBytes(count); } static void serialize( const Ordinal count, const T buffer[], const Ordinal bytes, char charBuffer[] ) { #ifdef TEUCHOS_DEBUG TEST_FOR_EXCEPT(bytes!=fromCountToIndirectBytes(count)); #endif const char *_buffer = convertToCharPtr(buffer); std::copy(_buffer,_buffer+bytes,charBuffer); } static Ordinal fromIndirectBytesToCount(const Ordinal bytes) { return fromDirectBytesToCount(bytes); } static void deserialize( const Ordinal bytes, const char charBuffer[], const Ordinal count, T buffer[] ) { #ifdef TEUCHOS_DEBUG TEST_FOR_EXCEPT(count!=fromIndirectBytesToCount(bytes)); #endif char *_buffer = convertToCharPtr(buffer); std::copy(charBuffer,charBuffer+bytes,_buffer); } }; template class SerializationTraits : public DirectSerializationTraits {}; template class SerializationTraits : public DirectSerializationTraits {}; template class SerializationTraits : public DirectSerializationTraits {}; template class SerializationTraits : public DirectSerializationTraits {}; #if defined(HAVE_COMPLEX) && defined(HAVE_TEUCHOS_COMPLEX) template class SerializationTraits > : public DirectSerializationTraits > {}; template class SerializationTraits > : public DirectSerializationTraits > {}; #endif // defined(HAVE_COMPLEX) && defined(HAVE_TEUCHOS_COMPLEX) } // namespace Teuchos #endif // TEUCHOS_SERIALIZATION_TRAITS_HPP