Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
toki
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Redmine
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Analysers
toki
Commits
a34cc0a7
Commit
a34cc0a7
authored
Jun 10, 2014
by
Radosław Warzocha
Browse files
Options
Downloads
Patches
Plain Diff
Bool translator definition
parent
2f910fe3
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
libtoki/util/confignode.cpp
+23
-24
23 additions, 24 deletions
libtoki/util/confignode.cpp
libtoki/util/confignode.h
+27
-0
27 additions, 0 deletions
libtoki/util/confignode.h
with
50 additions
and
24 deletions
libtoki/util/confignode.cpp
+
23
−
24
View file @
a34cc0a7
...
@@ -20,7 +20,6 @@ or FITNESS FOR A PARTICULAR PURPOSE.
...
@@ -20,7 +20,6 @@ or FITNESS FOR A PARTICULAR PURPOSE.
#include
<libtoki/util/confignode.h>
#include
<libtoki/util/confignode.h>
#include
<libtoki/exception.h>
#include
<libtoki/exception.h>
#include
<boost/foreach.hpp>
#include
<libtoki/parser/loose_ini_paser.h>
#include
<libtoki/parser/loose_ini_paser.h>
#include
<boost/algorithm/string.hpp>
#include
<boost/algorithm/string.hpp>
...
@@ -29,6 +28,7 @@ or FITNESS FOR A PARTICULAR PURPOSE.
...
@@ -29,6 +28,7 @@ or FITNESS FOR A PARTICULAR PURPOSE.
#include
<boost/property_tree/ini_parser.hpp>
#include
<boost/property_tree/ini_parser.hpp>
#include
<boost/property_tree/xml_parser.hpp>
#include
<boost/property_tree/xml_parser.hpp>
#include
<boost/property_tree/json_parser.hpp>
#include
<boost/property_tree/json_parser.hpp>
#include
<boost/optional.hpp>
#include
<iostream>
#include
<iostream>
...
@@ -108,33 +108,32 @@ void write(const Node &c, const std::string &filename)
...
@@ -108,33 +108,32 @@ void write(const Node &c, const std::string &filename)
}
}
}
}
/** Boolean values translator **/
class
BoolTranslator
{
public:
typedef
std
::
string
internal_type
;
typedef
bool
external_type
;
boost
::
optional
<
external_type
>
get_value
(
internal_type
const
&
v
);
boost
::
optional
<
internal_type
>
put_value
(
external_type
const
&
v
);
}
boost
::
optional
<
BoolTranslator
::
external_type
>
boost
::
optional
<
BoolTranslator
::
external_type
>
BoolTranslator
::
get_value
(
BoolTranslator
::
internal_type
const
&
v
)
BoolTranslator
::
get_value
(
BoolTranslator
::
internal_type
const
&
v
)
{}
{
std
::
string
v_lower
=
boost
::
algorithm
::
to_lower_copy
(
v
);
if
(
v_lower
.
compare
(
"true"
)
==
0
||
v_lower
.
compare
(
"yes"
)
==
0
||
v_lower
.
compare
(
"on"
)
==
0
||
v_lower
.
compare
(
"1"
)
==
0
)
return
true
;
else
if
(
v_lower
.
compare
(
"false"
)
==
0
||
v_lower
.
compare
(
"off"
)
==
0
||
v_lower
.
compare
(
"no"
)
==
0
||
v_lower
.
compare
(
"0"
)
==
0
)
return
false
;
else
return
boost
::
none
;
}
boost
::
optional
<
BoolTranslator
::
internal_type
>
boost
::
optional
<
BoolTranslator
::
internal_type
>
BoolTranslator
::
get_value
(
BoolTranslator
::
external_type
const
&
v
)
BoolTranslator
::
put_value
(
BoolTranslator
::
external_type
const
&
v
)
{}
{
if
(
v
)
return
boost
::
optional
<
BoolTranslator
::
internal_type
>
(
"true"
);
else
return
boost
::
optional
<
BoolTranslator
::
internal_type
>
(
"false"
);
}
}
/* end ns Config */
}
/* end ns Config */
}
/* end namespace Toki */
}
/* end namespace Toki */
// This will make boost use our translator
// when retrieving boolean value all the time
namespace
boost
{
namespace
property_tree
{
template
<
>
struct
translator_between
<
std
::
string
,
bool
>
{
typedef
BoolTranslator
type
;
};
}}
This diff is collapsed.
Click to expand it.
libtoki/util/confignode.h
+
27
−
0
View file @
a34cc0a7
...
@@ -58,6 +58,33 @@ Node& merge_into(Node& accu, const Node& other);
...
@@ -58,6 +58,33 @@ Node& merge_into(Node& accu, const Node& other);
*/
*/
void
write
(
const
Node
&
c
,
const
std
::
string
&
filename
);
void
write
(
const
Node
&
c
,
const
std
::
string
&
filename
);
/**
* @brief Custom boolean values translator
*
* This translator convertes strings stored in boost::property_tree
* (default data struct for configuration) into boolean values when
* needed. It extends accepted strings to be one of @c true/false,
* @c yes/no, @c on/off, and @c 1/0 (like in default translator).
*/
class
BoolTranslator
{
public:
typedef
std
::
string
internal_type
;
typedef
bool
external_type
;
boost
::
optional
<
external_type
>
get_value
(
internal_type
const
&
v
);
boost
::
optional
<
internal_type
>
put_value
(
external_type
const
&
v
);
};
}
/* end ns Config */
}
/* end ns Toki */
}
/* end ns Config */
}
/* end ns Toki */
// This will make boost use our translator
// when retrieving boolean value all the time
namespace
boost
{
namespace
property_tree
{
template
<
>
struct
translator_between
<
std
::
string
,
bool
>
{
typedef
Toki
::
Config
::
BoolTranslator
type
;
};
}}
#endif // LIBTOKI_CONFIGNODE_H
#endif // LIBTOKI_CONFIGNODE_H
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment