|
Parsnip
parsing library
|
Generic data type. More...
#include <parsnip.h>
Data Structures | |
| union | data_t |
| struct | dictionary_t |
| struct | iterator |
| An iterator used to walk items in Data's lists. More... | |
| struct | list_t |
Public Types | |
| enum class | Type { Null , Dictionary , List , String , FlexibleString , Integer , Real , Boolean } |
| Types that may be stored in the data. More... | |
| using | StringType = std::string |
| using | ListType = std::vector< Data > |
| using | size_type = ListType::size_type |
Public Member Functions | |
| Data (std::nullptr_t null=nullptr) | |
| Default constructor. | |
| Data (const char *value, bool type_certainty=true) | |
| String constructor. | |
| Data (const std::string &value, bool type_certainty=true) | |
| String constructor, accepting C++ string. | |
| template<typename T , typename = typename std::enable_if<!std::is_same<T, bool>::value && std::is_integral<T>::value, int>::type> | |
| Data (T value) noexcept | |
| Integer constructor. | |
| Data (double value) noexcept | |
| Floating point constructor. | |
| Data (bool value) noexcept | |
| Boolean constructor. | |
| template<typename... T> | |
| Data (dictionary_t, T... params) | |
| Dictionary constructor. | |
| template<typename... T> | |
| Data (list_t, T... params) | |
| List constructor. | |
| Data (Data &&from) noexcept | |
| Move construction. | |
| Data & | operator= (Data &&from) noexcept |
| Move assignment. | |
| Data (const Data &from) | |
| Copy constructor. | |
| Data & | operator= (const Data &from) |
| Copy assignment. | |
| bool | operator== (const Data &compare) const |
| Compare two structures. | |
| bool | operator!= (const Data &compare) const |
| const Data & | makeFlexible () const |
| IF a data item is a string, change it to a flexible string. | |
| bool | isNull (void) const noexcept |
| Check if data contains a value. | |
| const StringType & | asString () const |
| Retrieve string value. | |
| long | asLong (int base=0) const |
| Return value of data as a long integer. | |
| int | asInteger (int base=0) const |
| Return value of data as an integer. | |
| double | asDouble () const |
| Return value of data as a double. If the data is a string, parse it as a float. | |
| bool | asBoolean () const |
| Return value of data as a boolean. If the data is a string, translate "true" and "false". | |
| ListType | asList () const |
| Return data as a list. | |
| template<typename DataType , typename = typename std::enable_if<std::is_same<DataType, std::string>::value>::type> | |
| const std::string & | as () const |
| Extract data as a string (templated version). | |
| template<typename DataType , typename = typename std::enable_if<!std::is_same<DataType, bool>::value && std::is_integral<DataType>::value>::type> | |
| DataType | as (int base=0) const |
| Extract data as an integer type, except boolean. | |
| template<typename DataType , typename = typename std::enable_if<std::is_floating_point<DataType>::value>::type> | |
| DataType | as () const |
| Extract data as a floating point type. | |
| template<typename DataType , typename = typename std::enable_if<std::is_same<DataType, bool>::value>::type> | |
| bool | as () const |
| Extract data as a boolean. | |
| template<typename DataType , typename = typename std::enable_if<std::is_same<DataType, Data>::value>::type> | |
| const Data & | as () const |
| Retrieve data as the object itself. | |
| template<typename Container , typename = typename std::enable_if<std::is_class<Container>::value && !std::is_same<Container, Data>::value && !std::is_same<Container, std::string>::value>::type> | |
| Container | as (std::nullptr_t null=nullptr) const |
| Return contents of a list as a specific datatype. | |
| template<typename Datatype > | |
| Datatype | getOr (const std::string &name, const Datatype &defvalue) const |
| Extract a value if a member exists, otherwise use a default. | |
| StringType | getOr (const std::string &name, const char *defvalue) const |
| Specialization of 'getOr' that accepts a const char * default; otherwise, overload resolution chooses implicit instantiation of a Data and uses that as a default rather than implicitly instantiating the string type. | |
| Data | getOr (const std::string &name, const Data &defvalue) const |
Specialization of getOr for Data. | |
| template<typename Datatype > | |
| std::vector< Datatype > | toList () const |
| Return a list or a single item in a container. | |
| const Data & | operator[] (const std::string &word) const |
| Dictionary member accessor. | |
| Data & | operator[] (const std::string &word) |
| Dictionary member accessor. | |
| bool | contains (const std::string &word) const |
| Check if a key exists in a dictionary. | |
| void | remove (const std::string &word) |
| Remove key from dictionary. | |
| const Data & | at (const std::string &word) const |
| template<typename DataType , typename = typename std::enable_if<std::is_trivial<DataType>::value>::type> | |
| void | foreach (std::function< void(const std::string &, DataType)> func) const |
| Iterate over all dictionary members, calling a function with each key and value. | |
| template<typename DataType > | |
| void | foreach (std::function< void(const std::string &, const DataType &)> func) const |
Specialization of dictionary iterator for strings and Data. | |
| const Data & | operator[] (size_type index) const |
| List member accessor. | |
| Data & | operator[] (size_type index) |
| List member accessor. | |
| void | remove (size_type index) |
| Remove entry from position in list. | |
| void | push_back (const Data &value) |
| List back-inserter. | |
| void | push_back (Data &&value) |
| List back-inserter. | |
| size_type | size () const |
| Retrieve number of entries in a list. | |
| bool | empty () const |
| Empty check. | |
| template<typename DataType , typename = typename std::enable_if<std::is_trivial<Type>::value>::type> | |
| void | foreach (std::function< void(const DataType)> func) const |
| Iterate over all list members, calling a function with each value. | |
| template<typename DataType > | |
| void | foreach (std::function< void(const DataType &)> func) const |
Specialization of list iterator for strings and Data. | |
| Data::iterator | begin () const |
| Retrieve forward iterator. | |
| Data::iterator | end () const |
| Retrieve end iterator. | |
| std::string | toJson (int indent=NoIndent) const |
| Return serial data encoded using JSON. | |
| std::ostream & | toJson (std::ostream &target, int indent=NoIndent, bool suppress=false) const |
| Write serial data encoded using JSON. | |
| std::ostream & | dumpJson (const std::string &intro, std::ostream &target=std::clog) const |
Static Public Member Functions | |
| static const char * | type_name (Type ty) |
| Return the name corresponding to a type. | |
| template<typename T = Data> | |
| static Data | make_dictionary (std::initializer_list< std::pair< const char *, const T > > initial_data) |
| Dictionary factory. | |
| template<typename... T> | |
| static Data | make_list (T &&... params) |
| List factory. | |
Protected Types | |
| using | DictionaryType = std::unordered_map< std::string, Data > |
Protected Member Functions | |
| void | release () |
| Release any stored data and restore to an "empty" state. | |
Protected Attributes | |
| Type | datatype = Type::Null |
| Type presently stored in object. | |
| union Parsnip::Data::data_t | data |
Friends | |
| class | IncorrectDataType |
| class | SchemaBase |
| class | DictionarySchema |
| class | ConjunctionSchema |
Generic data type.
A datatype that can hold strings, numbers, booleans, lists, or dictionaries.
|
strong |
Types that may be stored in the data.
|
inline |
String constructor.
| value | The string value to assign. |
| type_certainty | True if known to be string, false if it may be a number or boolean in string form. |
|
inline |
Dictionary constructor.
Use Dictionary flag variable as first parameter. Subsequent parameters are name, value ... pairs (function must have an odd number of arguments).
|
inline |
List constructor.
Use List flag variable as first parameter. Subsequent parameters are inserted in order as list members.
|
inline |
Extract data as a string (templated version).
| IncorrectDataType | if value is not a string. |
|
inline |
Extract data as a floating point type.
| DataType | the desired return type. |
| DataRangeError | if value will not fit DataType. |
| IncorrectDataType | if value is not a number. |
|
inline |
Extract data as a boolean.
| IncorrectDataType | if value is not true or false. |
|
inline |
Retrieve data as the object itself.
Seems stupid on the surface, but useful for templates.
|
inline |
Extract data as an integer type, except boolean.
| DataType | the desired return type. |
| DataRangeError | if value will not fit in DataType. |
| IncorrectDataType | if value is not an integer. |
|
inline |
Return contents of a list as a specific datatype.
| Container | A standard (-like) container, such as std::vector <std::string>, std::list <int>, or even std::vector<std::vector<long>> to extract a contents of a 2-dimensional array. Member typedef Container::value_type determines the inner type. |
| IncorrectDataType | if the item is not a list, or any of the list's members are not the expected type. |
| int Parsnip::Data::asInteger | ( | int | base = 0 | ) | const |
Return value of data as an integer.
| base | The base for interpreting strings. Default is 10. |
| DataRangeError | if the value is out of representable range. |
| Data::ListType Parsnip::Data::asList | ( | ) | const |
Return data as a list.
| IncorrectDataType | for dictionaries. |
| long Parsnip::Data::asLong | ( | int | base = 0 | ) | const |
Return value of data as a long integer.
If the data is a string, parse it as an integer.
| base | The base for interpreting strings. Default is 10. |
| DataRangeError | if the value is out of representable range. |
| const Data::StringType & Parsnip::Data::asString | ( | ) | const |
|
inline |
Retrieve forward iterator.
| IncorrectDataType | if not a list. |
| bool Parsnip::Data::contains | ( | const std::string & | key | ) | const |
Check if a key exists in a dictionary.
| key | The key to check. |
|
inline |
Empty check.
Use isNull to check specifically for that condition.
|
inline |
Retrieve end iterator.
| IncorrectDataType | if not a list. |
|
inline |
Iterate over all list members, calling a function with each value.
This implementation covers simple types.
| DataType | The datatype expected to be in the dictionary. |
| func | The function to invoke with each member. |
| IncorrectDataType | if the list contains values of inapplicable type. This occurs when encountered; some invokations may happen before throwing. |
|
inline |
Iterate over all dictionary members, calling a function with each key and value.
This implementation covers simple types.
| DataType | The datatype expected to be in the dictionary. |
| func | The function to invoke with each member. |
| IncorrectDataType | if the dictionary contains values of inapplicable type. This occurs when encountered; some invokations may happen before throwing. |
Specialization of getOr for Data.
| Datatype | The desired return type of the data. |
| name | The name of the member to retrieve. |
| defvalue | The default to use if the member is not present. |
| IncorrectDataType | if the object is not a dictionary. |
|
inline |
Extract a value if a member exists, otherwise use a default.
| Datatype | The desired return type of the data. |
| name | The name of the member to retrieve. |
| defvalue | The default to use if the member is not present. |
| IncorrectDataType | if the object is not a dictionary, or the item is present but not the desired type. |
|
inlinenoexcept |
Check if data contains a value.
|
inlinestatic |
Dictionary factory.
| initial_data | Name-value pairs to be inserted into the dictionary. |
|
inlinestatic |
List factory.
Parameters are inserted in order as list members.
| const Data & Parsnip::Data::makeFlexible | ( | ) | const |
IF a data item is a string, change it to a flexible string.
Use of this function should be very rare; strings should be strings and numbers numbers. But if a number gets stored in a string, this allows it to be accessed until it can be fixed (and it tolerates fixing, too).
| bool Parsnip::Data::operator== | ( | const Data & | compare | ) | const |
Compare two structures.
| compare | The structure to compare to. |
| Data & Parsnip::Data::operator[] | ( | const std::string & | key | ) |
Dictionary member accessor.
If dictionary is NOT constant, accessing non-existent key creates it.
| const Data & Parsnip::Data::operator[] | ( | const std::string & | key | ) | const |
Dictionary member accessor.
If dictionary is constant, avoid automatic insertion, and throw error if key is not present in dictionary.
| Data & Parsnip::Data::operator[] | ( | size_type | index | ) |
List member accessor.
If list is NOT constant, accessing outside of range expands list size
| const Data & Parsnip::Data::operator[] | ( | size_type | index | ) | const |
List member accessor.
If list is constant, avoid automatic insertion, and throw error if index is out of range.
| Data::size_type Parsnip::Data::size | ( | ) | const |
Retrieve number of entries in a list.
| IncorrectDataType | if not a list. |
| std::string Parsnip::Data::toJson | ( | int | indent = NoIndent | ) | const |
Return serial data encoded using JSON.
| indent | Starting indentation for elements. |
| std::ostream & Parsnip::Data::toJson | ( | std::ostream & | target, |
| int | indent = NoIndent, |
||
| bool | suppress = false |
||
| ) | const |
Write serial data encoded using JSON.
| target | Stream to which to write data. |
| indent | Starting indentation for elements. |
| suppress | If true, suppress indent on first line. |
|
inline |
Return a list or a single item in a container.
| Container | A standard (-like) container, such as std::vector <std::string>. Member typedef Container::value_type determines the inner type. If this object is a list, the container is populated with the list contents; otherwise, it is populated with the single data item. |
| IncorrectDataType | if this object is neither a list nor the expected type, or if it is a list but some members are not the expected type. |
|
static |
Return the name corresponding to a type.
| ty | The type whose name to retrieve. |