|
MsPASS C++ API
2.4.4.dev122+gb075c5a7b
Defines the C++ API for MsPASS
|
Type-safe metadata container used throughout MsPASS. More...
#include <Metadata.h>


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. | |
| 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. | |
| 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 |
| Public Member Functions inherited from mspass::utility::BasicMetadata | |
| virtual | ~BasicMetadata () |
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 &) |
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.
|
inline |
Default constructor. Does nothing.
| 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.
| ifs | ifstream from which to read data |
| form | optional format specification. Currently only default of "text" is accepted. |
| MsPASSError | can be thrown for a variety of conditions. |
References changed_or_set, and md.
| mspass::utility::Metadata::Metadata | ( | const Metadata & | mdold | ) |
Standard copy constructor.
| mdold | parent object to be copied |
References changed_or_set, md, and 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.
| 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.
| key | is the key that defines the string. |
| val | is the the new string to append to the chain |
| separator | is the string used for a separator (default ":") |
| MsPASSError | will be thrown if data is found in key and it is not of type string. |
References changed_or_set, get_string(), is_defined(), put(), and type().
|
noexcept |
| 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.
| oldkey | is the key to search for to be changed |
| newkey | is the new key to use for the replacement. |
References md.
|
inline |
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.
References changed_or_set.
|
noexcept |
| void mspass::utility::Metadata::erase | ( | const std::string | key | ) |
Overload for C string
Clear data associated with a particular key.
References changed_or_set, and md.
|
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.
| key | is the name tag of desired component. |
| - | will throw a MetadataGetError (child of MsPASSError) for type mismatch or in an overflow or underflow condition. |
References get().
| float mspass::utility::Metadata::get | ( | const std::string | key | ) | const |
Specialization of Metadata::get for float values.
| bool mspass::utility::Metadata::get | ( | const std::string | key | ) | const |
Specialization of Metadata::get for bool values.
| long mspass::utility::Metadata::get | ( | const std::string | key | ) | const |
Specialization of Metadata::get for long values.
| int mspass::utility::Metadata::get | ( | const std::string | key | ) | const |
Specialization of Metadata::get for int values.
| double mspass::utility::Metadata::get | ( | const std::string | key | ) | const |
Specialization of Metadata::get for double values.
| 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.
| key | is the name tag of desired component. |
| - | will throw a MetadataGetError (child of MsPASSError) for type mismatch or in an overflow or underflow condition. |
References get(), md, and Metadata().
| float mspass::utility::Metadata::get | ( | const string | key | ) | const |
Specialization returning numeric metadata as a float when in range.
References md.
| bool mspass::utility::Metadata::get | ( | const string | key | ) | const |
Specialization returning boolean metadata as a bool.
References get_bool().
| long mspass::utility::Metadata::get | ( | const string | key | ) | const |
Specialization returning integer-valued metadata as a long.
References get_long().
| int mspass::utility::Metadata::get | ( | const string | key | ) | const |
Specialization returning integer-valued metadata as an int.
References get_int().
| double mspass::utility::Metadata::get | ( | const string | key | ) | const |
Specialization returning numeric metadata as a double.
References get_double().
|
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.
| key | is the name tag of desired component. |
| - | MetadataGetError if requested parameter is not found. |
References md.
|
overridevirtual |
Get a boolean parameter from the Metadata object.
| MetadataGetError | if requested parameter is not found or there is a type mismatch. |
| key | keyword associated with requested metadata member. |
Implements mspass::utility::BasicMetadata.
References md.
|
overridevirtual |
Get a real number from the Metadata object.
| MetadataGetError | if requested parameter is not found or there is a type mismatch. |
| key | keyword associated with requested metadata member. |
Implements mspass::utility::BasicMetadata.
References md.
|
overridevirtual |
Get an integer from the Metadata object.
| MetadataGetError | if requested parameter is not found or there is a type mismatch. |
| key | keyword associated with requested metadata member. |
Implements mspass::utility::BasicMetadata.
References md.
| long mspass::utility::Metadata::get_long | ( | const std::string | key | ) | const |
Get a long integer from the Metadata object.
| MetadataGetError | if requested parameter is not found or there is a type mismatch. |
| key | keyword associated with requested metadata member. |
References md.
|
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.
| MetadataGetError | if requested parameter is not found or there is a type mismatch. |
| key | keyword associated with requested metadata member. |
Implements mspass::utility::BasicMetadata.
References get().
|
noexcept |
|
noexcept |
|
inline |
Add two Metadata objects. Uses operator+=
References Metadata().
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.
| rhs | is the new metadata to be insert/replace on the lhs. |
References changed_or_set, md, and Metadata().
Standard assignment operator.
| mdold | parent object to copy |
References changed_or_set, md, and Metadata().
|
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
References put().
|
inlinenoexcept |
Store a value using a C string key.
| key | null-terminated metadata key to create or replace. |
| val | value copied into the internal boost::any map. |
References changed_or_set, and md.
|
inlineoverridevirtual |
Store a boolean attribute.
| key | metadata key to create or replace. |
| val | value to store. |
Implements mspass::utility::BasicMetadata.
References put().
|
inlineoverridevirtual |
Store a double-valued attribute.
| key | metadata key to create or replace. |
| val | value to store. |
Implements mspass::utility::BasicMetadata.
References put().
|
inlineoverridevirtual |
Store an integer-valued attribute.
| key | metadata key to create or replace. |
| val | value to store. |
Implements mspass::utility::BasicMetadata.
References put().
|
inlineoverridevirtual |
Store a string-valued attribute.
| key | metadata key to create or replace. |
| val | value to store. |
Implements mspass::utility::BasicMetadata.
References put().
|
inlinenoexcept |
Store a value of any boost::any-compatible C++ type.
The key is marked as changed so database update logic can detect it.
| key | metadata key to create or replace. |
| val | value copied into the internal boost::any map. |
References changed_or_set, and md.
|
inline |
|
inline |
|
inline |
Store a double through the Python-friendly typed wrapper.
References put().
|
inline |
|
inline |
|
inline |
Store a pybind11 object for Python-facing metadata.
| key | metadata key to create or replace. |
| val | Python object to store in the metadata map. |
References put().
|
inline |
Store a string through the Python-friendly typed wrapper.
References put().
|
noexcept |
| std::string mspass::utility::Metadata::type | ( | const std::string | key | ) | const |
Return the stored C++ type name for a metadata value.
| key | metadata key to inspect. |
| MetadataGetError | if key is undefined. |
References get_any().
|
friend |
Standard operator for overloading output to a stringstream
References Metadata().
|
friend |
Unpack serialized Metadata.
This function is the inverse of the serialize function. It recreates a Metadata object serialized previously with the serialize function.
| sd | is the serialized data to be unpacked |
References md, Metadata(), and restore_serialized_metadata_py.
|
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.
| md | is the Metadata object to be serialized |
References changed_or_set, md, Metadata(), and serialize_metadata_py.
|
protected |
Keys that have been created or changed since the last clear_modified.
|
protected |
Map from metadata key to value stored in a boost::any container.