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.
ObjectMetadata & operator= (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, std::string_view 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, std::string_view 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.
ObjectMetadata & operator+= (const ObjectMetadata &obj)
 Operator merging the contents of two objects.
ObjectMetadata & merge (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, std::string_view docstring="")
 Make the template implementation from IAddVariable visible.
template<class T, class U>
StatusCode addVariable (const std::string &name, T *&ptr, std::string_view 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, std::string_view docstring="")
 Add a variable to the tuple.
template<class T, class U>
StatusCode addDimensionedVariable (const std::string &name, T *&ptr, const std::string &dim, std::string_view 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< Variable > m_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 62 of file ObjectMetadata.cxx.

63 : m_variables(), m_name( "" ), m_prefix( "" ),
64 m_container( false ) {
65
66 }
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 68 of file ObjectMetadata.cxx.

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

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,
std::string_view 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 158 of file ObjectMetadata.cxx.

163 {
164
165 REPORT_MESSAGE_WITH_CONTEXT( MSG::FATAL, "ObjectMetadata" )
166 << "addDimensionedVariable(...) not implemented";
167
168 return StatusCode::FAILURE;
169 }
#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,
std::string_view 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,
std::string_view 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,
std::string_view 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.

Definition at line 110 of file ObjectMetadata.cxx.

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

◆ addVariable() [2/3]

template<class T, class U>
StatusCode D3PD::IAddVariable::addVariable ( const std::string & name,
T *& ptr,
std::string_view 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,
std::string_view 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 353 of file ObjectMetadata.cxx.

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

◆ container()

bool D3PD::ObjectMetadata::container ( ) const

Get whether the D3PDObject describes a container or not.

Definition at line 231 of file ObjectMetadata.cxx.

231 {
232
233 return m_container;
234 }

◆ 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 376 of file ObjectMetadata.cxx.

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

◆ name()

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

Get the name of the D3PDObject that this object describes.

Definition at line 171 of file ObjectMetadata.cxx.

171 {
172
173 return m_name;
174 }

◆ 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 214 of file ObjectMetadata.cxx.

214 {
215
216 return metaName.substr( 0, metaName.size() -
218 }
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 364 of file ObjectMetadata.cxx.

364 {
365
366 return this->merge( obj );
367 }
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 92 of file ObjectMetadata.cxx.

92 {
93
94 if( prefix() != rhs.prefix() ) {
95 return ( prefix() < rhs.prefix() );
96 } else if( name() != rhs.name() ) {
97 return ( name() < rhs.name() );
98 } else {
99 return ( container() < rhs.container() );
100 }
101 }
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 74 of file ObjectMetadata.cxx.

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

◆ operator==()

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

Equality operator.

Definition at line 86 of file ObjectMetadata.cxx.

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

◆ prefix()

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

Get the prefix given to variables in this D3PDObject.

Definition at line 220 of file ObjectMetadata.cxx.

220 {
221
222 return m_prefix;
223 }

◆ 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 277 of file ObjectMetadata.cxx.

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

236 {
237
239 return;
240 }

◆ setName()

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

Set the name of the D3PDObject that this object describes.

Definition at line 176 of file ObjectMetadata.cxx.

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

◆ setPrefix()

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

Set the prefix given to variables in this D3PDObject.

Definition at line 225 of file ObjectMetadata.cxx.

225 {
226
228 return;
229 }

◆ 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 250 of file ObjectMetadata.cxx.

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

◆ variables()

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

Function for accessing all the variables of the D3PDObject.

Definition at line 578 of file ObjectMetadata.cxx.

578 {
579
580 return m_variables;
581 }

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: