Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
gar::geo::BitFieldCoder Class Reference

Helper class for decoding and encoding a bit field of 64bits for convenient declaration. More...

#include <BitFieldCoder.h>

Public Types

typedef std::map< std::string, unsigned int > IndexMap
 

Public Member Functions

 BitFieldCoder ()=default
 Default constructor. More...
 
 BitFieldCoder (const BitFieldCoder &)=default
 Copy constructor. More...
 
 BitFieldCoder (BitFieldCoder &&)=default
 Move constructor. More...
 
 ~BitFieldCoder ()=default
 Default destructor. More...
 
BitFieldCoderoperator= (const BitFieldCoder &)=default
 Assignment operator. More...
 
 BitFieldCoder (const std::string &initString)
 
long64 get (long64 bitfield, size_t index) const
 
long64 get (long64 bitfield, const std::string &name) const
 
void set (long64 &bitfield, size_t index, ulong64 value) const
 
void set (long64 &bitfield, const std::string &name, ulong64 value) const
 
unsigned highestBit () const
 
size_t size () const
 
size_t index (const std::string &name) const
 
const BitFieldValueoperator[] (const std::string &name) const
 
const BitFieldValueoperator[] (unsigned index) const
 
std::string fieldDescription () const
 
std::string valueString (ulong64 bitfield) const
 
const std::vector< BitFieldValue > & fields () const
 
ulong64 mask () const
 

Static Public Member Functions

static long64 toLong (unsigned low_Word, unsigned high_Word)
 
static unsigned lowWord (long64 bitfield)
 
static unsigned highWord (long64 bitfield)
 

Protected Member Functions

void addField (const std::string &name, unsigned offset, int width)
 
void init (const std::string &initString)
 

Protected Attributes

std::vector< BitFieldValue_fields {}
 
IndexMap _map {}
 
long64 _joined {}
 

Detailed Description

Helper class for decoding and encoding a bit field of 64bits for convenient declaration.

and manipulation of sub fields of various widths.
This is a thread safe re-implementation of the functionality in the deprected BitField64.

Example:
BitFieldCoder bc("layer:7,system:-3,barrel:3,theta:32:11,phi:11" ) ;
bc.set( field, "layer" , 123 );
bc.set( field, "system" , -4 );
bc.set( field, "barrel" , 7 );
bc.set( field, "theta" , 180 );
bc.set( field, "phi" , 270 );
...
int theta = bc.get( field, "theta" ) ;
...
unsigned phiIndex = bc.index("phi") ;
int phi = bc.get( field, phiIndex ) ;

Author
F.Gaede, DESY
Date
2017-09

Definition at line 130 of file BitFieldCoder.h.

Member Typedef Documentation

typedef std::map<std::string, unsigned int> gar::geo::BitFieldCoder::IndexMap

Definition at line 134 of file BitFieldCoder.h.

Constructor & Destructor Documentation

gar::geo::BitFieldCoder::BitFieldCoder ( )
default

Default constructor.

gar::geo::BitFieldCoder::BitFieldCoder ( const BitFieldCoder )
default

Copy constructor.

gar::geo::BitFieldCoder::BitFieldCoder ( BitFieldCoder &&  )
default

Move constructor.

gar::geo::BitFieldCoder::~BitFieldCoder ( )
default

Default destructor.

gar::geo::BitFieldCoder::BitFieldCoder ( const std::string initString)
inline

The c'tor takes an initialization string of the form:
<fieldDesc>[,<fieldDesc>...]
fieldDesc = name:[start]:[-]length
where:
name: The name of the field
start: The start bit of the field. If omitted assumed to start immediately following previous field, or at the least significant bit if the first field.
length: The number of bits in the field. If preceeded by '-' the field is signed, otherwise unsigned.
Bit numbering is from the least significant bit (bit 0) to the most significant (bit 63).
Example: "layer:7,system:-3,barrel:3,theta:32:11,phi:11"

Definition at line 162 of file BitFieldCoder.h.

162  : _joined(0)
163  {
164  init( initString ) ;
165  }
void init(const std::string &initString)

Member Function Documentation

void gar::geo::BitFieldCoder::addField ( const std::string name,
unsigned  offset,
int  width 
)
protected

Add an additional field to the list

Definition at line 141 of file BitFieldCoder.cxx.

141  {
142 
143 
144  _fields.push_back( BitFieldValue( name, offset, width ) ) ;
145 
146  BitFieldValue& bfv = _fields.back() ;
147 
148  _map[ name ] = _fields.size()-1 ;
149 
150  if( _joined & bfv.mask() ) {
151 
152  std::stringstream s ;
153  s << " BitFieldValue::addField(" << name << "): bits already used " << std::hex << _joined
154  << " for mask " << bfv.mask() ;
155 
156  throw( std::runtime_error( s.str() ) ) ;
157 
158  }
159 
160  _joined |= bfv.mask() ;
161 
162  }
static QCString name
Definition: declinfo.cpp:673
std::vector< BitFieldValue > _fields
QTextStream & hex(QTextStream &s)
static QCString * s
Definition: config.cpp:1042
std::string gar::geo::BitFieldCoder::fieldDescription ( ) const

Return a valid description string of all fields

Definition at line 119 of file BitFieldCoder.cxx.

119  {
120 
121  std::stringstream os ;
122 
123  for(unsigned i=0;i<_fields.size();i++){
124 
125  if( i != 0 ) os << "," ;
126 
127  os << _fields[i].name() << ":"
128  << _fields[i].offset() << ":" ;
129 
130  if( _fields[i].isSigned() )
131  os << "-" ;
132 
133  os << _fields[i].width() ;
134 
135  }
136 
137  return os.str() ;
138  }
std::vector< BitFieldValue > _fields
const std::vector<BitFieldValue>& gar::geo::BitFieldCoder::fields ( ) const
inline

Definition at line 223 of file BitFieldCoder.h.

223 { return _fields;}
std::vector< BitFieldValue > _fields
long64 gar::geo::BitFieldCoder::get ( long64  bitfield,
size_t  index 
) const
inline

get value of sub-field specified by index

Definition at line 181 of file BitFieldCoder.h.

181 { return _fields.at(index).value( bitfield ); }
std::vector< BitFieldValue > _fields
size_t index(const std::string &name) const
long64 gar::geo::BitFieldCoder::get ( long64  bitfield,
const std::string name 
) const
inline

Access to field through name .

Definition at line 185 of file BitFieldCoder.h.

185 { return _fields.at( index( name ) ).value( bitfield ); }
static QCString name
Definition: declinfo.cpp:673
std::vector< BitFieldValue > _fields
size_t index(const std::string &name) const
unsigned gar::geo::BitFieldCoder::highestBit ( ) const

Highest bit used in fields [0-63]

Definition at line 91 of file BitFieldCoder.cxx.

91  {
92 
93  unsigned hb(0) ;
94 
95  for(unsigned i=0;i<_fields.size();i++){
96 
97  if( hb < ( _fields[i].offset() + _fields[i].width() ) )
98  hb = _fields[i].offset() + _fields[i].width() ;
99  }
100  return hb ;
101  }
std::vector< BitFieldValue > _fields
static unsigned gar::geo::BitFieldCoder::highWord ( long64  bitfield)
inlinestatic

The high word, bits 32-63

Definition at line 177 of file BitFieldCoder.h.

177 { return unsigned( bitfield >> 32); }
size_t gar::geo::BitFieldCoder::index ( const std::string name) const

Index for field named 'name'

Definition at line 78 of file BitFieldCoder.cxx.

78  {
79 
80  IndexMap::const_iterator it = _map.find( name ) ;
81 
82  if( it != _map.end() )
83 
84  return it->second ;
85 
86  else
87  throw std::runtime_error(" BitFieldValue: unknown name: " + name ) ;
88  }
static QCString name
Definition: declinfo.cpp:673
intermediate_table::const_iterator const_iterator
void gar::geo::BitFieldCoder::init ( const std::string initString)
protected

Decode the initialization string as described in the constructor.

See also
BitFieldCoder( const std::string& initString )

Definition at line 165 of file BitFieldCoder.cxx.

165  {
166 
167  unsigned offset = 0 ;
168 
169  // need to compute bit field masks and offsets ...
170  std::vector<std::string> fieldDescriptors ;
171  Tokenizer t( fieldDescriptors ,',') ;
172 
173  std::for_each( initString.begin(), initString.end(), t ) ;
174 
175  for(unsigned i=0; i< fieldDescriptors.size() ; i++ ){
176 
177  std::vector<std::string> subfields ;
178  Tokenizer ts( subfields ,':') ;
179 
180  std::for_each( fieldDescriptors[i].begin(), fieldDescriptors[i].end(), ts );
181 
182  std::string name ;
183  int width ;
184  unsigned thisOffset ;
185 
186  switch( subfields.size() ){
187 
188  case 2:
189 
190  name = subfields[0] ;
191  width = atol( subfields[1].c_str() ) ;
192  thisOffset = offset ;
193 
194  offset += abs( width ) ;
195 
196  break ;
197 
198  case 3:
199  name = subfields[0] ;
200  thisOffset = atol( subfields[1].c_str() ) ;
201  width = atol( subfields[2].c_str() ) ;
202 
203  offset = thisOffset + abs( width ) ;
204 
205  break ;
206 
207  default:
208 
209  std::stringstream s ;
210  s << " BitFieldCoder: invalid number of subfields "
211  << fieldDescriptors[i] ;
212 
213  throw( std::runtime_error( s.str() ) ) ;
214  }
215 
216  addField( name , thisOffset, width ) ;
217  }
218  }
static QCString name
Definition: declinfo.cpp:673
end
while True: pbar.update(maxval-len(onlies[E][S])) #print iS, "/", len(onlies[E][S]) found = False for...
std::string string
Definition: nybbler.cc:12
T abs(T value)
void addField(const std::string &name, unsigned offset, int width)
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:72
static QCString * s
Definition: config.cpp:1042
static unsigned gar::geo::BitFieldCoder::lowWord ( long64  bitfield)
inlinestatic

The low word, bits 0-31

Definition at line 173 of file BitFieldCoder.h.

173 { return unsigned( bitfield & 0xffffFFFFUL ); }
ulong64 gar::geo::BitFieldCoder::mask ( ) const
inline

the mask of all the bits used in the description

Definition at line 226 of file BitFieldCoder.h.

226 { return _joined ; }
BitFieldCoder& gar::geo::BitFieldCoder::operator= ( const BitFieldCoder )
default

Assignment operator.

const BitFieldValue& gar::geo::BitFieldCoder::operator[] ( const std::string name) const
inline

Const Access to field through name .

Definition at line 209 of file BitFieldCoder.h.

209 { return _fields[ index( name ) ] ;}
static QCString name
Definition: declinfo.cpp:673
std::vector< BitFieldValue > _fields
size_t index(const std::string &name) const
const BitFieldValue& gar::geo::BitFieldCoder::operator[] ( unsigned  index) const
inline

Const Access to field through index .

Definition at line 213 of file BitFieldCoder.h.

213 { return _fields[ index ] ;}
std::vector< BitFieldValue > _fields
size_t index(const std::string &name) const
void gar::geo::BitFieldCoder::set ( long64 bitfield,
size_t  index,
ulong64  value 
) const
inline

set value of sub-field specified by index

Definition at line 189 of file BitFieldCoder.h.

189 { _fields.at(index).set( bitfield , value ); }
std::vector< BitFieldValue > _fields
size_t index(const std::string &name) const
void gar::geo::BitFieldCoder::set ( long64 bitfield,
const std::string name,
ulong64  value 
) const
inline

Access to field through name .

Definition at line 193 of file BitFieldCoder.h.

193 { _fields.at( index( name ) ).set( bitfield, value ); }
static QCString name
Definition: declinfo.cpp:673
std::vector< BitFieldValue > _fields
size_t index(const std::string &name) const
size_t gar::geo::BitFieldCoder::size ( ) const
inline

Number of values

Definition at line 201 of file BitFieldCoder.h.

201 { return _fields.size() ; }
std::vector< BitFieldValue > _fields
static long64 gar::geo::BitFieldCoder::toLong ( unsigned  low_Word,
unsigned  high_Word 
)
inlinestatic

return a new 64bit value given as high and low 32bit words.

Definition at line 169 of file BitFieldCoder.h.

169 { return ( ( low_Word & 0xffffffffULL ) | ( ( high_Word & 0xffffffffULL ) << 32 ) ); }
std::string gar::geo::BitFieldCoder::valueString ( ulong64  bitfield) const

Return a string with a comma separated list of the current sub field values

Definition at line 104 of file BitFieldCoder.cxx.

104  {
105 
106  std::stringstream os ;
107 
108  for(unsigned i=0;i<_fields.size();i++){
109 
110  if( i != 0 ) os << "," ;
111 
112  os << _fields[i].name() << ":" << _fields[i].value(bitfield) ;
113 
114  }
115  return os.str() ;
116  }
std::vector< BitFieldValue > _fields

Member Data Documentation

std::vector<BitFieldValue> gar::geo::BitFieldCoder::_fields {}
protected

Definition at line 243 of file BitFieldCoder.h.

long64 gar::geo::BitFieldCoder::_joined {}
protected

Definition at line 245 of file BitFieldCoder.h.

IndexMap gar::geo::BitFieldCoder::_map {}
protected

Definition at line 244 of file BitFieldCoder.h.


The documentation for this class was generated from the following files: