10000 ConfigTree with some checks added by chleh · Pull Request #912 · ufz/ogs · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

ConfigTree with some checks added #912

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions Applications/ApplicationsLib/ProjectData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#include "FileIO/XmlIO/Boost/BoostXmlGmlInterface.h"
#include "FileIO/readMeshFromFile.h"

#include "BaseLib/ConfigTreeNew.h"

namespace detail
{
static
Expand Down Expand Up @@ -61,7 +63,9 @@ ProjectData::ProjectData(BaseLib::ConfigTree const& project_config,
_mesh_vec.push_back(mesh);

// process variables
parseProcessVariables(project_config.get_child("process_variables"));

BaseLib::ConfigTreeNew var_conf(project_config.get_child("process_variables"));
parseProcessVariables(var_conf);

// parameters
parseParameters(project_config.get_child("parameters"));
Expand Down Expand Up @@ -174,7 +178,7 @@ bool ProjectData::isMeshNameUniqueAndProvideUniqueName(std::string &name) const
}

void ProjectData::parseProcessVariables(
BaseLib::ConfigTree const& process_variables_config)
BaseLib::ConfigTreeNew& process_variables_config)
{
DBUG("Parse process variables:")
if (_geoObjects == nullptr) {
Expand All @@ -191,12 +195,12 @@ void ProjectData::parseProcessVariables(
return;
}

_process_variables.reserve(process_variables_config.size());
// _process_variables.reserve(process_variables_config.size());

for (auto it : process_variables_config) {
BaseLib::ConfigTree const& var_config = it.second;
for (auto var_config
: process_variables_config.getConfSubtreeList("process_variable")) {
// TODO Extend to referenced meshes.
_process_variables.emplace_back(var_config,*_mesh_vec[0],*_geoObjects);
_process_variables.emplace_back(var_config, *_mesh_vec[0], *_geoObjects);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Applications/ApplicationsLib/ProjectData.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ class ProjectData
/// Parses the process variables configuration and creates new variables for
/// each variable entry passing the corresponding subtree to the process
/// variable constructor.
void parseProcessVariables(BaseLib::ConfigTree const& process_variables_config);
void parseProcessVariables(BaseLib::ConfigTreeNew& process_variables_config);

/// Parses the parameters configuration and saves them in a list.
/// Checks if a parameter has name tag.
Expand Down
159 changes: 159 additions & 0 deletions BaseLib/ConfigTreeNew-impl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/**
* \copyright
* Copyright (c) 2012-2015, OpenGeoSys Community (http://www.opengeosys.org)
* Distributed under a Modified BSD License.
* See accompanying file LICENSE.txt or
* http://www.opengeosys.org/project/license
*
*/

#include "ConfigTreeNew.h"

#include <logog/include/logog.hpp>

namespace BaseLib
{

//! Wraps a pair of iterators for use as a range in range-based for-loops.
template<typename Iterator>
class Range
{
public:
explicit Range(Iterator begin, Iterator end)
: _begin(begin), _end(end)
{}

Iterator begin() const { return _begin; }
Iterator end() const { return _end; }
private:
Iterator _begin;
Iterator _end;
};

template<typename T>
T
ConfigTreeNew::
getConfParam(std::string const& param)
{
auto p = getConfParamOptional<T>(param);
if (p) return *p;

error("Key <" + param + "> has not been found");
return T();
}

template<typename T>
T
ConfigTreeNew::
getConfParam(std::string const& param, T const& default_value)
{
auto p = getConfParamOptional<T>(param);
if (p) return *p;
return default_value;
}

template<typename T>
boost::optional<T>
ConfigTreeNew::
getConfParamOptional(std::string const& param)
{
checkUnique(param);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are the other getters (getConfParam and its optional version) without this checkUnique?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other methods call this one, so additional checks are not necessary.

auto p = _tree->get_child_optional(param);

bool peek_only = p == boost::none;
markVisited<T>(param, peek_only);

if (p) {
auto v = p->get_value_optional<T>();
if (v) {
return v;
} else {
error("Value for key <" + param + "> `" + shortString(p->data())
+ "' not convertible to the desired type.");
}
}

return boost::none;
}

template<typename T>
Range<ConfigTreeNew::ValueIterator<T> >
ConfigTreeNew::
getConfParamList(std::string const& param)
{
checkUnique(param);
markVisited<T>(param, true);

auto p = _tree->equal_range(param);
return Range<ValueIterator<T> >(
ValueIterator<T>(p.first, param, *this),
ValueIterator<T>(p.second, param, *this));
}

template<typename T>
T
ConfigTreeNew::
peekConfParam(std::string const& param)
{
checkKeyname(param);

auto p =_tree->get_child_optional(param);

if (!p) {
error("Key <" + param + "> has not been found");
} else {
try {
return p->get_value<T>();
} catch (boost::property_tree::ptree_bad_data) {
error("Value for key <" + param + "> `" + shortString(p->data())
+ "' not convertible to the desired type.");
}
}

return T();
}

template<typename T>
void
ConfigTreeNew::
checkConfParam(std::string const& param, T const& value)
{
if (getConfParam<T>(param) != value) {
error("The value of key <" + param + "> is not the expected one.");
}
}

template&l 629A t;typename Ch>
void
ConfigTreeNew::
checkConfParam(std::string const& param, Ch const* value)
{
if (getConfParam<std::string>(param) != value) {
error("The value of key <" + param + "> is not the expected one.");
}
}


template<typename T>
ConfigTreeNew::CountType&
ConfigTreeNew::
markVisited(std::string const& key, bool peek_only)
{
auto const type = std::type_index(typeid(T));

auto p = _visited_params.emplace(key, CountType{peek_only ? 0 : 1, type});

if (!p.second) { // no insertion happened
auto& v = p.first->second;
if (v.type == type) {
if (!peek_only) ++v.count;
} else {
error("There already was an attempt to obtain key <" + key
+ "> with type \"" + v.type.name() + "\" (now: \"" + type.name() + "\").");
}
}

return p.first->second;
}

}
Loading
0