MsPASS C++ API  2.4.4.dev122+gb075c5a7b
Defines the C++ API for MsPASS
Loading...
Searching...
No Matches
mspass::algorithms::amplitudes::Metadata Class Reference

Type-safe metadata container used throughout MsPASS. More...

#include <Metadata.h>

Inheritance diagram for mspass::algorithms::amplitudes::Metadata:
Collaboration diagram for mspass::algorithms::amplitudes::Metadata:

Public Member Functions

 Metadata ()
 Metadata (std::ifstream &ifs, const std::string form=std::string("pf"))
 Metadata (const Metadata &mdold)
virtual ~Metadata ()
Metadata & operator= (const Metadata &mdold)
Metadata & operator+= (const Metadata &rhs) noexcept
const Metadata operator+ (const Metadata &other) const
double get_double (const std::string key) const override
int get_int (const std::string key) const override
long get_long (const std::string key) const
std::string get_string (const std::string key) const override
bool get_bool (const std::string key) const override
template<typename T>
T get (const std::string key) const
template<typename T>
T get (const char *key) const
 Generic get interface for C char array.
template<>
double get (const std::string key) const
template<>
int get (const std::string key) const
template<>
long get (const std::string key) const
template<>
bool get (const std::string key) const
template<>
float get (const std::string key) const
template<>
double get (const string key) const
template<>
int get (const string key) const
template<>
long get (const string key) const
template<>
bool get (const string key) const
template<>
float get (const string key) const
boost::any get_any (const std::string key) const
std::string type (const std::string key) const
template<typename T>
void put (const std::string key, T val) noexcept
template<typename T>
void put (const char *key, T val) noexcept
void put (const std::string key, const double val) override
void put (const std::string key, const int val) override
void put (const std::string key, const bool val) override
void put (const std::string key, const std::string val) override
void put (const char *key, const char *val)
void put (std::string key, const char *val)
void put_object (const std::string key, const pybind11::object val)
void put_int (const std::string key, const int val)
void put_string (const std::string key, const std::string val)
void put_bool (const std::string key, const bool val)
void put_double (const std::string key, const double val)
void put_long (const std::string key, const long val)
void append_chain (const std::string key, const std::string val, const std::string separator=std::string(":"))
std::set< std::string > modified () const
void clear_modified ()
 Mark all data as unmodified.
std::set< std::string > keys () const noexcept
bool is_defined (const std::string key) const noexcept
void erase (const std::string key)
std::size_t size () const noexcept
std::map< std::string, boost::any >::const_iterator begin () const noexcept
std::map< std::string, boost::any >::const_iterator end () const noexcept
void change_key (const std::string oldkey, const std::string newkey)
 Change the keyword to access an attribute.

Protected Attributes

std::map< std::string, boost::any > md
std::set< std::string > changed_or_set

Friends

pybind11::object serialize_metadata_py (const Metadata &md)
Metadata restore_serialized_metadata_py (const pybind11::object &sd)
std::ostringstream & operator<< (std::ostringstream &, const mspass::utility::Metadata &)

Detailed Description

Type-safe metadata container used throughout MsPASS.

Metadata stores named scalar values in a boost::any map and provides typed accessors, serialization helpers, and merge/update operations for seismic data headers and processing state.

Constructor & Destructor Documentation

◆ Metadata() [1/3]

Default constructor. Does nothing.

105{};

◆ Metadata() [2/3]

mspass::utility::Metadata::Metadata ( std::ifstream & ifs,
const std::string form = std::string("pf") )

Construct from a file.

This simple file based constructor assumes the file contains only a set lines with this format: key value type where type must be one of: real, integer, bool, or string. Note int is actually always promoted to a long. The optional format variable is there to allow alternative formats in the future.

