inlib  1.2.0
Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes
inlib::wroot::buffer Class Reference
Inheritance diagram for inlib::wroot::buffer:
Inheritance graph
[legend]
Collaboration diagram for inlib::wroot::buffer:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 buffer (std::ostream &a_out, bool a_byte_swap, uint32 a_size)
virtual ~buffer ()
std::ostream & out () const
char * buf ()
const char * buf () const
uint32 length () const
uint32 size () const
char *& pos ()
char * max_pos () const
template<class T >
bool write (T x)
bool write (bool x)
bool write (const std::string &x)
bool write_fast_array (const char *a_a, uint32 a_n)
bool write_cstring (const char *a_s)
template<class T >
bool write_fast_array (const T *a_a, uint32 a_n)
template<class T >
bool write_array (const T *a_a, uint32 a_n)
bool write_version (short a_version)
bool write_version (short a_version, uint32 &a_pos)
bool set_byte_count (uint32 a_pos)
bool write_object (const ibo &a_obj)
bool expand (uint32 a_new_size)
unsigned int to_displace () const
bool displace_mapped (unsigned int a_num)

Protected Member Functions

 buffer (const buffer &a_from)
bufferoperator= (const buffer &)
bool write_class (const std::string &a_cls)
bool set_byte_count_obj (uint32 a_pos)

Static Protected Member Functions

static short kMaxVersion ()
static uint32 kMaxMapCount ()
static short kByteCountVMask ()
static uint32 kNewClassTag ()
static int kMapOffset ()
static unsigned int kClassMask ()
static uint32 kByteCountMask ()
static std::string sout (const std::string &a_string)

Protected Attributes

std::ostream & m_out
bool m_byte_swap
uint32 m_size
char * m_buffer
char * m_max
char * m_pos
wbuf m_wb
std::map< ibo *, uint32m_objs
std::vector< std::pair< uint32,
uint32 > > 
m_obj_mapped
std::map< std::string, uint32m_clss
std::vector< std::pair< uint32,
uint32 > > 
m_cls_mapped

Detailed Description

Definition at line 26 of file buffer.


Constructor & Destructor Documentation

inlib::wroot::buffer::buffer ( std::ostream &  a_out,
bool  a_byte_swap,
uint32  a_size 
) [inline]

Definition at line 32 of file buffer.

  :m_out(a_out)
  ,m_byte_swap(a_byte_swap)
  ,m_size(0)
  ,m_buffer(0)
  ,m_max(0)
  ,m_pos(0)
  ,m_wb(a_out,a_byte_swap,0,m_pos) //it holds a ref on m_pos.
  {
#ifdef INLIB_MEM
    mem::increment(s_class().c_str());
#endif
    m_size = a_size;
    m_buffer = new char[m_size];
    //if(!m_buffer) {}
    m_max = m_buffer+m_size;
    m_pos = m_buffer;
    m_wb.set_eob(m_max);
  }
virtual inlib::wroot::buffer::~buffer ( ) [inline, virtual]

Definition at line 54 of file buffer.

                   {
    m_objs.clear();
    m_obj_mapped.clear();

    m_clss.clear();
    m_cls_mapped.clear();

    delete [] m_buffer;
#ifdef INLIB_MEM
    mem::decrement(s_class().c_str());
#endif
  }
inlib::wroot::buffer::buffer ( const buffer a_from) [inline, protected]

Definition at line 67 of file buffer.

  :m_out(a_from.m_out)
  ,m_byte_swap(a_from.m_byte_swap)
  ,m_size(0)
  ,m_buffer(0)
  ,m_max(0)
  ,m_pos(0)
  ,m_wb(a_from.m_out,a_from.m_byte_swap,0,m_pos)
  {
#ifdef INLIB_MEM
    mem::increment(s_class().c_str());
#endif
  }

Member Function Documentation

char* inlib::wroot::buffer::buf ( ) [inline]

Definition at line 86 of file buffer.

{return m_buffer;}
const char* inlib::wroot::buffer::buf ( ) const [inline]

Definition at line 87 of file buffer.

{return m_buffer;}
bool inlib::wroot::buffer::displace_mapped ( unsigned int  a_num) [inline]

