OpenWalnut  1.4.0
Public Member Functions | Static Public Attributes | Private Member Functions | Private Attributes | Friends
WStructuredTextParser::StructuredValueTree Class Reference

This simplifies working with a tree in a WStructuredTextParser::FileType instance. More...

#include <WStructuredTextParser.h>

List of all members.

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 >
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 >
operator[] (std::string key) const
 Queries the value with the given name.
StructuredValueTree getSubTree (std::string key) const
 Gets a subtree.
std::vector< StructuredValueTreegetSubTrees (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

Detailed Description

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.

Notes:
The syntax of the parsed files is defined by the parser itself. See WStructuredTextParser::Grammar for details.
Notes:
This also stores the comments of the parsed file. This allows them to be written again if OW loads a file, modifies it and re-writes it.

Definition at line 267 of file WStructuredTextParser.h.


Constructor & Destructor Documentation

Construct the instance given the original parsing structure.

Parameters:
filethe 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.

Parameters:
toParsethe 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.

Parameters:
filethe 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.


Member Function Documentation

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.

Parameters:
keypath to the values to count
valuesOnlyif true, it only counts values matching the given name.
Returns:
the number of found values.

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.

Parameters:
keypath to the value
valuesOnlyif true, it checks only if a value with the name exists. If false, also objects with this name cause this function to return true.
Returns:
true if existing.

Definition at line 68 of file WStructuredTextParser.cpp.

References count().

Referenced by WModuleMetaInformation::isIconAvailable(), and WModuleMetaInformation::WModuleMetaInformation().

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.

Parameters:
keykey to search.
Returns:
the structured value tree.

Definition at line 91 of file WStructuredTextParser.cpp.

References getSubTrees(), and StructuredValueTree().

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.

Parameters:
keykey to search.
Returns:
the structured value trees.

Definition at line 106 of file WStructuredTextParser.cpp.

References m_file, StructuredValueTree(), and traverse().

Referenced by WModuleMetaInformation::getOnlineResources(), WModuleMetaInformation::getScreenshots(), and getSubTree().

template<typename T >
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.

Parameters:
keypath to the value. Paths to whole objects are invalid.
defaultValuethe default if no value was found
Template Parameters:
Tthe return type. This method tries to cast to this type. If it fails, an exception is thrown. Type std::string is always valid.
Exceptions:
WTypeMismatchif the value cannot be cast to the specified target type
Returns:
the value
Notes:
this does not return a reference as the default value might be returned. It returns a copy of the value.

Definition at line 472 of file WStructuredTextParser.h.

Referenced by WModuleMetaInformation::getAuthors(), WModuleMetaInformation::getDescription(), WModuleMetaInformation::getHelp(), WModuleMetaInformation::getIcon(), and WModuleMetaInformation::getWebsite().

template<typename T >
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.

Parameters:
keypath to the value. Paths to whole objects are invalid.
defaultsthe defaults if no value was found
Template Parameters:
Tthe return type. This method tries to cast to this type. If it fails, an exception is thrown. Type std::string is always valid.
Exceptions:
WTypeMismatchif the value cannot be cast to the specified target type
Returns:
the value
Notes:
this does not return a reference as the default value might be returned. It returns a copy of the value.

Definition at line 479 of file WStructuredTextParser.h.

Referenced by WModuleMetaInformation::getAuthors(), and WModuleMetaInformation::getTags().

template<typename T >
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.

Parameters:
keypath to the value. Paths to whole objects are invalid.
Template Parameters:
Tthe return type. This method tries to cast to this type. If it fails, an exception is thrown. Type std::string is always valid.
Exceptions:
WTypeMismatchif the value cannot be cast to the specified target type
Returns:
the value vector. Might be empty if no elements where found.
Notes:
this does not return a reference as the default value might be returned. It returns a copy of the value.

Definition at line 596 of file WStructuredTextParser.h.

template<typename T >
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

Parameters:
keypath to the value. Paths to whole objects are invalid.
Template Parameters:
Tthe return type. This method tries to cast to this type. If it fails, an exception is thrown. Type std::string is always valid.
Exceptions:
WTypeMismatchif the value cannot be cast to the specified target type
WNotFoundif the key:value pair does not exist
Returns:
the value as copy to avoid any const_cast which would allow modification.

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.

Parameters:
currentcurrent element to check and recursively traverse
keyIterthe current path element
keyEndthe end iter. Just used to stop iteration if the key as not further elements
resultObjectsall matching instances of type WStructuredTextParser::ObjectType
resultValuesall 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.

Parameters:
currentcurrent element to check and recursively traverse
keythe path
resultObjectsall matching instances of type WStructuredTextParser::ObjectType
resultValuesall matching instances of type WStructuredTextParser::KeyValueType

Definition at line 125 of file WStructuredTextParser.cpp.

References Separator, string_utils::tokenize(), and traverse().


Member Data Documentation

The named values.

Definition at line 417 of file WStructuredTextParser.h.

Referenced by count(), and getSubTrees().

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().


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