-
Notifications
You must be signed in to change notification settings - Fork 239
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0e758e6
[BL] Added ConfigTree wrapper.
chleh db8dfa9
[T] Added tests for new ConfigTree
chleh 3a0f704
[PL] ProcessVariable config parsed with new ConfigTree
chleh e3191b8
Added to Changelog
chleh 0172868
[BL] Dima's comments: updated doc, changed return value.
chleh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
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; | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 thischeckUnique
?There was a problem hiding this comment.
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.