Definition at line 275 of file buffer.

                                          {
    char* opos = m_pos;

    //m_out << "inlib::wroot::buffer::displace_mapped :"
    //      << " cls num " << m_cls_mapped.size()
    //      << std::endl;

   {std::vector< std::pair<uint32,uint32> >::const_iterator it;
    for(it=m_cls_mapped.begin();it!=m_cls_mapped.end();++it) {
      unsigned int offset = (*it).first;
      unsigned int id = (*it).second;
      //m_out << "displace " << offset << " " << id << std::endl;
      m_pos = m_buffer+offset;
      unsigned int clIdx = id+a_num;
      if(!write(uint32(clIdx|kClassMask()))) {m_pos = opos;return false;}
    }}
  
    //m_out << "inlib::wroot::buffer::displace_mapped :"
    //      << " obj num " << m_obj_mapped.size()
    //      << std::endl;

   {std::vector< std::pair<uint32,uint32> >::const_iterator it;
    for(it=m_obj_mapped.begin();it!=m_obj_mapped.end();++it) {
      uint32 offset = (*it).first;
      uint32 id = (*it).second;
      //m_out << "displace at " << offset
      //      << " the obj pos " << id
      //      << " by " << a_num
      //      << std::endl;
      m_pos = m_buffer+offset;
      unsigned int objIdx = id+a_num;
      if(!write(objIdx)) {m_pos = opos;return false;}
    }}
  
    m_pos = opos;
    return true;
  }
bool inlib::wroot::buffer::expand ( uint32  a_new_size) [inline]

Definition at line 252 of file buffer.

                                 {
    unsigned long len = m_pos-m_buffer;
    if(!realloc<char>(m_buffer,a_new_size,m_size)) {
      m_out << "inlib::wroot::buffer::expand :"
            << " can't realloc " << a_new_size << " bytes."
            << std::endl;
      m_size = 0;
      m_max = 0;
      m_pos = 0;
      m_wb.set_eob(m_max);
      return false;
    }
    m_size = a_new_size;
    m_max = m_buffer + m_size;
    m_pos = m_buffer + len;
    m_wb.set_eob(m_max);
    return true;
  }
static uint32 inlib::wroot::buffer::kByteCountMask ( ) [inline, static, protected]

Definition at line 321 of file buffer.

{return 0x40000000;}
static short inlib::wroot::buffer::kByteCountVMask ( ) [inline, static, protected]

Definition at line 316 of file buffer.

{return 0x4000;}
static unsigned int inlib::wroot::buffer::kClassMask ( ) [inline, static, protected]

Definition at line 320 of file buffer.

{return 0x80000000;}
static int inlib::wroot::buffer::kMapOffset ( ) [inline, static, protected]

Definition at line 319 of file buffer.

{return 2;}
static uint32 inlib::wroot::buffer::kMaxMapCount ( ) [inline, static, protected]

Definition at line 315 of file buffer.

{return 0x3FFFFFFE;}
static short inlib::wroot::buffer::kMaxVersion ( ) [inline, static, protected]

Definition at line 314 of file buffer.

{return 0x3FFF;}
static uint32 inlib::wroot::buffer::kNewClassTag ( ) [inline, static, protected]

Definition at line 317 of file buffer.

{return 0xFFFFFFFF;}
uint32 inlib::wroot::buffer::length ( ) const [inline]

Definition at line 88 of file buffer.

{return m_pos-m_buffer;}
char* inlib::wroot::buffer::max_pos ( ) const [inline]

Definition at line 92 of file buffer.

{return m_max;} //used in basket.
buffer& inlib::wroot::buffer::operator= ( const buffer ) [inline, protected]

Definition at line 80 of file buffer.

{return *this;}
std::ostream& inlib::wroot::buffer::out ( ) const [inline]

Definition at line 82 of file buffer.

{return m_out;}
char*& inlib::wroot::buffer::pos ( ) [inline]

Definition at line 91 of file buffer.

{return m_pos;}          //used in basket.
bool inlib::wroot::buffer::set_byte_count ( uint32  a_pos) [inline]

