Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members

BCP_buffer Class Reference

#include <BCP_buffer.hpp>

List of all members.

Public Member Functions

Query methods
BCP_message_tag msgtag () const
const BCP_proc_idsender () const
int size () const
const char * data () const
Modifying methods
void set_position (const int pos) throw (BCP_fatal_error)
void set_msgtag (const BCP_message_tag tag)
void set_content (const char *data, const size_t size, BCP_proc_id *sender, BCP_message_tag msgtag)
BCP_bufferoperator= (const BCP_buffer &buf)
void make_fit (const int add_size)
void clear ()
template<class T> BCP_bufferpack (const T &value)
template<class T> BCP_bufferunpack (T &value)
template<class T> BCP_bufferpack (const T *const values, const int length)
template<class T> BCP_bufferunpack (T *&values, int &length, bool allocate=true) throw (BCP_fatal_error)
BCP_bufferpack (const BCP_string &value)
BCP_bufferpack (BCP_string &value)
BCP_bufferunpack (BCP_string &value)
template<class T> BCP_bufferpack (const BCP_vec< T > &vec)
template<class T> BCP_bufferpack (const typename std::vector< T > &vec)
template<class T> BCP_bufferunpack (BCP_vec< T > &vec)
template<class T> BCP_bufferunpack (typename std::vector< T > &vec)
Constructors and destructor
 BCP_buffer ()
 BCP_buffer (const BCP_buffer &buf)
 ~BCP_buffer ()

Public Attributes

Data members
The data members are public for efficiency reasons. Access these fields sparingly.
THINK: maybe it's not that inefficient to access the fields thru functions...

Note that:

  1. The size of the buffer never decreases. The buffer size increases when the current size is not sufficient for the message.
  2. With normal usage for incoming messages _size stays constant while reading out the message and _pos moves forward in the buffer.
  3. With normal usage for outgoing messages _size and _pos moves forward together as the buffer is filled with the message.


BCP_message_tag _msgtag
BCP_proc_id_sender
size_t _pos
size_t _max_size
size_t _size
char * _data


Detailed Description

This class describes the message buffer used for all processes of BCP. This buffer is a character array; the components of a message are simply copied into this array one after the other. Note that each process has only one buffer, which serves both for outgoing and incoming messages. This can be done since when a message arrives it is completely unpacked before anything else is done and conversely, once a message is started to be packed together it will be sent out before another message is received.

NOTE: Only the following type of objects can be packed with the various pack() member methods:

Everything else that needs to be packed at any time must have a pack() member method.

Definition at line 39 of file BCP_buffer.hpp.


Constructor & Destructor Documentation

BCP_buffer::BCP_buffer  )  [inline]
 

The default constructor creates a buffer of size 16 Kbytes with no message in it.

Definition at line 372 of file BCP_buffer.hpp.

References _data, _max_size, _msgtag, _pos, _sender, and _size.

00372                 : _msgtag(BCP_Msg_NoMessage), _sender(0), _pos(0),
00373       _max_size(0x4000/*16K*/), _size(0), _data(new char[_max_size]) {}

BCP_buffer::BCP_buffer const BCP_buffer buf  )  [inline]
 

The copy constructor makes an exact replica of the other buffer.

Definition at line 375 of file BCP_buffer.hpp.

References _data, _max_size, _msgtag, _pos, _sender, _size, and operator=().

00375                                      :
00376       _msgtag(BCP_Msg_NoMessage), _sender(0), _pos(0),
00377       _max_size(0), _size(0), _data(0){
00378          operator=(buf);
00379    }

BCP_buffer::~BCP_buffer  )  [inline]
 

The desctructor deletes all data members (including freeing the buffer).

Definition at line 382 of file BCP_buffer.hpp.

References _data, and _sender.

00382                  {
00383       delete _sender;   _sender = 0;
00384       delete[] _data;
00385    }


Member Function Documentation

void BCP_buffer::clear  )  [inline]
 

Completely clear the buffer. Delete and zero out _msgtag, _size, _pos and _sender.

Definition at line 159 of file BCP_buffer.hpp.

References _msgtag, _pos, _sender, and _size.

