MOOSE - Multiscale Object Oriented Simulation Environment
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
PrepackedBuffer.cpp
Go to the documentation of this file.
1 /**********************************************************************
2 ** This program is part of 'MOOSE', the
3 ** Messaging Object Oriented Simulation Environment.
4 ** Copyright (C) 2003-2010 Upinder S. Bhalla. and NCBS
5 ** It is made available under the terms of the
6 ** GNU Lesser General Public License version 2.1
7 ** See the file COPYING.LIB for the full notice.
8 **********************************************************************/
9 
10 #include <cstring>
11 #include "PrepackedBuffer.h"
12 
14  const char* data, unsigned int dataSize, unsigned int numEntries )
15  : dataSize_( dataSize ), numEntries_( numEntries )
16 {
17  if ( numEntries == 0 )
19  else
21 
22  data_ = new char[ dataSize ];
23  memcpy( data_, data, dataSize );
24 }
25 
27  : dataSize_( other.dataSize_ ),
28  numEntries_( other.numEntries_ )
29 {
30  if ( numEntries_ == 0 )
32  else
34  data_ = new char[ dataSize_ ];
35  memcpy( data_, other.data_, dataSize_ );
36 }
37 
39  : dataSize_( *reinterpret_cast< const unsigned int * >( buf ) ),
40  numEntries_( *reinterpret_cast< const unsigned int * >(
41  buf + sizeof( unsigned int ) ) )
42 {
43  if ( numEntries_ == 0 )
45  else
47  data_ = new char[ dataSize_ ];
48  memcpy( data_, buf + 2 * sizeof( unsigned int ), dataSize_ );
49 }
50 
51 PrepackedBuffer::PrepackedBuffer() // Used to make StrSet happy
52  : dataSize_( 0 ), numEntries_( 0 ), individualDataSize_( 0 )
53 {
54  data_ = new char[1];
55  data_[0] = '\0';
56 }
57 
59 {
60  delete[] data_;
61 }
62 
63 const char* PrepackedBuffer::operator[]( unsigned int index ) const
64 {
65  if ( numEntries_ == 0 )
66  return data_ ;
67  return data_ + ( index % numEntries_ ) * individualDataSize_;
68 }
69 
70 unsigned int PrepackedBuffer::conv2buf( char* buf ) const
71 {
72  *reinterpret_cast< unsigned int* >( buf ) = dataSize_;
73  buf += sizeof( unsigned int );
74  *reinterpret_cast< unsigned int* >( buf ) = numEntries_;
75  buf += sizeof( unsigned int );
76  memcpy( buf, data_, dataSize_ );
77  return size();
78 }
unsigned int numEntries_
unsigned int size() const
const int numEntries
Definition: proc.cpp:60
const char * operator[](unsigned int index) const
unsigned int dataSize_
unsigned int individualDataSize_
unsigned int conv2buf(char *buf) const
unsigned int dataSize() const