OpenWalnut
1.4.0
|
This simplifies working with a tree in a WStructuredTextParser::FileType instance. More...
#include <WStructuredTextParser.h>
Public Member Functions | |
StructuredValueTree (const FileType &file) | |
Construct the instance given the original parsing structure. | |
StructuredValueTree (const std::string &toParse) | |
Construct the instance given a text as string. | |
StructuredValueTree (const boost::filesystem::path &file) | |
Construct the instance given a path to a file to load. | |
StructuredValueTree () | |
Creates an empty tree. | |
virtual | ~StructuredValueTree () |
Cleanup. | |
bool | exists (std::string key, bool valuesOnly=false) const |
Checks whether the given value or object exists. | |
size_t | count (std::string key, bool valuesOnly=false) const |
It is possible that there are multiple values matching a key. | |
template<typename T > | |
T | getValue (std::string key, const T &defaultValue) const |
Queries the value with the given name. | |
template<typename T > | |
std::vector< T > | getValues (std::string key, const std::vector< T > &defaults) const |
Queries the list of values matching the given path. | |
template<typename T > | |
std::vector< T > | getValues (std::string key) const |
Queries the list of values matching the given path. | |
template<typename T > | |
T | operator[] (std::string key) const |
Queries the value with the given name. | |
StructuredValueTree | getSubTree (std::string key) const |
Gets a subtree. | |
std::vector< StructuredValueTree > | getSubTrees (std::string key) const |
Gets all matching subtrees. | |
Static Public Attributes | |
static const std::string | Separator = "/" |
This char is used as separator for identifying values in the tree. | |
Private Member Functions | |
void | traverse (MemberType current, std::vector< std::string >::const_iterator keyIter, std::vector< std::string >::const_iterator keyEnd, std::vector< ObjectType > &resultObjects, std::vector< KeyValueType > &resultValues) const |
Recursively fills a result vector using a given path iterator. | |
void | traverse (FileType current, std::string key, std::vector< ObjectType > &resultObjects, std::vector< KeyValueType > &resultValues) const |
Recursively fills a result vector using a given path iterator. | |
Private Attributes | |
FileType | m_file |
The named values. | |
Friends | |
class | WStructuredTextParserTest |
This simplifies working with a tree in a WStructuredTextParser::FileType instance.
It provides easy query and check methods. It does not provide any semantic options. So check validity of the contents and structure of the tree is the job of the using class/derived class. As the tree does not know anything about the semantics of your structure, it is also untyped. For every key you query, you need to specify the type.
This tree uses the types in the WStructuredTextParser namespace. To avoid unnecessary copy operations, this class is not recursive itself. When querying, you always need to specify the full path. This class can be seen as accessor to the WStructuredTextParser::ObjectType tree.
Definition at line 267 of file WStructuredTextParser.h.
WStructuredTextParser::StructuredValueTree::StructuredValueTree | ( | const FileType & | file | ) | [explicit] |
Construct the instance given the original parsing structure.
file | the parsing result structure (the root node). |
Definition at line 41 of file WStructuredTextParser.cpp.
WStructuredTextParser::StructuredValueTree::StructuredValueTree | ( | const std::string & | toParse | ) | [explicit] |
Construct the instance given a text as string.
toParse | the text to parse |
Definition at line 52 of file WStructuredTextParser.cpp.
WStructuredTextParser::StructuredValueTree::StructuredValueTree | ( | const boost::filesystem::path & | file | ) | [explicit] |
Construct the instance given a path to a file to load.
file | the path to a file to load. |
Definition at line 58 of file WStructuredTextParser.cpp.
Creates an empty tree.
It will contain no information at all.
Definition at line 47 of file WStructuredTextParser.cpp.
Referenced by getSubTree(), and getSubTrees().
Cleanup.
Definition at line 63 of file WStructuredTextParser.cpp.
size_t WStructuredTextParser::StructuredValueTree::count | ( | std::string | key, |
bool | valuesOnly = false |
||
) | const |
It is possible that there are multiple values matching a key.
This method counts them.
key | path to the values to count |
valuesOnly | if true, it only counts values matching the given name. |
Definition at line 73 of file WStructuredTextParser.cpp.
References m_file, and traverse().
Referenced by exists().
bool WStructuredTextParser::StructuredValueTree::exists | ( | std::string | key, |
bool | valuesOnly = false |
||
) | const |
Checks whether the given value or object exists.
If you want to know only if a value with the given name exists, set valuesOnly to true.
key | path to the value |
valuesOnly | if true, it checks only if a value with the name exists. If false, also objects with this name cause this function to return true. |
Definition at line 68 of file WStructuredTextParser.cpp.
References count().
Referenced by WModuleMetaInformation::isIconAvailable(), and WModuleMetaInformation::WModuleMetaInformation().
StructuredValueTree WStructuredTextParser::StructuredValueTree::getSubTree | ( | std::string | key | ) | const |
Gets a subtree.
The ValueTree returned contains the node you have searched. It only contains the first match. If all matches are needed, use getSubTrees instead. If the key is not valid/nothing matches the key, an empty value tree is returned. If they key matches a key-value pair, nothing is returned. This means, this method is only useful for objects.
key | key to search. |
Definition at line 91 of file WStructuredTextParser.cpp.
References getSubTrees(), and StructuredValueTree().
std::vector< StructuredValueTree > WStructuredTextParser::StructuredValueTree::getSubTrees | ( | std::string | key | ) | const |
Gets all matching subtrees.
The subtrees returned contains the node you have searched. If multiple objects match the key, a list of subtrees is returned. If nothing matches, the returned list is empty. If they key matches a key-value pair, nothing is returned. This means, this method is only useful for objects.
key | key to search. |
Definition at line 106 of file WStructuredTextParser.cpp.
References m_file, StructuredValueTree(), and traverse().
Referenced by WModuleMetaInformation::getOnlineResources(), WModuleMetaInformation::getScreenshots(), and getSubTree().
T WStructuredTextParser::StructuredValueTree::getValue | ( | std::string | key, |
const T & | defaultValue | ||
) | const |
Queries the value with the given name.
If it is not found, the default value will be returned.
key | path to the value. Paths to whole objects are invalid. |
defaultValue | the default if no value was found |
T | the return type. This method tries to cast to this type. If it fails, an exception is thrown. Type std::string is always valid. |
WTypeMismatch | if the value cannot be cast to the specified target type |
Definition at line 472 of file WStructuredTextParser.h.
Referenced by WModuleMetaInformation::getAuthors(), WModuleMetaInformation::getDescription(), WModuleMetaInformation::getHelp(), WModuleMetaInformation::getIcon(), and WModuleMetaInformation::getWebsite().
std::vector< T > WStructuredTextParser::StructuredValueTree::getValues | ( | std::string | key, |
const std::vector< T > & | defaults | ||
) | const |
Queries the list of values matching the given path.
If it is not found, the default value will be returned.
key | path to the value. Paths to whole objects are invalid. |
defaults | the defaults if no value was found |
T | the return type. This method tries to cast to this type. If it fails, an exception is thrown. Type std::string is always valid. |
WTypeMismatch | if the value cannot be cast to the specified target type |
Definition at line 479 of file WStructuredTextParser.h.
Referenced by WModuleMetaInformation::getAuthors(), and WModuleMetaInformation::getTags().
std::vector< T > WStructuredTextParser::StructuredValueTree::getValues | ( | std::string | key | ) | const |
Queries the list of values matching the given path.
If it is not found, an empty results vector is returned.
key | path to the value. Paths to whole objects are invalid. |
T | the return type. This method tries to cast to this type. If it fails, an exception is thrown. Type std::string is always valid. |
WTypeMismatch | if the value cannot be cast to the specified target type |
Definition at line 596 of file WStructuredTextParser.h.
T WStructuredTextParser::StructuredValueTree::operator[] | ( | std::string | key | ) | const |
Queries the value with the given name.
If it is not found, an exception is thrown. If multiple entries with this path exist, the first one is returned. Use getValues in this case. Query the count of a key:value pair using count
key | path to the value. Paths to whole objects are invalid. |
T | the return type. This method tries to cast to this type. If it fails, an exception is thrown. Type std::string is always valid. |
WTypeMismatch | if the value cannot be cast to the specified target type |
WNotFound | if the key:value pair does not exist |
Definition at line 493 of file WStructuredTextParser.h.
void WStructuredTextParser::StructuredValueTree::traverse | ( | MemberType | current, |
std::vector< std::string >::const_iterator | keyIter, | ||
std::vector< std::string >::const_iterator | keyEnd, | ||
std::vector< ObjectType > & | resultObjects, | ||
std::vector< KeyValueType > & | resultValues | ||
) | const [private] |
Recursively fills a result vector using a given path iterator.
It checks whether the current element matches the current key. If yes, it traverses or adds the value to the result vector. This uses depth-first search and allows multiple matches for one key.
current | current element to check and recursively traverse |
keyIter | the current path element |
keyEnd | the end iter. Just used to stop iteration if the key as not further elements |
resultObjects | all matching instances of type WStructuredTextParser::ObjectType |
resultValues | all matching instances of type WStructuredTextParser::KeyValueType |
Definition at line 144 of file WStructuredTextParser.cpp.
References WStructuredTextParser::ObjectType::m_nodes.
Referenced by count(), getSubTrees(), and traverse().
void WStructuredTextParser::StructuredValueTree::traverse | ( | FileType | current, |
std::string | key, | ||
std::vector< ObjectType > & | resultObjects, | ||
std::vector< KeyValueType > & | resultValues | ||
) | const [private] |
Recursively fills a result vector using a given path iterator.
It checks whether the current element matches the current key. If yes, it traverses or adds the value to the result vector. This uses depth-first search and allows multiple matches for one key.
current | current element to check and recursively traverse |
key | the path |
resultObjects | all matching instances of type WStructuredTextParser::ObjectType |
resultValues | all matching instances of type WStructuredTextParser::KeyValueType |
Definition at line 125 of file WStructuredTextParser.cpp.
References Separator, string_utils::tokenize(), and traverse().
The named values.
Definition at line 417 of file WStructuredTextParser.h.
Referenced by count(), and getSubTrees().
const std::string WStructuredTextParser::StructuredValueTree::Separator = "/" [static] |
This char is used as separator for identifying values in the tree.
NEVER change this value.
Definition at line 274 of file WStructuredTextParser.h.
Referenced by traverse().