Parameters
ifsifstream from which to read data
formoptional format specification. Currently only default of "text" is accepted.
Exceptions
MsPASSErrorcan be thrown for a variety of conditions.
289 {
290 try {
291 char linebuffer[256];
292 while (ifs.getline(linebuffer, 128)) {
293 string s1, s2, s3;
294 boost::any a;
295 stringstream ss(linebuffer);
296 ss >> s1;
297 ss >> s2;
298 ss >> s3;
299 if (s3 == "real") {
300 double dval;
301 dval = atof(s2.c_str());
302 a = dval;
303 md[s1] = a;
304 changed_or_set.insert(s1);
305 } else if (s3 == "integer") {
306 long ival;
307 ival = atol(s2.c_str());
308 a = ival;
309 md[s1] = a;
310 changed_or_set.insert(s1);
311 } else if (s3 == "string") {
312 string sval;
313 a = sval;
314 md[s1] = a;
315 changed_or_set.insert(s1);
316 } else if (s3 == "bool") {
317 bool bval;
318 if ((s2 == "TRUE") || (s2 == "true") || (s2 == "1"))
319 bval = true;
320 else
321 bval = false;
322 a = bval;
323 md[s1] = a;
324 changed_or_set.insert(s1);
325 } else {
326 stringstream sserr;
327 sserr
328 << "Metadata file constructor: Illegal type specification for key="
329 << s1 << " with a value field of " << s2 << endl
330 << "type specified as " << s3 << " is illegal. "
331 << "Must be on of the following: real, integer, bool, or string."
332 << endl;
333 throw MsPASSError(sserr.str(), ErrorSeverity::Invalid);
334 }
335 }
336 } catch (...) {
337 throw;
338 };
339}
std::map< std::string, boost::any > md
Definition Metadata.h:473
std::set< std::string > changed_or_set
Definition Metadata.h:475

◆ Metadata() [3/3]

mspass::utility::Metadata::Metadata ( const Metadata & mdold)

Standard copy constructor.

Parameters
mdoldparent object to be copied
341 : md(parent.md), changed_or_set(parent.changed_or_set) {}
std::map< std::string, boost::any > md
Definition Metadata.h:473
std::set< std::string > changed_or_set
Definition Metadata.h:475

◆ ~Metadata()

virtual mspass::utility::Metadata::~Metadata ( )
inlinevirtual

Destructor - has to be explicitly implemented and declared virtual for reasons found in textbooks and various web forums. A very subtle feature of C++ inheritance.

129{};

Member Function Documentation

◆ append_chain()

void mspass::utility::Metadata::append_chain ( const std::string key,
const std::string val,
const std::string separator = std::string(":") )

Create or append to a chained string.

A chain conceptually is identical to a list of string data. We implement it in Metadata because sometimes (e.g. MongoDB interaction and some constructs like the unix shell PATH variable) handling a full scale container like list<std::string> would be awkward. If more extensive capability like that is needed it would be better to add a class that inherits Metadata and does so. AntelopePf more or less does this, for example, handling Tbl sections. In any case, this usage is more for one word strings separated by a common separator: e.g. path=/usr/local/bin:/bin uses : as the separator. /usr/local/bin and /bin are the chain.

If the key related to the chain does not yet exist it is silently created. If it already exists and we append to it.

Parameters
keyis the key that defines the string.
valis the the new string to append to the chain
separatoris the string used for a separator (default ":")
Exceptions
MsPASSErrorwill be thrown if data is found in key and it is not of type string.
443 {
444 if (this->is_defined(key)) {
445 string typ = this->type(key);
446 if (typ.find("string") == string::npos)
447 throw MsPASSError("Metadata::append_chain: data for key=" + key +
448 " is not string type but " + typ +
449 "\nMust be string type to define a valid chain",
450 ErrorSeverity::Invalid);
451 string sval = this->get_string(key);
452 sval += separator;
453 sval += val;
454 this->put(key, sval);
455 } else {
456 this->put(key, val);
457 }
458 changed_or_set.insert(key);
459}
bool is_defined(const std::string key) const noexcept
Definition Metadata.cc:342
std::string get_string(const std::string key) const override
Definition Metadata.h:189
void put(const std::string key, T val) noexcept
Definition Metadata.h:277
std::string type(const std::string key) const
Definition Metadata.cc:525

◆ begin()

std::map< string, boost::any >::const_iterator mspass::utility::Metadata::begin ( ) const
noexcept

Return iterator to beginning of internal map container.

507 {
508 return md.begin();
509}

◆ change_key()

void mspass::utility::Metadata::change_key ( const std::string oldkey,
const std::string newkey )

Change the keyword to access an attribute.

Sometimes it is useful to change the key used to access a particular piece of data. Doing so, for example, is one way to implement an alias (alternative name) for something. The entry for the old key is copied to a entry accessible by the new key. The entry for old is then deleted. This avoids downstream inconsistencies a the cost of possible failures from the translation. This method always returns and will silently do nothing if old is not defined. If the new key is already defined, its content will be replaced by the old's.

Parameters
oldkeyis the key to search for to be changed
newkeyis the new key to use for the replacement.
651 {
652 map<string, boost::any>::iterator mdptr;
653 mdptr = md.find(oldkey);
654 /* We silently do nothing if old is not found */
655 if (mdptr != md.end()) {
656 md.insert_or_assign(newkey, mdptr->second);
657 md.erase(mdptr);
658 }
659}

◆ clear_modified()

Mark all data as unmodified.

There are situations where it is necessary to clear the data structure used to mark changed metadata. The best example know is when data objects interact with a database and try to do updates. Effort can be wasted in unnecessary updates if metadata are improperly marked as modified. This method clears the entire container that defines changed data.

407{ changed_or_set.clear(); };

◆ end()

std::map< string, boost::any >::const_iterator mspass::utility::Metadata::end ( ) const
noexcept

Return iterator to end of internal map container.

510 {
511 return md.end();
512}

◆ erase()

void mspass::utility::Metadata::erase ( const std::string key)

Overload for C string

Clear data associated with a particular key.

495 {
496 map<string, boost::any>::iterator iptr;
497 iptr = md.find(key);
498 if (iptr != md.end())
499 md.erase(iptr);
500 /* Also need to modify this set if the key is found there */
501 set<std::string>::iterator sptr;
502 sptr = changed_or_set.find(key);
503 if (sptr != changed_or_set.end())
504 changed_or_set.erase(sptr);
505}

◆ get() [1/12]

template<typename T>
T mspass::utility::Metadata::get ( const char * key) const
inline

Generic get interface for C char array.

This is a generic interface most useful for template procedures that need to get a Metadata component. Since this object only can contain simple types the type requested must be simple. Currently supports only int, long, short, double, float, and string. C char* is intentionally not supported. This is largely a wrapper on the string key version of this same generic function.

Parameters
keyis the name tag of desired component.
Exceptions
-will throw a MetadataGetError (child of MsPASSError) for type mismatch or in an overflow or underflow condition.
237 {
238 try {
239 T val;
240 val = get<T>(std::string(key));
241 return val;
242 } catch (...) {
243 throw;
244 };
245 }
T get(const std::string key) const
Definition Metadata.h:477

◆ get() [2/12]

template<>
float mspass::utility::Metadata::get ( const std::string key) const

Specialization of Metadata::get for float values.

◆ get() [3/12]

template<>
bool mspass::utility::Metadata::get ( const std::string key) const

Specialization of Metadata::get for bool values.

◆ get() [4/12]

template<>
long mspass::utility::Metadata::get ( const std::string key) const

Specialization of Metadata::get for long values.

◆ get() [5/12]

template<>
int mspass::utility::Metadata::get ( const std::string key) const

Specialization of Metadata::get for int values.

◆ get() [6/12]

template<>
double mspass::utility::Metadata::get ( const std::string key) const

Specialization of Metadata::get for double values.

◆ get() [7/12]

template<typename T>
T mspass::utility::Metadata::get ( const std::string key) const

Generic get interface.

This is a generic interface most useful for template procedures that need to get a Metadata component. Since this object only can contain simple types the type requested must be simple. Currently supports only int, long, short, double, float, and string. C char* is intentionally not supported. Calls to anything but the supported types will throw an exception.

Parameters
keyis the name tag of desired component.
Exceptions
-will throw a MetadataGetError (child of MsPASSError) for type mismatch or in an overflow or underflow condition.
477 {
478 T result;
479 std::map<std::string, boost::any>::const_iterator iptr;
480 iptr = md.find(key);
481 if (iptr == md.end()) {
482 throw MetadataGetError(key, typeid(T).name());
483 }
484 boost::any aval = iptr->second;
485 try {
486 result = boost::any_cast<T>(aval);
487 } catch (boost::bad_any_cast &err) {
488 const std::type_info &ti = aval.type();
489 throw MetadataGetError(err.what(), key, typeid(T).name(), ti.name());
490 };
491 return result;
492}

◆ get() [8/12]

template<>
float mspass::utility::Metadata::get ( const string key) const

Specialization returning numeric metadata as a float when in range.

425 {
426 map<string, boost::any>::const_iterator iptr;
427 iptr = md.find(key);
428 if (iptr == md.end())
429 throw MetadataGetError(key, typeid(float).name());
430 if (iptr->second.type() == typeid(float))
431 return boost::any_cast<float>(iptr->second);
432 double val(0.0);
433 if (any_to_double(iptr->second, val) && isfinite(val) &&
434 val >= -static_cast<double>(numeric_limits<float>::max()) &&
435 val <= static_cast<double>(numeric_limits<float>::max()))
436 return static_cast<float>(val);
437 throw MetadataGetError(
438 key, typeid(float).name(), iptr->second.type().name(),
439 "Metadata value is not numeric or is outside float range");
440}

◆ get() [9/12]

template<>
bool mspass::utility::Metadata::get ( const string key) const

Specialization returning boolean metadata as a bool.

420 {
421 return this->get_bool(key);
422}
bool get_bool(const std::string key) const override
Definition Metadata.cc:392

◆ get() [10/12]

template<>
long mspass::utility::Metadata::get ( const string key) const

Specialization returning integer-valued metadata as a long.

415 {
416 return this->get_long(key);
417}
long get_long(const std::string key) const
Definition Metadata.cc:379

◆ get() [11/12]

template<>
int mspass::utility::Metadata::get ( const string key) const

Specialization returning integer-valued metadata as an int.

410 {
411 return this->get_int(key);
412}
int get_int(const std::string key) const override
Definition Metadata.cc:364

◆ get() [12/12]

template<>
double mspass::utility::Metadata::get ( const string key) const

Specialization returning numeric metadata as a double.

405 {
406 return this->get_double(key);
407}
double get_double(const std::string key) const override
Definition Metadata.cc:352

◆ get_any()

boost::any mspass::utility::Metadata::get_any ( const std::string key) const
inline

Get the boost::any container from the Metadata object.

This method is mostly for Python bindings so that a generic get method can work in Python.

Parameters
keyis the name tag of desired component.
Exceptions
-MetadataGetError if requested parameter is not found.
256 {
257 std::map<std::string, boost::any>::const_iterator iptr;
258 iptr = md.find(key);
259 if (iptr == md.end()) {
260 throw MetadataGetError(key, typeid(boost::any).name());
261 }
262 return iptr->second;
263 };

◆ get_bool()

bool mspass::utility::Metadata::get_bool ( const std::string key) const
overridevirtual

Get a boolean parameter from the Metadata object.

Exceptions
MetadataGetErrorif requested parameter is not found or there is a type mismatch.
Parameters
keykeyword associated with requested metadata member.

Implements mspass::utility::BasicMetadata.

392 {
393 map<string, boost::any>::const_iterator iptr;
394 iptr = md.find(key);
395 if (iptr == md.end())
396 throw MetadataGetError(key, typeid(bool).name());
397 bool val(false);
398 if (any_to_bool(iptr->second, val))
399 return val;
400 throw MetadataGetError(key, typeid(bool).name(), iptr->second.type().name(),
401 "Metadata value is not boolean");
402}

◆ get_double()

double mspass::utility::Metadata::get_double ( const std::string key) const
overridevirtual

Get a real number from the Metadata object.

Exceptions
MetadataGetErrorif requested parameter is not found or there is a type mismatch.
Parameters
keykeyword associated with requested metadata member.

Implements mspass::utility::BasicMetadata.

352 {
353 map<string, boost::any>::const_iterator iptr;
354 iptr = md.find(key);
355 if (iptr == md.end())
356 throw MetadataGetError(key, typeid(double).name());
357 double val(0.0);
358 if (any_to_double(iptr->second, val))
359 return val;
360 throw MetadataGetError(key, typeid(double).name(), iptr->second.type().name(),
361 "Metadata value is not numeric");
362}

◆ get_int()

int mspass::utility::Metadata::get_int ( const std::string key) const
overridevirtual

Get an integer from the Metadata object.

Exceptions
MetadataGetErrorif requested parameter is not found or there is a type mismatch.
Parameters
keykeyword associated with requested metadata member.

Implements mspass::utility::BasicMetadata.

364 {
365 map<string, boost::any>::const_iterator iptr;
366 iptr = md.find(key);
367 if (iptr == md.end())
368 throw MetadataGetError(key, typeid(int).name());
369 long val(0);
370 if (any_to_long(iptr->second, val) &&
371 val >= static_cast<long>(numeric_limits<int>::min()) &&
372 val <= static_cast<long>(numeric_limits<int>::max()))
373 return static_cast<int>(val);
374 throw MetadataGetError(
375 key, typeid(int).name(), iptr->second.type().name(),
376 "Metadata value is not integer-valued or is outside int range");
377}

◆ get_long()

long mspass::utility::Metadata::get_long ( const std::string key) const

Get a long integer from the Metadata object.

Exceptions
MetadataGetErrorif requested parameter is not found or there is a type mismatch.
Parameters
keykeyword associated with requested metadata member.
379 {
380 map<string, boost::any>::const_iterator iptr;
381 iptr = md.find(key);
382 if (iptr == md.end())
383 throw MetadataGetError(key, typeid(long).name());
384 long val(0);
385 if (any_to_long(iptr->second, val))
386 return val;
387 throw MetadataGetError(
388 key, typeid(long).name(), iptr->second.type().name(),
389 "Metadata value is not integer-valued or is outside long range");
390}

◆ get_string()

std::string mspass::utility::Metadata::get_string ( const std::string key) const
inlineoverridevirtual

Get a string from the Metadata object.

Note the string in this case can be quite large. If the string was parsed from an Antelope Pf nested Tbl and Arrs can be extracted this way and parsed with pf routines.

Exceptions
MetadataGetErrorif requested parameter is not found or there is a type mismatch.
Parameters
keykeyword associated with requested metadata member.

Implements mspass::utility::BasicMetadata.

189 {
190 try {
191 std::string val;
192 val = get<std::string>(key);
193 return val;
194 } catch (...) {
195 throw;
196 };
197 };

◆ is_defined()

bool mspass::utility::Metadata::is_defined ( const std::string key) const
noexcept

Test if a key has an associated value. Returns true if a value is defined.

342 {
343 map<string, boost::any>::const_iterator mptr;
344 mptr = md.find(key);
345 if (mptr != md.end()) {
346 return true;
347 } else {
348 return false;
349 }
350}

◆ keys()

set< string > mspass::utility::Metadata::keys ( ) const
noexcept

Return all keys without any type information.

486 {
487 set<string> result;
488 map<string, boost::any>::const_iterator mptr;
489 for (mptr = md.begin(); mptr != md.end(); ++mptr) {
490 string key(mptr->first);
491 result.insert(key);
492 }
493 return result;
494}

◆ modified()

std::set< std::string > mspass::utility::Metadata::modified ( ) const
inline

Return the keys of all altered Metadata values.

396{ return changed_or_set; };

◆ operator+()

const Metadata mspass::utility::Metadata::operator+ ( const Metadata & other) const

Add two Metadata objects. Uses operator+=

481 {
482 Metadata result(*this);
483 result += other;
484 return result;
485}
Metadata()
Definition Metadata.h:105

◆ operator+=()

Metadata & mspass::utility::Metadata::operator+= ( const Metadata & rhs)
noexcept

Append additional metadata with replacement.

A plus operator implies addition, but this overloading does something very different. A simple way to describe the effect is that on completion the left hand side Metadata object will contain a duplicate of the right hand side plus any attributes in the rhs that were not present on the lhs. Another way to clarify this is to describe the algorithm. We take each attribute on the right and search for it in the lhs. If it is not in the lhs it will be added. If it is there already, the rhs value will replace the old value on the lhs. This is most useful when an algorithm creates a new set of attributes that we want to use in downstream processing but retain all the other attributes.

Parameters
rhsis the new metadata to be insert/replace on the lhs.
468 {
469 if (this != (&rhs)) {
470 /* We depend here upon the map container replacing values associated with
471 existing keys. We mark all entries changes anyway. This is a vastly
472 simpler algorithm than the old SEISPP::Metadata. */
473 map<string, boost::any>::const_iterator rhsptr;
474 for (rhsptr = rhs.md.begin(); rhsptr != rhs.md.end(); ++rhsptr) {
475 md[rhsptr->first] = rhsptr->second;
476 changed_or_set.insert(rhsptr->first);
477 }
478 }
479 return *this;
480}

◆ operator=()

Metadata & mspass::utility::Metadata::operator= ( const Metadata & mdold)

Standard assignment operator.

Parameters
mdoldparent object to copy
460 {
461 if (this != (&parent)) {
462 md = parent.md;
463 changed_or_set = parent.changed_or_set;
464 }
465 return *this;
466}

◆ put() [1/8]

void mspass::utility::Metadata::put ( const char * key,
const char * val )
inline

Put a C string literal. Requires special handling because it is not copy constructable (see warning in boost docs: https://theboostcpplibraries.com/boost.any

331 {
332 std::string sval(val);
333 this->put<std::string>(key, val);
334 }

◆ put() [2/8]

template<typename T>
void mspass::utility::Metadata::put ( const char * key,
T val )
inlinenoexcept

Store a value using a C string key.

Parameters
keynull-terminated metadata key to create or replace.
valvalue copied into the internal boost::any map.
287 {
288 /* could do this as put(string(key),val) but this is so trivial duplicating
289 the code for the string method is more efficient than an added function
290 call.*/
291 boost::any aval = val;
292 md[std::string(key)] = aval;
293 changed_or_set.insert(std::string(key));
294 }

◆ put() [3/8]

void mspass::utility::Metadata::put ( const std::string key,
const bool val )
inlineoverridevirtual

Store a boolean attribute.

Parameters
keymetadata key to create or replace.
valvalue to store.

Implements mspass::utility::BasicMetadata.

316 {
317 this->put<bool>(key, val);
318 };

◆ put() [4/8]

void mspass::utility::Metadata::put ( const std::string key,
const double val )
inlineoverridevirtual

Store a double-valued attribute.

Parameters
keymetadata key to create or replace.
valvalue to store.

Implements mspass::utility::BasicMetadata.

300 {
301 this->put<double>(key, val);
302 };

◆ put() [5/8]

void mspass::utility::Metadata::put ( const std::string key,
const int val )
inlineoverridevirtual

Store an integer-valued attribute.

Parameters
keymetadata key to create or replace.
valvalue to store.

Implements mspass::utility::BasicMetadata.

308 {
309 this->put<int>(key, val);
310 };

◆ put() [6/8]

void mspass::utility::Metadata::put ( const std::string key,
const std::string val )
inlineoverridevirtual

Store a string-valued attribute.

Parameters
keymetadata key to create or replace.
valvalue to store.

Implements mspass::utility::BasicMetadata.

324 {
325 this->put<std::string>(key, val);
326 };

◆ put() [7/8]

template<typename T>
void mspass::utility::Metadata::put ( const std::string key,
T val )
inlinenoexcept

Store a value of any boost::any-compatible C++ type.

The key is marked as changed so database update logic can detect it.

Parameters
keymetadata key to create or replace.
valvalue copied into the internal boost::any map.
277 {
278 boost::any aval = val;
279 md[key] = aval;
280 changed_or_set.insert(key);
281 }

◆ put() [8/8]

void mspass::utility::Metadata::put ( std::string key,
const char * val )
inline

Store a C string value using a std::string key.

Parameters
keymetadata key to create or replace.
valnull-terminated string value to store.
340{ this->put(key.c_str(), val); }

◆ put_bool()

void mspass::utility::Metadata::put_bool ( const std::string key,
const bool val )
inline

Store a bool through the Python-friendly typed wrapper.

358 {
359 this->put<bool>(key, val);
360 };

◆ put_double()

void mspass::utility::Metadata::put_double ( const std::string key,
const double val )
inline

Store a double through the Python-friendly typed wrapper.

362 {
363 this->put<double>(key, val);
364 };

◆ put_int()

void mspass::utility::Metadata::put_int ( const std::string key,
const int val )
inline

Store an int through the Python-friendly typed wrapper.

350 {
351 this->put<int>(key, val);
352 };

◆ put_long()

void mspass::utility::Metadata::put_long ( const std::string key,
const long val )
inline

Store a long through the Python-friendly typed wrapper.

366 {
367 this->put<long>(key, val);
368 };

◆ put_object()

void mspass::utility::Metadata::put_object ( const std::string key,
const pybind11::object val )
inline

Store a pybind11 object for Python-facing metadata.

Parameters
keymetadata key to create or replace.
valPython object to store in the metadata map.
346 {
347 this->put<pybind11::object>(key, val);
348 }

◆ put_string()

void mspass::utility::Metadata::put_string ( const std::string key,
const std::string val )
inline

Store a string through the Python-friendly typed wrapper.

354 {
355 this->put<std::string>(key, val);
356 };

◆ size()

std::size_t mspass::utility::Metadata::size ( ) const
noexcept

Overload for C string

Return the size of the internal map container.

506{ return md.size(); }

◆ type()

std::string mspass::utility::Metadata::type ( const std::string key) const

Return the stored C++ type name for a metadata value.

Parameters
keymetadata key to inspect.
Returns
demangled type name of the value stored for key.
Exceptions
MetadataGetErrorif key is undefined.
525 {
526 try {
527 boost::any a = this->get_any(key);
528 return demangled_name(a);
529 } catch (...) {
530 throw;
531 };
532}
boost::any get_any(const std::string key) const
Definition Metadata.h:256

◆ operator<<

std::ostringstream & operator<< ( std::ostringstream & ,
const mspass::utility::Metadata &  )
friend

Standard operator for overloading output to a stringstream

◆ restore_serialized_metadata_py

Metadata restore_serialized_metadata_py ( const pybind11::object & sd)
friend

Unpack serialized Metadata.

This function is the inverse of the serialize function. It recreates a Metadata object serialized previously with the serialize function.

Parameters
sdis the serialized data to be unpacked
Returns
Metadata derived from sd
631 {
632 pybind11::gil_scoped_acquire acquire;
633 try {
634 pybind11::module pickle = pybind11::module::import("pickle");
635 pybind11::object loads = pickle.attr("loads");
636 pybind11::tuple md_dump = loads(s);
637 pybind11::dict md_dict = md_dump[0];
638 pybind11::object md_py = pybind11::module_::import("mspasspy.ccore.utility")
639 .attr("Metadata")(md_dict);
640 Metadata md = md_py.cast<Metadata>();
641 md.changed_or_set = md_dump[1].cast<std::set<std::string>>();
642 pybind11::gil_scoped_release release;
643 return md;
644 } catch (...) {
645 pybind11::gil_scoped_release release;
646 throw;
647 };
648}

◆ serialize_metadata_py

pybind11::object serialize_metadata_py ( const Metadata & md)
friend

Serialize Metadata to a python bytes object. This function is needed to support pickle in the python interface. It cast the C++ object to a Python dict and calls pickle against that dict directly to generate a Python bytes object. This may not be the most elegant approach, but it should be bombproof.

Parameters
mdis the Metadata object to be serialized
Returns
pickle serialized data object.
614 {
615 pybind11::gil_scoped_acquire acquire;
616 try {
617 pybind11::dict md_dict = pybind11::cast(md);
618 pybind11::set changed_or_set = pybind11::cast(md.changed_or_set);
619 pybind11::module pickle = pybind11::module::import("pickle");
620 pybind11::object dumps = pickle.attr("dumps");
621 pybind11::object md_dump =
622 dumps(pybind11::make_tuple(md_dict, changed_or_set));
623 pybind11::gil_scoped_release release;
624 return md_dump;
625 } catch (...) {
626 pybind11::gil_scoped_release release;
627 throw;
628 };
629}

Member Data Documentation

◆ changed_or_set

std::set<std::string> mspass::utility::Metadata::changed_or_set
protected

Keys that have been created or changed since the last clear_modified.

◆ md

std::map<std::string, boost::any> mspass::utility::Metadata::md
protected

Map from metadata key to value stored in a boost::any container.


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