EpetraExt Development
EpetraExt_PackTraits.h
Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 //
00004 //     EpetraExt: Epetra Extended - Linear Algebra Services Package
00005 //                 Copyright (2001) Sandia Corporation
00006 //
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 //
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00025 //
00026 // ***********************************************************************
00027 // @HEADER
00028 #ifndef EPETRAEXT_PACKTRAITS_H
00029 #define EPETRAEXT_PACKTRAITS_H
00030 
00031 // ---------- Standard Includes ----------
00032 
00033 #include <string>
00034 #include <vector>
00035 
00036 // ----------   Xyce Includes   ----------
00037 
00038 // ----------  Other Includes   ----------
00039 
00040 // ----------  Fwd Declarations ----------
00041 
00042 namespace EpetraExt {
00043 
00048 template <typename T>
00049 struct PackTraits
00050 {
00057   static size_t size( T const & object )
00058   { return object.packedByteCount(); }
00059 
00067   static void pack( T const & object, char * buf, size_t size, int & pos )
00068   { object.pack( buf, size, pos ); }
00069 
00077   static void unpack( T & object, char * buf, size_t size, int & pos )
00078   { object.unpack( buf, size, pos ); }
00079 };
00080 
00082 
00084 template <>
00085 struct PackTraits<std::string>
00086 {
00087   static size_t size( std::string const & object )
00088   { return object.length() + sizeof(size_t); }
00089 
00090   static void pack( std::string const & object, char * buf, size_t size, int & pos )
00091   {
00092     size_t len = object.length();
00093     std::memcpy( buf+pos, &len, sizeof(size_t) );
00094     pos += sizeof(size_t);
00095     std::memcpy( buf+pos, object.c_str(), len );
00096     pos += len;
00097   }
00098 
00099   static void unpack( std::string & object, char * buf, size_t size, int & pos )
00100   {
00101     size_t len;
00102     std::memcpy( &len, buf+pos, sizeof(size_t) );
00103     pos += sizeof(size_t);
00104     object = std::string( buf+pos, len );
00105     pos += len;
00106   }
00107 };
00108 
00110 
00113 template <typename T>
00114 struct PackTraits< std::vector<T> >
00115 {
00116   static size_t size( std::vector<T> const & object )
00117   { return object.size() * sizeof(T) + sizeof(size_t); }
00118 
00119   static void pack( std::vector<T> const & object, char * buf, size_t size, int & os )
00120   {
00121     size_t len = object.size();
00122     std::memcpy( buf+pos, &len, sizeof(size_t) );
00123     pos += sizeof(size_t);
00124     std::memcpy( buf+pos, &object[0], len*sizeof(T) );
00125     pos += len*sizeof(T);
00126   }
00127 
00128   static void unpack( std::vector<T> & object, char * buf, int size, int & pos )
00129   {
00130     size_t len;
00131     std::memcpy( &len, buf+pos, sizeof(size_t) );
00132     pos += sizeof(size_t);
00133     object.resize(len);
00134     std::memcpy( &object[0], buf+pos, len*sizeof(T) );
00135     pos += len*sizeof(T);
00136   }
00137 };
00138 
00139 } //namespace EpetraExt
00140 
00141 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines