ATLAS Offline Software
Loading...
Searching...
No Matches
D3PD::ObjectMetadata Class Reference

D3PD variable metadata handling class. More...

#include <ObjectMetadata.h>

Inheritance diagram for D3PD::ObjectMetadata:
Collaboration diagram for D3PD::ObjectMetadata:

Classes

class  Variable
 Internal class keeping track of a single variable. More...

Public Member Functions

 ObjectMetadata ()
 Default constructor.
 ObjectMetadata (const ObjectMetadata &parent)
 Copy constructor.
ObjectMetadataoperator= (const ObjectMetadata &parent)
 Assignment operator.
bool operator== (const ObjectMetadata &rhs) const
 Equality operator.
bool operator< (const ObjectMetadata &rhs) const
 Operator needed to use such objects in ordered STL containers.
virtual StatusCode addVariable (const std::string &name, const std::type_info &ti, void *&ptr, const std::string &docstring="", const void *defval=0)
 This function can be used to save the metadata about a D3PD variable.
virtual StatusCode addDimensionedVariable (const std::string &name, const std::type_info &ti, void *&ptr, const std::string &dim, const std::string &docstring="", const void *defval=0)
 The object doesn't support dimensioned variables at the moment, like most of the D3PDMaker code doesn't.
const std::string & name () const
 Get the name of the D3PDObject that this object describes.
void setName (const std::string &name)
 Set the name of the D3PDObject that this object describes.
std::string metadataName ATLAS_NOT_THREAD_SAFE () const
 Get the name for the metadata object that should be created.
const std::string & prefix () const
 Get the prefix given to variables in this D3PDObject.
void setPrefix (const std::string &prefix)
 Set the prefix given to variables in this D3PDObject.
bool container () const
 Get whether the D3PDObject describes a container or not.
void setContainer (bool container)
 Set whether the D3PDObject describes a container or not.
std::string toString () const
 Function "serializing" the stored information into a string.
StatusCode read (const std::string &data)
 Function "de-serializing" the stored information from a string.
void clear ()
 Function clearing the object.
ObjectMetadataoperator+= (const ObjectMetadata &obj)
 Operator merging the contents of two objects.
ObjectMetadatamerge (const ObjectMetadata &obj)
 Function merging the contents of two objects.
const std::set< Variable > & variables () const
 Function for accessing all the variables of the D3PDObject.
template<class T>
StatusCode addVariable (const std::string &name, T *&ptr, const std::string &docstring="")
 Make the template implementation from IAddVariable visible.
template<class T, class U>
StatusCode addVariable (const std::string &name, T *&ptr, const std::string &docstring, const U &defval)
 Make the template implementation from IAddVariable visible.
template<class T>
StatusCode addDimensionedVariable (const std::string &name, T *&ptr, const std::string &dim, const std::string &docstring="")
 Add a variable to the tuple.
template<class T, class U>
StatusCode addDimensionedVariable (const std::string &name, T *&ptr, const std::string &dim, const std::string &docstring, const U &defval)
 Add a variable to the tuple.

Static Public Member Functions

static std::string objectName (const std::string &metaName)
 Get the D3PDObject's name from the name of the metadata object.

Static Public Attributes

static const size_t RANDOM_NAME_POSTFIX_LENGTH = 6
 Length of the random string appended to the object name.
static const char *const STRING_SEPARATOR = "@"
 Character separating parts of the object's metadata.
static const unsigned int SERIALIZER_VERSION = 1
 "Version number" of the serialized information

Protected Attributes

std::set< Variablem_variables
 The list of variables created by a D3PDObject.
std::string m_name
 Name of the D3PDObject that this object describes.
std::string m_prefix
 Prefix used by the D3PDObject.
bool m_container
 The D3PDObject describes a container.

Static Private Member Functions

static std::string genSuffix ATLAS_NOT_THREAD_SAFE (const std::string &name, size_t length)
 Generate a unique suffix for a metadata object name.

Detailed Description

D3PD variable metadata handling class.

   This class is used to store information in D3PD files about the
   variables that were created using a single D3PDObject in the job.
   The created metadata can be used to create D3PDReader classes
   later on, independent of the storage technology and the language
   used.
Author
Attila Krasznahorkay Attil.nosp@m.a.Kr.nosp@m.aszna.nosp@m.hork.nosp@m.ay@ce.nosp@m.rn.c.nosp@m.h

Definition at line 36 of file ObjectMetadata.h.

Constructor & Destructor Documentation

◆ ObjectMetadata() [1/2]

D3PD::ObjectMetadata::ObjectMetadata ( )

Default constructor.

Definition at line 61 of file ObjectMetadata.cxx.

62 : m_variables(), m_name( "" ), m_prefix( "" ),
63 m_container( false ) {
64
65 }
std::set< Variable > m_variables
The list of variables created by a D3PDObject.
bool m_container
The D3PDObject describes a container.
std::string m_name
Name of the D3PDObject that this object describes.
std::string m_prefix
Prefix used by the D3PDObject.

◆ ObjectMetadata() [2/2]

D3PD::ObjectMetadata::ObjectMetadata ( const ObjectMetadata & parent)

Copy constructor.

Definition at line 67 of file ObjectMetadata.cxx.

68 : m_variables( parent.m_variables ), m_name( parent.m_name ),
69 m_prefix( parent.m_prefix ), m_container( parent.m_container ) {
70
71 }

Member Function Documentation

◆ addDimensionedVariable() [1/3]

StatusCode D3PD::ObjectMetadata::addDimensionedVariable ( const std::string & name,
const std::type_info & ti,
void *& ptr,
const std::string & dim,
const std::string & docstring = "",
const void * defval = 0 )
virtual

The object doesn't support dimensioned variables at the moment, like most of the D3PDMaker code doesn't.

Still, we might implement something like this later on.

Implements D3PD::IAddVariable.

Definition at line 157 of file ObjectMetadata.cxx.

162 {
163
164 REPORT_MESSAGE_WITH_CONTEXT( MSG::FATAL, "ObjectMetadata" )
165 << "addDimensionedVariable(...) not implemented";
166
167 return StatusCode::FAILURE;
168 }
#define REPORT_MESSAGE_WITH_CONTEXT(LVL, CONTEXT_NAME)
Report a message, with an explicitly specified context name.

◆ addDimensionedVariable() [2/3]

template<class T, class U>
StatusCode D3PD::IAddVariable::addDimensionedVariable ( const std::string & name,
T *& ptr,
const std::string & dim,
const std::string & docstring,
const U & defval )

Add a variable to the tuple.

Parameters
nameThe name of the variable.
ptrPointer to the type of the variable. The pointer need not be initialized; the D3PD software will set the pointer prior to calling fill().
dimDimension for the variable. (Presently unimplemented!)
docstringDocumentation string for this variable.
defvalPointer to the default value to use for this variable. Only works for basic types.

If called from the constructor, the only effect is to clear ptr.

◆ addDimensionedVariable() [3/3]

template<class T>
StatusCode D3PD::IAddVariable::addDimensionedVariable ( const std::string & name,
T *& ptr,
const std::string & dim,
const std::string & docstring = "" )

Add a variable to the tuple.

Parameters
nameThe name of the variable.
ptrPointer to the type of the variable. The pointer need not be initialized; the D3PD software will set the pointer prior to calling fill().
dimDimension for the variable. (Presently unimplemented!)
docstringDocumentation string for this variable.

If called from the constructor, the only effect is to clear ptr.

◆ addVariable() [1/3]

StatusCode D3PD::ObjectMetadata::addVariable ( const std::string & name,
const std::type_info & ti,
void *& ptr,
const std::string & docstring = "",
const void * defval = 0 )
virtual

This function can be used to save the metadata about a D3PD variable.

Note that this class doesn't implement the full might of an ID3PD object, so no new object is created as a result of this function call. This means that it should always be followed by calling the addVariable(...) function of a proper IAddVariable object.

Implements D3PD::IAddVariable.

Reimplemented in D3PD::RootObjectMetadata.

Definition at line 109 of file ObjectMetadata.cxx.

113 {
114
115 // Check that the variable has the correct prefix:
116 if( m_prefix != "" &&
117 !name.starts_with( m_prefix) )
118 {
119 REPORT_MESSAGE_WITH_CONTEXT( MSG::ERROR, "ObjectMetadata" )
120 << "Specified variable name (" << name << ") doesn't have the "
121 << "expected prefix (" << m_prefix << ")";
122 return StatusCode::RECOVERABLE;
123 }
124
125 // Create a new variable:
127
128 // Remove the prefix from the variable name:
129 var.setName( name.substr( m_prefix.size(), name.npos ) );
130
131 // Check if it's already amongst the existing variables:
132 if( m_variables.find( var ) != m_variables.end() ) {
133 return StatusCode::SUCCESS;
134 }
135
136 // Set the type of the variable:
137 var.setType( System::typeinfoName( ti.name() ) );
138
139 // Set whether the variable is a primitive:
140 var.setPrimitive( isPrimitive( ti ) );
141
142 // Set the documentation string for the variable:
143 var.setDoc( docstring );
144
145 // Remember the variable:
146 m_variables.insert( std::move(var) );
147
148 return StatusCode::SUCCESS;
149 }
Internal class keeping track of a single variable.
const std::string & name() const
Get the name of the D3PDObject that this object describes.
bool isPrimitive(const std::string &type)
This function is used in the code generator to determine from a type name if it's a primitive type or...

