COOLFluiD  Release kernel
COOLFluiD is a Collaborative Simulation Environment (CSE) focused on complex MultiPhysics simulations.
Public Member Functions | Static Public Member Functions | Public Attributes | Static Private Member Functions | List of all members
Map Class Reference

#include <Map.hpp>

Public Member Functions

 Map (XmlNode node=XmlNode())
 
XmlNode set_value (const std::string &value_key, const std::string type_name, const std::string &value_str, const std::string &descr=std::string())
 
XmlNode set_array (const std::string &value_key, const std::string element_type_name, const std::string &value_str, const std::string &delimiter, const std::string &descr=std::string())
 
XmlNode find_value (const std::string &value_key=std::string(), const char *value_type=nullptr) const
 Searches for a value in this map. More...
 
bool check_entry (const std::string &entry_key) const
 Checks whether an entry exists or not. More...
 
template<typename TYPE >
TYPE get_value (const std::string &val_key) const
 
template<typename TYPE >
std::vector< TYPE > get_array (const std::string &val_key) const
 
template<typename TYPE >
std::vector< TYPE > array_to_vector (const XmlNode &array_node, std::string *delim=nullptr) const
 

Static Public Member Functions

static bool is_single_value (const XmlNode &node)
 
static bool is_array_value (const XmlNode &node)
 
static const char * get_value_type (const XmlNode &node)
 
template<typename TYPE >
static void split_string (const std::string &str, const std::string &delimiter, std::vector< TYPE > &result, int size=-1)
 

Public Attributes

XmlNode content
 The managed node. More...
 

Static Private Member Functions

template<typename TYPE >
static bool value_has_ptr (const XmlNode &node)
 

Detailed Description

Manages maps in a XML tree. A map is an associative container of values: it contains values identified by a unique name (the "key"). Each value can contain another map. There are two kind of values: single values and array values. Single values contain only one value of a certain type and array ones may contain several values (each of the same type).
A value has an assigned type (one of those supported by #from_value() ) which cannot be changed (from example, a bool value cannot be changed to int). An existing value can be modified using set_value() or set_array(). If it does not exist, these function automatically add it.
A value can have a restricted list of values. This is an array that defines the acceptable values. Setting a restricted to value is the same as setting a array to it.

Author
Quentin Gasper.

Definition at line 38 of file Map.hpp.

Constructor & Destructor Documentation

Map ( XmlNode  node = XmlNode())

Constructor.

Parameters
nodeThe node to manage. Must be valid.

Definition at line 144 of file Map.cpp.

Member Function Documentation

std::vector< TYPE > array_to_vector ( const XmlNode array_node,
std::string *  delim = nullptr 
) const

Converts an array XML node to a vector of TYPE

Parameters
array_nodeThe array node to convert. Must be valid.
delimA pointer a string where the delimiter will be stored. Can be NULL.
Exceptions
XmlErrorif no delimiter is found.
ParsingFailedif no size is found or if the found size does not match with the number of found elements.

Definition at line 401 of file Map.cpp.

bool check_entry ( const std::string &  entry_key) const

Checks whether an entry exists or not.

An entry can be either a single value, or a map.

Parameters
entry_keyThe entry name
Returns
Returns true if the entry was found.

Definition at line 258 of file Map.cpp.

XmlNode find_value ( const std::string &  value_key = std::string(),
const char *  value_type = nullptr 
) const

Searches for a value in this map.

Parameters
value_keyThe key (name) of the wanted value. May be empty.
value_typeThe value type (single value or array). Three values are accepted : Protocol::Tags::node_value() (for a value), Protocol::Tags::node_array() (for an array) and an empty string (any type).
Returns
Returns a pointer to the first value found with the key. If the key is empty, returns the first value found in the map. Otherwise, returns a null pointer. If value_type is not valid, a null pointer is returned as well.

Definition at line 311 of file Map.cpp.

std::vector< TYPE > get_array ( const std::string &  val_key) const

Searches an array in the provided map and converts it to std::vector<TYPE>.

Note
This function can be used to get a restricted list of a value.
Parameters
mapThe map under which the array has to searched for.
val_keyThe array key.
Returns
Returns a vector containing the array values. The vector might be empty as the array may have no value.
Exceptions
BadValueIf the value key is empty.
XmlErrorIf no value with that key and value was found.

Definition at line 374 of file Map.cpp.

TYPE get_value ( const std::string &  val_key) const

Searches a value in the provided map and converts it to TYPE.

Parameters
mapThe map.
val_keyThe value key (name). Cannot be empty.
Returns
Returns the converted value.
Exceptions
BadValueIf the value key is empty.
XmlErrorIf no value with that key and value was found.

Definition at line 349 of file Map.cpp.

const char * get_value_type ( const XmlNode node)
static

Gives the type name of a provided value node.

Parameters
nodeThe node to process. Must be valid.
Returns
Returns a C-string with the type name.
Note
The function does not check whether the
Exceptions
XmlErrorIf
  • the node is neither a single nor an array value
  • no type was found inside the node

Definition at line 281 of file Map.cpp.

bool is_array_value ( const XmlNode node)
static

Checks whether the provided node represents an array value.

Note
This function does not check the validity of the XML representation (i.e. it doesn't check that the type exists or is valid,...).
Parameters
nodeThe node to check.
Returns
Returns true if the node represents a array value. Otherwise, returns false.

Definition at line 274 of file Map.cpp.

bool is_single_value ( const XmlNode node)
static

Checks whether the provided node represents a single value.

Note
This function does not check the validity of the XML representation (i.e. it doesn't check that the type exists or is valid,...).
Parameters
nodeThe node to check.
Returns
Returns true if the node represents a single value. Otherwise, returns false.

Definition at line 267 of file Map.cpp.

XmlNode set_array ( const std::string &  value_key,
const std::string  element_type_name,
const std::string &  value_str,
const std::string &  delimiter,
const std::string &  descr = std::string() 
)

Adds or modifies an array value. The TYPE must be one of those supported by #from_value(). If no value with the provided key exists, a single value is created. If it already exists and is a single value, it is modified. An array value cannot be modified as a single one.

Parameters
value_keyThe value key (name). Cannot be empty.
type_nameThe string description of the type of a single element
value_strThe value, as string, for the entire array
descrDescription
Exceptions
BadValueIf the value key is empty.

Definition at line 197 of file Map.cpp.

XmlNode set_value ( const std::string &  value_key,
const std::string  type_name,
const std::string &  value_str,
const std::string &  descr = std::string() 
)

Adds or modifies a value. The TYPE must be one of those supported by #from_value(). If no value with the provided key exists, a single value is created. If it already exists and is a single value, it is modified. An array value cannot be modified as a single one.

Parameters
value_keyThe value key (name). Cannot be empty.
type_nameThe string description of the type
value_strThe value, as string
descrDescription
Exceptions
BadValueIf the value key is empty.
XmlErrorif
  • the value exists and is an array
  • the value exists and its type does not match to TYPE
  • the value exists but does not have a type

Definition at line 152 of file Map.cpp.

void split_string ( const std::string &  str,
const std::string &  delimiter,
std::vector< TYPE > &  result,
int  size = -1 
)
static

Splits a string and casts each resulting part to TYPE. This function can whether the number of elements to read is known or or not. If the size is known the provided vector is cleared and initialized to contain this number of items. This way is more efficient, of course, because there is no reallocation needed. If the size if not defined, elements are appended to the vector by using push_back().

Parameters
strThe string to split.
delimiterThe string that delimitates the parts. It will not appear in the result.
resultThe vector where cast parts are stored.
sizeIf greater or equal to 0, gives the number of elements to read. Otherwise, the vector is reallocated as the elements are added.
Exceptions
CastingFailedIf an item cannot be cast to TYPE.

Definition at line 79 of file Map.cpp.

bool value_has_ptr ( const XmlNode node)
staticprivate

Checks whether the provided has the type TYPE. This function can be called for both single and array values.

Parameters
nodeThe node to check.
Returns
Returns true if the value has the correct type. Otherwise, or if the node is not valid or is neither a signle value nor an array value, returns false.

Definition at line 437 of file Map.cpp.

Member Data Documentation

XmlNode content

The managed node.

Definition at line 179 of file Map.hpp.


The documentation for this class was generated from the following files:
Send comments to:
COOLFluiD Web Admin