Referenced by BCP_cg_user::send_cut(), and BCP_vg_user::send_var().

00159                       {
00160       _msgtag = BCP_Msg_NoMessage;
00161       _size = 0;
00162       _pos = 0;
00163       delete _sender; _sender = 0;
00164    }

const char* BCP_buffer::data  )  const [inline]
 

Return a const pointer to the data stored in the buffer.

Definition at line 96 of file BCP_buffer.hpp.

References _data.

00096 { return _data; }

void BCP_buffer::make_fit const int  add_size  )  [inline]
 

Reallocate the buffer if necessary so that at least add_size number of additional bytes will fit into the buffer.

Definition at line 147 of file BCP_buffer.hpp.

References _data, _max_size, and _size.

Referenced by pack().

00147                                            {
00148       if (_max_size < _size + add_size){
00149          _max_size = 2 * (_size + add_size + 0x1000/*4K*/);
00150          char *new_data = new char[_max_size];
00151          if (_size)
00152             memcpy(new_data, _data, _size);
00153          delete[] _data;
00154          _data = new_data;
00155       }
00156    }

BCP_message_tag BCP_buffer::msgtag  )  const [inline]
 

Return the message tag of the message in the buffer.

Definition at line 89 of file BCP_buffer.hpp.

References _msgtag.

Referenced by BCP_vg_user::unpack_dual_solution(), and BCP_cg_user::unpack_primal_solution().

00089 { return _msgtag; }

BCP_buffer& BCP_buffer::operator= const BCP_buffer buf  )  [inline]
 

Make an exact replica of the other buffer.

Definition at line 130 of file BCP_buffer.hpp.

References _data, _max_size, _msgtag, _pos, _sender, _size, and BCP_proc_id::clone().

Referenced by BCP_buffer().

00130                                                 {
00131       _msgtag = buf._msgtag;
00132       _sender = buf._sender ? buf._sender->clone() : 0;
00133       _pos = buf._pos;
00134       if (_max_size < buf._max_size) {
00135          delete[] _data;
00136          _data = new char[buf._max_size];
00137          _max_size = buf._max_size;
00138       }
00139       _size = buf._size;
00140       if (_size)
00141          memcpy(_data, buf._data, _size * sizeof(char));
00142       return *this;
00143    }

template<class T>
BCP_buffer& BCP_buffer::pack const typename std::vector< T > &  vec  )  [inline]
 

Pack a std::vector into the buffer.

Definition at line 309 of file BCP_buffer.hpp.

References _data, _size, make_fit(), and size().

00309                                                                          {
00310      int objnum = vec.size();
00311      int new_bytes = objnum * sizeof(T);
00312      make_fit( sizeof(int) + new_bytes );
00313      memcpy(_data + _size, &objnum, sizeof(int));
00314      _size += sizeof(int);
00315      if (objnum > 0){
00316        memcpy(_data + _size, &vec[0], new_bytes);
00317        _size += new_bytes;
00318      }
00319      return *this;
00320    }

template<class T>
BCP_buffer& BCP_buffer::pack const BCP_vec< T > &  vec  )  [inline]
 

Pack a BCP_vec into the buffer.

Definition at line 295 of file BCP_buffer.hpp.

References _data, _size, BCP_vec< T >::begin(), make_fit(), and BCP_vec< T >::size().

00295                                                               {
00296      int objnum = vec.size();
00297      int new_bytes = objnum * sizeof(T);
00298      make_fit( sizeof(int) + new_bytes );
00299      memcpy(_data + _size, &objnum, sizeof(int));
00300      _size += sizeof(int);
00301      if (objnum > 0){
00302        memcpy(_data + _size, vec.begin(), new_bytes);
00303        _size += new_bytes;
00304      }
00305      return *this;
00306    }

BCP_buffer& BCP_buffer::pack BCP_string value  )  [inline]
 

Pack a BCP_string into the buffer.

Definition at line 272 of file BCP_buffer.hpp.

References _data, _size, BCP_string::c_str(), BCP_string::length(), and make_fit().

00272                                       {
00273       // must define here, 'cos in BCP_message.C we have only templated members
00274       int len = value.length();
00275       make_fit( sizeof(int) + len );
00276       memcpy(_data + _size, &len, sizeof(int));
00277       _size += sizeof(int);
00278       if (len > 0){
00279          memcpy(_data + _size, value.c_str(), len);
00280          _size += len;
00281       }
00282       return *this;
00283    }

BCP_buffer& BCP_buffer::pack const BCP_string value  )  [inline]
 

Pack a BCP_string into the buffer.

Definition at line 259 of file BCP_buffer.hpp.

References _data, _size, BCP_string::c_str(), BCP_string::length(), and make_fit().

00259                                             {
00260       // must define here, 'cos in BCP_message.C we have only templated members
00261       int len = value.length();
00262       make_fit( sizeof(int) + len );
00263       memcpy(_data + _size, &len, sizeof(int));
00264       _size += sizeof(int);
00265       if (len > 0){
00266          memcpy(_data + _size, value.c_str(), len);
00267          _size += len;
00268       }
00269       return *this;
00270    }

template<class T>
BCP_buffer& BCP_buffer::pack const T *const  values,
const int  length
[inline]
 

Pack a C style array of objects of type T.

Definition at line 188 of file BCP_buffer.hpp.

References _data, _size, and make_fit().

00189                                                         {
00190      make_fit( sizeof(int) + sizeof(T) * length );
00191      memcpy(_data + _size, &length, sizeof(int));
00192      _size += sizeof(int);
00193      if (length > 0){
00194        memcpy(_data + _size, values, sizeof(T) * length);
00195        _size += sizeof(T) * length;
00196      }
00197      return *this;
00198    }

template<class T>
BCP_buffer& BCP_buffer::pack const T &  value  )  [inline]
 

Pack a single object of type T. Copies sizeof(T) bytes from the address of the object.

Definition at line 168 of file BCP_buffer.hpp.

References _data, _size, and make_fit().

Referenced by BCP_vec_change< char >::pack(), BCP_problem_core_change::pack(), BCP_problem_core::pack(), BCP_parameter_set< BCP_tm_par >::pack(), BCP_lp_relax::pack(), BCP_internal_brobj::pack(), BCP_lp_user::pack_dual_solution(), BCP_lp_user::pack_feasible_solution(), BCP_lp_user::pack_primal_solution(), BCP_cg_user::send_cut(), and BCP_vg_user::send_var().

00168                                                        {
00169      make_fit( sizeof(T) );
00170      memcpy(_data + _size, &value, sizeof(T));
00171      _size += sizeof(T);
00172      return *this;
00173    }

const BCP_proc_id* BCP_buffer::sender  )  const [inline]
 

Return a const pointer to the process id of the sender of the message in the buffer.

Definition at line 92 of file BCP_buffer.hpp.

References _sender.

00092 { return _sender; }

void BCP_buffer::set_content const char *  data,
const size_t  size,
BCP_proc_id sender,
BCP_message_tag  msgtag
[inline]
 

Set the buffer to be a copy of the given data. Use this with care!

Definition at line 113 of file BCP_buffer.hpp.

References _data, _max_size, _msgtag, _pos, _sender, and _size.

00114                                                                 {
00115     _sender = sender;
00116     _msgtag = msgtag;
00117     if (_max_size < size) {
00118       delete[] _data;
00119       _data = new char[size];
00120       _max_size = size;
00121     }
00122     _pos = 0;
00123     _size = size;
00124     if (_size)
00125       memcpy(_data, data, size * sizeof(char));
00126    }

void BCP_buffer::set_msgtag const BCP_message_tag  tag  )  [inline]
 

Set the message tag on the buffer

Definition at line 110 of file BCP_buffer.hpp.

References _msgtag.

Referenced by BCP_lp_user::pack_dual_solution(), and BCP_lp_user::pack_primal_solution().

00110 { _msgtag = tag; }

void BCP_buffer::set_position const int  pos  )  throw (BCP_fatal_error) [inline]
 

Position the read/write head in the buffer. Must be between 0 and size().

Definition at line 104 of file BCP_buffer.hpp.

References _pos, and size().

00104                                                                   {
00105      if (pos < 0 || pos >= size())
00106        throw BCP_fatal_error("Incorrest buffer position setting.\n");
00107      _pos = pos;
00108    }

int BCP_buffer::size  )  const [inline]
 

Return the size of the current message in the buffer.

Definition at line 94 of file BCP_buffer.hpp.

References _size.

Referenced by pack(), and set_position().

00094 { return _size; }

template<class T>
BCP_buffer& BCP_buffer::unpack typename std::vector< T > &  vec  )  [inline]
 

Unpack a std::vector from the buffer.

Definition at line 345 of file BCP_buffer.hpp.

References _data, _pos, and _size.

00345                                                                      {
00346      int objnum;
00347 #ifdef PARANOID
00348      if (_pos + sizeof(int) > _size)
00349        throw BCP_fatal_error("Reading over the end of buffer.\n");
00350 #endif
00351      memcpy(&objnum, _data + _pos, sizeof(int));
00352      _pos += sizeof(int);
00353      vec.clear();
00354      if (objnum > 0){
00355 #ifdef PARANOID
00356        if (_pos + sizeof(T)*objnum > _size)
00357          throw BCP_fatal_error("Reading over the end of buffer.\n");
00358 #endif
00359        vec.insert(vec.end(), objnum, T());
00360        memcpy(&vec[0], _data + _pos, objnum * sizeof(T));
00361        _pos += objnum * sizeof(T);
00362      }
00363      return *this;
00364    }

template<class T>
BCP_buffer& BCP_buffer::unpack BCP_vec< T > &  vec  )  [inline]
 

Unpack a BCP_vec from the buffer.

Definition at line 323 of file BCP_buffer.hpp.

References _data, _pos, _size, BCP_vec< T >::clear(), BCP_vec< T >::end(), BCP_vec< T >::insert(), and BCP_vec< T >::reserve().

00323                                                           {
00324      int objnum;
00325 #ifdef PARANOID
00326      if (_pos + sizeof(int) > _size)
00327        throw BCP_fatal_error("Reading over the end of buffer.\n");
00328 #endif
00329      memcpy(&objnum, _data + _pos, sizeof(int));
00330      _pos += sizeof(int);
00331      vec.clear();
00332      if (objnum > 0){
00333 #ifdef PARANOID
00334        if (_pos + sizeof(T)*objnum > _size)
00335          throw BCP_fatal_error("Reading over the end of buffer.\n");
00336 #endif
00337        vec.reserve(objnum);
00338        vec.insert(vec.end(), _data + _pos, objnum);
00339        _pos += objnum * sizeof(T);
00340      }
00341      return *this;
00342    }

BCP_buffer& BCP_buffer::unpack BCP_string value  )  [inline]
 

Unpack a BCP_string from the buffer.

Definition at line 285 of file BCP_buffer.hpp.

References _data, _pos, BCP_string::assign(), and unpack().

00285                                         {
00286       int len;
00287       unpack(len);
00288       value.assign(_data + _pos, len);
00289       _pos += len;
00290       return *this;
00291    }

template<class T>
BCP_buffer& BCP_buffer::unpack T *&  values,
int &  length,
bool  allocate = true
throw (BCP_fatal_error) [inline]
 

Unpack an array of objects of type T, where T must be a built-in type (ar at least something that can be copied with memcpy).

If the third argument is true then memory is allocated for the array and the array pointer and the length of the array are returned in the arguments.

If the third argument is false then the arriving array's length is compared to length and an exception is thrown if they are not the same. Also, the array passed as the first argument will be filled with the arriving array.

Definition at line 213 of file BCP_buffer.hpp.

References _data, _pos, and _size.

00215                             {
00216      if (allocate) {
00217 #ifdef PARANOID
00218        if (_pos + sizeof(int) > _size)
00219          throw BCP_fatal_error("Reading over the end of buffer.\n");
00220 #endif
00221        memcpy(&length, _data + _pos, sizeof(int));
00222        _pos += sizeof(int);
00223        if (length > 0){
00224 #ifdef PARANOID
00225          if (_pos + sizeof(T)*length > _size)
00226            throw BCP_fatal_error("Reading over the end of buffer.\n");
00227 #endif
00228          values = new T[length];
00229          memcpy(values, _data + _pos, sizeof(T)*length);
00230          _pos += sizeof(T) * length;
00231        }
00232        
00233      } else { /* ! allocate */
00234 
00235        int l;
00236 #ifdef PARANOID
00237        if (_pos + sizeof(int) > _size)
00238          throw BCP_fatal_error("Reading over the end of buffer.\n");
00239 #endif
00240        memcpy(&l, _data + _pos, sizeof(int));
00241        _pos += sizeof(int);
00242        if (l != length)
00243          throw BCP_fatal_error("BCP_buffer::unpack() : bad array lentgh.\n");
00244        if (length > 0){
00245 #ifdef PARANOID
00246          if (_pos + sizeof(T)*length > _size)
00247            throw BCP_fatal_error("Reading over the end of buffer.\n");
00248 #endif
00249          memcpy(values, _data + _pos, sizeof(T)*length);
00250          _pos += sizeof(T) * length;
00251        }
00252        
00253      }
00254      
00255      return *this;
00256    }

template<class T>
BCP_buffer& BCP_buffer::unpack T &  value  )  [inline]
 

Unpack a single object of type T. Copies sizeof(T) bytes to the address of the object.

Definition at line 177 of file BCP_buffer.hpp.

References _data, _pos, and _size.

Referenced by BCP_vg_prob::probe_messages(), BCP_cg_prob::probe_messages(), BCP_vec_change< char >::unpack(), BCP_problem_core_change::unpack(), BCP_problem_core::unpack(), BCP_parameter_set< BCP_tm_par >::unpack(), BCP_lp_relax::unpack(), unpack(), BCP_internal_brobj::unpack(), BCP_vg_prob::unpack_cut(), BCP_vg_user::unpack_dual_solution(), BCP_tm_user::unpack_feasible_solution(), BCP_cg_user::unpack_primal_solution(), and BCP_cg_prob::unpack_var().

00177                                                   {
00178 #ifdef PARANOID
00179      if (_pos + sizeof(T) > _size)
00180        throw BCP_fatal_error("Reading over the end of buffer.\n");
00181 #endif
00182      memcpy(&value, _data + _pos, sizeof(T));
00183      _pos += sizeof(T);
00184      return *this;
00185    }


Member Data Documentation

char* BCP_buffer::_data
 

Pointer to the buffer itself.

Definition at line 81 of file BCP_buffer.hpp.

Referenced by BCP_buffer(), data(), make_fit(), operator=(), pack(), set_content(), unpack(), and ~BCP_buffer().

size_t BCP_buffer::_max_size
 

The amount of memory allocated for the buffer.

Definition at line 76 of file BCP_buffer.hpp.

Referenced by BCP_buffer(), make_fit(), operator=(), and set_content().

BCP_message_tag BCP_buffer::_msgtag
 

The message tag of the last received message. This member has no meaning if the buffer holds an outgoing message.

Definition at line 69 of file BCP_buffer.hpp.

Referenced by BCP_buffer(), clear(), msgtag(), operator=(), set_content(), and set_msgtag().

size_t BCP_buffer::_pos
 

The next read/write position in the buffer.

Definition at line 74 of file BCP_buffer.hpp.

Referenced by BCP_buffer(), clear(), operator=(), set_content(), set_position(), and unpack().

BCP_proc_id* BCP_buffer::_sender
 

The process id of the sender of the last received message. This member has no meaning if the buffer holds an outgoing message.

Definition at line 72 of file BCP_buffer.hpp.

Referenced by BCP_buffer(), clear(), operator=(), sender(), set_content(), and ~BCP_buffer().

size_t BCP_buffer::_size
 

The current size of the message (the first _size bytes of the buffer).

Definition at line 79 of file BCP_buffer.hpp.

Referenced by BCP_buffer(), clear(), make_fit(), operator=(), pack(), set_content(), size(), and unpack().


The documentation for this class was generated from the following file:
Generated on Wed Dec 3 14:32:36 2003 for BCP by doxygen 1.3.5