◆ addVariable() [2/3]

template<class T, class U>
StatusCode D3PD::IAddVariable::addVariable ( const std::string & name,
T *& ptr,
const std::string & docstring,
const U & defval )

Make the template implementation from IAddVariable visible.

◆ addVariable() [3/3]

template<class T>
StatusCode D3PD::IAddVariable::addVariable ( const std::string & name,
T *& ptr,
const std::string & docstring = "" )

Make the template implementation from IAddVariable visible.

◆ ATLAS_NOT_THREAD_SAFE() [1/2]

std::string metadataName D3PD::ObjectMetadata::ATLAS_NOT_THREAD_SAFE ( ) const

Get the name for the metadata object that should be created.

◆ ATLAS_NOT_THREAD_SAFE() [2/2]

std::string genSuffix D3PD::ObjectMetadata::ATLAS_NOT_THREAD_SAFE ( const std::string & name,
size_t length )
staticprivate

Generate a unique suffix for a metadata object name.

Parameters
nameThe metadata object name.
lengthThe length of the suffix to add.

We used to do this by generating a random string. However, this is very bad for regression testing, as adding or removing one variable can then change the names of all subsequent variables. Instead, we generate the suffix based on a count of the number of times we've seen a particular name.

◆ clear()

void D3PD::ObjectMetadata::clear ( )

Function clearing the object.

Definition at line 352 of file ObjectMetadata.cxx.

352 {
353
354 // Clear/reset all the variables:
355 m_variables.clear();
356 m_name = "";
357 m_prefix = "";
358 m_container = false;
359
360 return;
361 }

◆ container()

bool D3PD::ObjectMetadata::container ( ) const

Get whether the D3PDObject describes a container or not.

Definition at line 230 of file ObjectMetadata.cxx.

230 {
231
232 return m_container;
233 }

◆ merge()

ObjectMetadata & D3PD::ObjectMetadata::merge ( const ObjectMetadata & obj)

Function merging the contents of two objects.

This merging function was implemented in the anticipation that it might come in handy later on.

But no guarantees...

Parameters
objThe object that should be merged with this one
Returns
A reference to the current object

Definition at line 375 of file ObjectMetadata.cxx.

375 {
376
377 // Only objects with the same "name" should be merged:
378 if( name() != obj.name() ) {
379 REPORT_MESSAGE_WITH_CONTEXT( MSG::WARNING, "ObjectMetadata" )
380 << "Can't merge object with name \"" << obj.name()
381 << "\" into another object with name \"" << name() << "\"";
382 return *this;
383 }
384
385 // Now merge all the variable definitions:
386 std::set< Variable >::const_iterator itr = obj.variables().begin();
387 std::set< Variable >::const_iterator end = obj.variables().end();
388 for( ; itr != end; ++itr ) {
389 m_variables.insert( *itr );
390 }
391
392 return *this;
393 }

◆ name()

const std::string & D3PD::ObjectMetadata::name ( ) const

Get the name of the D3PDObject that this object describes.

Definition at line 170 of file ObjectMetadata.cxx.

170 {
171
172 return m_name;
173 }

◆ objectName()

std::string D3PD::ObjectMetadata::objectName ( const std::string & metaName)
static

Get the D3PDObject's name from the name of the metadata object.

This function should be used by the client code to "decode" the name of the original D3PDObject from the name of the metadata object.

This way the client code doesn't have to know how this class works internally.

Parameters
metaNameThe name of the metadata object in the D3PD file
Returns
The name of the original D3PDObject that the metadata object describes

Definition at line 213 of file ObjectMetadata.cxx.

213 {
214
215 return metaName.substr( 0, metaName.size() -
217 }
static const size_t RANDOM_NAME_POSTFIX_LENGTH
Length of the random string appended to the object name.

◆ operator+=()

ObjectMetadata & D3PD::ObjectMetadata::operator+= ( const ObjectMetadata & obj)

Operator merging the contents of two objects.

Definition at line 363 of file ObjectMetadata.cxx.

363 {
364
365 return this->merge( obj );
366 }
ObjectMetadata & merge(const ObjectMetadata &obj)
Function merging the contents of two objects.

◆ operator<()

bool D3PD::ObjectMetadata::operator< ( const ObjectMetadata & rhs) const

Operator needed to use such objects in ordered STL containers.

Definition at line 91 of file ObjectMetadata.cxx.

91 {
92
93 if( prefix() != rhs.prefix() ) {
94 return ( prefix() < rhs.prefix() );
95 } else if( name() != rhs.name() ) {
96 return ( name() < rhs.name() );
97 } else {
98 return ( container() < rhs.container() );
99 }
100 }
const std::string & prefix() const
Get the prefix given to variables in this D3PDObject.
bool container() const
Get whether the D3PDObject describes a container or not.

◆ operator=()

ObjectMetadata & D3PD::ObjectMetadata::operator= ( const ObjectMetadata & parent)

Assignment operator.

Definition at line 73 of file ObjectMetadata.cxx.

74 {
75 if (&parent != this) {
76 m_variables = parent.m_variables;
77 m_name = parent.m_name;
78 m_prefix = parent.m_prefix;
79 m_container = parent.m_container;
80 }
81
82 return *this;
83 }

◆ operator==()

bool D3PD::ObjectMetadata::operator== ( const ObjectMetadata & rhs) const

Equality operator.

Definition at line 85 of file ObjectMetadata.cxx.

85 {
86
87 return ( ( name() == rhs.name() ) && ( prefix() == rhs.prefix() ) &&
88 ( container() == rhs.container() ) );
89 }

◆ prefix()

const std::string & D3PD::ObjectMetadata::prefix ( ) const

Get the prefix given to variables in this D3PDObject.

Definition at line 219 of file ObjectMetadata.cxx.

219 {
220
221 return m_prefix;
222 }

◆ read()

StatusCode D3PD::ObjectMetadata::read ( const std::string & data)

Function "de-serializing" the stored information from a string.

This function can be used to read in information from a serialized form.

Note that the function doesn't clear the contents of the object. So it can be used to merge information from multiple metadata objects / files.

Parameters
dataThe string that should be decoded
Returns
StatusCode::SUCCESS if the operation was successful, StatusCode::FAILURE otherwise

Definition at line 276 of file ObjectMetadata.cxx.

276 {
277
278 //
279 // Tokenize the string using Boost:
280 //
281 boost::char_separator< char > separator( STRING_SEPARATOR, "",
282 boost::keep_empty_tokens );
283 boost::tokenizer< boost::char_separator< char > > tokens( data,
284 separator );
285 boost::tokenizer< boost::char_separator< char > >::const_iterator itr = tokens.begin();
286 boost::tokenizer< boost::char_separator< char > >::const_iterator end = tokens.end();
287
288 // Check that we didn't reach the last token yet:
289 if( itr == end ) {
290 REPORT_MESSAGE_WITH_CONTEXT( MSG::FATAL, "ObjectMetadata" )
291 << "The received data can not be parsed successfully: "
292 << data;
293 return StatusCode::FAILURE;
294 }
295
296 // Check that the metadata was saved using the same version of the
297 // serializer code. Later on we might want to introduce backward
298 // compatibility, but for now this simple check should be enough.
299 if( atoi(itr->c_str()) != SERIALIZER_VERSION ) {
300 REPORT_MESSAGE_WITH_CONTEXT( MSG::FATAL, "ObjectMetadata" )
301 << "Version mismatch! The metadata was saved with a different "
302 << "serialization version (" << *itr << ") than the code used ("
303 << SERIALIZER_VERSION << ")";
304 return StatusCode::FAILURE;
305 }
306
307 ++itr;
308
309 // Check that we didn't reach the last token yet:
310 if( itr == end ) {
311 REPORT_MESSAGE_WITH_CONTEXT( MSG::FATAL, "ObjectMetadata" )
312 << "The received data can not be parsed successfully: "
313 << data;
314 return StatusCode::FAILURE;
315 }
316
317 // Extract the prefix from the metadata:
318 m_prefix = *itr; ++itr;
319
320 // Check that we didn't reach the last token yet:
321 if( itr == end ) {
322 REPORT_MESSAGE_WITH_CONTEXT( MSG::FATAL, "ObjectMetadata" )
323 << "The received data can not be parsed successfully: "
324 << data;
325 return StatusCode::FAILURE;
326 }
327
328 // Extract whether this is a container that the variables describe:
329 if( *itr == "0" ) {
330 m_container = false;
331 } else if( *itr == "1" ) {
332 m_container = true;
333 } else {
334 REPORT_MESSAGE_WITH_CONTEXT( MSG::FATAL, "ObjectMetadata" )
335 << "The received data can not be parsed successfully: "
336 << data;
337 return StatusCode::FAILURE;
338 }
339
340 ++itr;
341
342 // Finally, let's extract all the variable metadata:
343 for( ; itr != end; ++itr ) {
345 CHECK( var.read( *itr ) );
346 m_variables.insert( std::move(var) );
347 }
348
349 return StatusCode::SUCCESS;
350 }
#define CHECK(...)
Evaluate an expression and check for errors.
char data[hepevt_bytes_allocation_ATLAS]
Definition HepEvt.cxx:11
static const unsigned int SERIALIZER_VERSION
"Version number" of the serialized information
static const char *const STRING_SEPARATOR
Character separating parts of the object's metadata.
int atoi(std::string_view str)
Helper functions to unpack numbers decoded in string into integers and doubles The strings are requir...

◆ setContainer()

void D3PD::ObjectMetadata::setContainer ( bool container)

Set whether the D3PDObject describes a container or not.

Definition at line 235 of file ObjectMetadata.cxx.

235 {
236
238 return;
239 }

◆ setName()

void D3PD::ObjectMetadata::setName ( const std::string & name)

Set the name of the D3PDObject that this object describes.

Definition at line 175 of file ObjectMetadata.cxx.

175 {
176
177 m_name = name;
178 return;
179 }

◆ setPrefix()

void D3PD::ObjectMetadata::setPrefix ( const std::string & prefix)

Set the prefix given to variables in this D3PDObject.

Definition at line 224 of file ObjectMetadata.cxx.

224 {
225
227 return;
228 }

◆ toString()

std::string D3PD::ObjectMetadata::toString ( ) const

Function "serializing" the stored information into a string.

This function translates the contents of the entire object into a single string.

The different parts of the configuration string are separated by "@" character. (So variable names and variable descriptions can't have this character in them.)

Returns
The configuration of this object "serialized" into a string

Definition at line 249 of file ObjectMetadata.cxx.

249 {
250
251 // Save the simple part of the information
252 std::string result =
253 std::to_string( SERIALIZER_VERSION ) +
255 STRING_SEPARATOR + ( m_container ? "1" : "0" );
256
257 // Save the serialized version of all the variables:
258 std::set< Variable >::const_iterator itr = m_variables.begin();
259 std::set< Variable >::const_iterator end = m_variables.end();
260 for( ; itr != end; ++itr ) {
261 result += STRING_SEPARATOR + itr->toString();
262 }
263
264 return result;
265 }

◆ variables()

const std::set< ObjectMetadata::Variable > & D3PD::ObjectMetadata::variables ( ) const

Function for accessing all the variables of the D3PDObject.

Definition at line 577 of file ObjectMetadata.cxx.

577 {
578
579 return m_variables;
580 }

Member Data Documentation

◆ m_container

bool D3PD::ObjectMetadata::m_container
protected

The D3PDObject describes a container.

Definition at line 175 of file ObjectMetadata.h.

◆ m_name

std::string D3PD::ObjectMetadata::m_name
protected

Name of the D3PDObject that this object describes.

Definition at line 173 of file ObjectMetadata.h.

◆ m_prefix

std::string D3PD::ObjectMetadata::m_prefix
protected

Prefix used by the D3PDObject.

Definition at line 174 of file ObjectMetadata.h.

◆ m_variables

std::set< Variable > D3PD::ObjectMetadata::m_variables
protected

The list of variables created by a D3PDObject.

Definition at line 172 of file ObjectMetadata.h.

◆ RANDOM_NAME_POSTFIX_LENGTH

const size_t D3PD::ObjectMetadata::RANDOM_NAME_POSTFIX_LENGTH = 6
static

Length of the random string appended to the object name.

Definition at line 109 of file ObjectMetadata.h.

◆ SERIALIZER_VERSION

const unsigned int D3PD::ObjectMetadata::SERIALIZER_VERSION = 1
static

"Version number" of the serialized information

While I don't plan to change this class any time soon, we should be able to clearly decide later on if the format of the metadata serialization changed, which version was used to write the metadata found in a given file.

Definition at line 119 of file ObjectMetadata.h.

◆ STRING_SEPARATOR

const char *const D3PD::ObjectMetadata::STRING_SEPARATOR = "@"
static

Character separating parts of the object's metadata.

Definition at line 111 of file ObjectMetadata.h.


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