Definition at line 177 of file buffer.

                                   {
    uint32 cnt = (uint32)(m_pos-m_buffer) - a_pos - sizeof(unsigned int);
    if(cnt>=kMaxMapCount()) {
      m_out << "inlib::wroot::buffer::set_byte_count :"
            << " bytecount too large (more than "
            << kMaxMapCount() << ")."
            << std::endl;
      return false;
    }

    union {
      uint32 cnt;
      short vers[2];
    } v;
    v.cnt = cnt;

    char* opos = m_pos;
    m_pos = (char*)(m_buffer+a_pos);
    if(m_byte_swap) {
      if(!m_wb.write(short(v.vers[1]|kByteCountVMask()))) 
        {m_pos = opos;return false;}
      if(!m_wb.write(v.vers[0])) {m_pos = opos;return false;}
    } else {
      if(!m_wb.write(short(v.vers[0]|kByteCountVMask())))
        {m_pos = opos;return false;}
      if(!m_wb.write(v.vers[1])) {m_pos = opos;return false;}
    }
    m_pos = opos;

    return true;
  }
bool inlib::wroot::buffer::set_byte_count_obj ( uint32  a_pos) [inline, protected]

Definition at line 347 of file buffer.

                                       {
    uint32 cnt = (uint32)(m_pos-m_buffer) - a_pos - sizeof(unsigned int);
    if(cnt>=kMaxMapCount()) {
      m_out << "inlib::wroot::buffer::set_byte_count_obj :"
            << " bytecount too large (more than "
            << kMaxMapCount() << ")."
            << std::endl;
      return false;
    }
    char* opos = m_pos;
    m_pos = (char*)(m_buffer+a_pos);
    if(!m_wb.write(uint32(cnt|kByteCountMask()))) {m_pos = opos;return false;}
    m_pos = opos;
    return true;
  }
uint32 inlib::wroot::buffer::size ( ) const [inline]

Definition at line 89 of file buffer.

{return m_size;}
static std::string inlib::wroot::buffer::sout ( const std::string &  a_string) [inline, static, protected]

Definition at line 363 of file buffer.

                                                   {
    return std::string("\"")+a_string+"\"";
  }
unsigned int inlib::wroot::buffer::to_displace ( ) const [inline]

Definition at line 271 of file buffer.

                                   {
    return m_cls_mapped.size()+m_obj_mapped.size();
  }
template<class T >
bool inlib::wroot::buffer::write ( x) [inline]

Definition at line 96 of file buffer.

                 {
    if(m_pos+sizeof(T)>m_max) {
      if(!expand(m_size+sizeof(T))) return false;
    }
    return m_wb.write(x);
  }
bool inlib::wroot::buffer::write ( bool  x) [inline]

Definition at line 103 of file buffer.

                    { 
    return write<unsigned char>(x?1:0);
  }
bool inlib::wroot::buffer::write ( const std::string &  x) [inline]

Definition at line 107 of file buffer.

                                 {
    uint32 sz = (uint32)(x.size() + sizeof(int) + 1);
    if((m_pos+sz)>m_max) {
      if(!expand(m_size+sz)) return false;
    }
    return m_wb.write(x);
  }
template<class T >
bool inlib::wroot::buffer::write_array ( const T *  a_a,
uint32  a_n 
) [inline]

Definition at line 141 of file buffer.

                                            {
    if(!write(a_n)) return false;
    return write_fast_array(a_a,a_n);
  }
bool inlib::wroot::buffer::write_class ( const std::string &  a_cls) [inline, protected]

Definition at line 323 of file buffer.

                                          {

    std::map<std::string,uint32>::const_iterator it = m_clss.find(a_cls);
    if(it!=m_clss.end()) {
      uint32 clIdx = (*it).second;

      unsigned int offset = (unsigned int)(m_pos-m_buffer);

      // save index of already stored class
      if(!write(uint32(clIdx|kClassMask()))) return false;

      m_cls_mapped.push_back(std::pair<uint32,uint32>(offset,clIdx));

    } else {

      unsigned int offset = (unsigned int)(m_pos-m_buffer);
      if(!write(kNewClassTag())) return false;
      if(!write_cstring(a_cls.c_str())) return false;
      m_clss[a_cls] = offset + kMapOffset();

    }
    return true;
  }
bool inlib::wroot::buffer::write_cstring ( const char *  a_s) [inline]

Definition at line 126 of file buffer.

                                      {
    return write_fast_array(a_s,(::strlen(a_s)+1)*sizeof(char));
  }
bool inlib::wroot::buffer::write_fast_array ( const char *  a_a,
uint32  a_n 
) [inline]

