Main Page
Related Pages
Modules
Namespaces
Classes
Files
Examples
File List
File Members
wire-cell-build
util
test
test_configuration.cxx
Go to the documentation of this file.
1
#include "
WireCellUtil/Configuration.h
"
2
#include "
WireCellUtil/Persist.h
"
3
#include "
WireCellUtil/Testing.h
"
4
5
#include <iostream>
6
7
using namespace
WireCell
;
8
using namespace
std
;
9
10
int
main
()
11
{
12
string
json = R
"(
13
{
14
"my_int": 1,
15
"my_float": 6.9,
16
"my_string": "hello",
17
"my_struct": { "x": 1, "y": 2, "z": 3 },
18
"my_array" : [ "one", "two", "three" ],
19
"my_array_dict" : [ {"a":1, "b":2}, {"a":10, "b":20}],
20
"data1" : { "data2": { "a":1, "b":2, "c":3 } }
21
}
22
)";
23
string
extra_json = R
"(
24
{
25
"my_int" : 2,
26
"data1" : { "data2": { "d":4 } },
27
"data3": { "data4" : 4 }
28
}
29
)";
30
31
32
Configuration
cfg
=
Persist::loads
(json);
33
Configuration
extra_cfg =
Persist::loads
(extra_json);
34
35
Assert
(
get
(cfg,
"my_int"
,0) == 1);
36
Assert
(
get
(cfg,
"my_float"
,0.0) == 6.9);
37
38
Assert
(
string
(
"hello"
) == convert<std::string>(cfg[
"my_string"
]));
39
40
cerr <<
"my_string="
<<
get
(
cfg
,
"my_string"
,
string
(
""
))<<
endl
;
41
Assert
(
get
(cfg,
"my_string"
,
string
(
""
)) ==
string
(
"hello"
));
42
43
44
put
(cfg,
"a.b.c"
, 42);
45
cerr << cfg <<
endl
;
46
int
n42get =
get
(
cfg
,
"a.b.c"
, 0);
47
Assert
(42 == n42get);
48
int
n42ind = cfg[
"a"
][
"b"
][
"c"
].asInt();
49
Assert
(42 == n42ind);
50
51
Assert
(get<int>(cfg,
"my_struct.x"
) == 1);
52
53
auto
nums = get< vector<string> >(
cfg
,
"my_array"
);
54
for
(
auto
anum : nums) {
55
cerr << anum <<
endl
;
56
}
57
58
Configuration
a
=
branch
(cfg,
"data1.data2.a"
);
59
Assert
(
convert<int>
(a) == 1);
60
61
Configuration
other
;
62
update
(other, cfg);
63
Configuration
last =
update
(other, extra_cfg);
64
cerr <<
"other:\n"
<< other <<
endl
;
65
cerr <<
"last:\n"
<< last <<
endl
;
66
Assert
(last[
"a"
][
"b"
][
"c"
] == 42);
67
Assert
(last[
"data3"
][
"data4"
] == 4);
68
69
return
0;
70
}
WireCell::update
Configuration update(Configuration &a, Configuration &b)
Merge dictionary b into a, return a.
Definition:
Configuration.cxx:19
string
std::string string
Definition:
nybbler.cc:12
std
STL namespace.
WireCell::put
void put(Configuration &cfg, const std::string &dotpath, const T &val)
Put value in configuration at the dotted path.
Definition:
Configuration.h:155
dbjson.cfg
cfg
Definition:
dbjson.py:29
Assert
#define Assert
Definition:
Testing.h:7
WireCell::branch
Configuration branch(Configuration cfg, const std::string &dotpath)
Follow a dot.separated.path and return the branch there.
Definition:
Configuration.cxx:7
Testing.h
fhicl::other
Definition:
exception.h:26
a
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition:
pointer.h:1124
WireCell
Definition:
Main.h:22
WireCell::Persist::loads
Json::Value loads(const std::string &text, const externalvars_t &extvar=externalvars_t(), const externalvars_t &extcode=externalvars_t())
Definition:
Persist.cxx:152
main
int main()
Definition:
test_configuration.cxx:10
Persist.h
WireCell::Configuration
Json::Value Configuration
Definition:
Configuration.h:50
WireCell::convert< int >
int convert< int >(const Configuration &cfg, const int &def)
Definition:
Configuration.h:74
Configuration.h
endl
QTextStream & endl(QTextStream &s)
Definition:
qtextstream.cpp:2030
Generated by
1.8.11