Definition at line 115 of file buffer.

                                                    {
    if(!a_n) return true;
    uint32 l = a_n * sizeof(char);
    if((m_pos+l)>m_max) {
      if(!expand(m_size+l)) return false;
    }
    ::memcpy(m_pos,a_a,l);
    m_pos += l;
    return true;
  }
template<class T >
bool inlib::wroot::buffer::write_fast_array ( const T *  a_a,
uint32  a_n 
) [inline]

Definition at line 131 of file buffer.

                                                 {
    if(!a_n) return true;
    uint32 l = a_n * sizeof(T);
    if((m_pos+l)>m_max) {
      if(!expand(m_size+l)) return false;
    }
    return m_wb.write<T>(a_a,a_n);
  }
bool inlib::wroot::buffer::write_object ( const ibo a_obj) [inline]

Definition at line 209 of file buffer.

                                     {
    //GB : if adding a write map logic, think to have a displace_mapped()
    //     in basket::write_on_file().

    std::map<ibo*,uint32>::const_iterator it = m_objs.find((ibo*)&a_obj);
    if(it!=m_objs.end()) {
      uint32 objIdx = (*it).second;

      unsigned int offset = (unsigned int)(m_pos-m_buffer);

      // save index of already stored object
      if(!write(objIdx)) return false;

      m_obj_mapped.push_back(std::pair<uint32,uint32>(offset,objIdx));

    } else {

      // reserve space for leading byte count
      uint32 cntpos = (unsigned int)(m_pos-m_buffer);

      //NOTE : the below test is lacking in CERN-ROOT !
      if((m_pos+sizeof(unsigned int))>m_max) {
        if(!expand(m_size+sizeof(unsigned int))) return false;
      }
      m_pos += sizeof(unsigned int);
    
      // write class of object first
      if(!write_class(a_obj.store_cls())) return false;
    
      // add to map before writing rest of object (to handle self reference)
      // (+kMapOffset so it's != kNullTag)
      m_objs[(ibo*)&a_obj] = cntpos + kMapOffset();
    
      // let the object write itself :
      if(!a_obj.stream(*this)) return false;
    
      // write byte count
      if(!set_byte_count_obj(cntpos)) return false;
    }
    return true;
  }
bool inlib::wroot::buffer::write_version ( short  a_version) [inline]

Definition at line 147 of file buffer.

                                     {
    if(a_version>kMaxVersion()) {
      m_out << "inlib::wroot::buffer::write_version :"
            << " version number " << a_version
            << " cannot be larger than " << kMaxVersion() << "."
            << std::endl;
      return false;
    }
    return write(a_version);
  }
bool inlib::wroot::buffer::write_version ( short  a_version,
uint32 a_pos 
) [inline]

Definition at line 157 of file buffer.

                                                   {
    // reserve space for leading byte count
    a_pos = (uint32)(m_pos-m_buffer);

    //NOTE : the below test is lacking in CERN-ROOT !
    if((m_pos+sizeof(unsigned int))>m_max) {
      if(!expand(m_size+sizeof(unsigned int))) return false;
    }
    m_pos += sizeof(unsigned int);

    if(a_version>kMaxVersion()) {
      m_out << "inlib::wroot::buffer::write_version :"
            << " version number " << a_version
            << " cannot be larger than " << kMaxVersion() << "."
            << std::endl;
      return false;
    }
    return write(a_version);
  }

Member Data Documentation

char* inlib::wroot::buffer::m_buffer [protected]

Definition at line 370 of file buffer.

Definition at line 368 of file buffer.

std::vector< std::pair<uint32,uint32> > inlib::wroot::buffer::m_cls_mapped [protected]

Definition at line 379 of file buffer.

std::map<std::string,uint32> inlib::wroot::buffer::m_clss [protected]

Definition at line 378 of file buffer.

char* inlib::wroot::buffer::m_max [protected]

Definition at line 371 of file buffer.

std::vector< std::pair<uint32,uint32> > inlib::wroot::buffer::m_obj_mapped [protected]

Definition at line 376 of file buffer.

std::map<ibo*,uint32> inlib::wroot::buffer::m_objs [protected]

Definition at line 375 of file buffer.

std::ostream& inlib::wroot::buffer::m_out [protected]

Definition at line 367 of file buffer.

char* inlib::wroot::buffer::m_pos [protected]

Definition at line 372 of file buffer.

Definition at line 369 of file buffer.

Definition at line 373 of file buffer.


The documentation for this class was generated from the following file:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines