tue_config
test_config.cpp
Go to the documentation of this file.
2 
3 #include <iostream>
4 
5 // ----------------------------------------------------------------------------------------------------
6 
7 void test1(const std::string& filename)
8 {
10 
11  config.loadFromYAMLFile(filename);
12 
14 
15  while(true)
16  {
17  if (config.sync())
18  {
19  std::cout << "Changed:" << std::endl << std::endl << config << std::endl;
20  }
21 
22  usleep(10000);
23  }
24 }
25 
26 // ----------------------------------------------------------------------------------------------------
27 
28 void test2()
29 {
31 
32  config.writeArray("array");
33 
35 
36  config.writeArray("array2");
37 
39  config.setValue("a", 1);
40  config.setValue("b", 2);
41  config.writeGroup("group");
42  config.setValue("c", 3);
43  config.setValue("d", 4);
44  config.endGroup();
45  config.setValue("e", 5);
47 
49  config.setValue("b", 2);
51 
52  config.endArray();
53 
55 
57  config.setValue("lib", "foo");
59 
61  config.setValue("lib", "bar");
63 
64  config.endArray();
65 
66  config.readArray("array");
67 
68  while(config.nextArrayItem())
69  {
71  }
72 
73  config.endArray();
74 
75  std::cout << "----------" << std::endl;
76 
78 }
79 
80 // ----------------------------------------------------------------------------------------------------
81 
82 void test3()
83 {
85  config.writeGroup("group");
86 
87  // create sub scope
89  config2.writeGroup("foo");
90  config2.setValue("bar", 123);
91 
92  // Accidentally do not close group 'foo': ...
93  // config2.endGroup(); // foo
94 
95  // config still refers to scope 'group', so NOT closing 'foo' causes NO problems
96  config.endGroup(); // group
97 
99 
100  std::cout << config.value("group.foo.br") << std::endl;
101 
102  config.readGroup("group");
103  config.readGroup("foo");
104  std::cout << config.value("br") << std::endl;
105  config.endArray();
106  config.endGroup();
107  config.endGroup();
108 
110 }
111 
112 // ----------------------------------------------------------------------------------------------------
113 
114 void test4()
115 {
117 
118  config.setValue("v1", "test");
119 
120  int v;
121  std::cout << config.value("v1", v) << std::endl;
122 
123  if (config.hasError())
124  {
125  std::cout << "ERROR: " << config.error() << std::endl;
126  }
127  else
128  {
129  std::cout << v << std::endl;
130  }
131 }
132 
133 // ----------------------------------------------------------------------------------------------------
134 
135 void test5()
136 {
137  tue::Configuration config1;
138  config1.setValue("foo", "test");
139  config1.setValue("bar", 123.456);
140  config1.writeGroup("group1");
141  config1.setValue("test", 100);
142  config1.setValue("test2", "bla");
143  config1.endGroup();
144 
145  tue::Configuration config2;
146  config2.setValue("foo", "replaced");
147  config2.writeGroup("group1");
148  config2.setValue("test2", "also replaced");
149  config2.endGroup();
150  config2.writeGroup("group2");
151  config2.setValue("name", "this is a new group");
152  config2.endGroup();
153 
154  config1.add(config2);
155 
156  std::cout << config1 << std::endl;
157  std::cout << "---" << std::endl;
158  std::cout << config2 << std::endl;
159 }
160 
161 // ----------------------------------------------------------------------------------------------------
162 
163 int main(int argc, char **argv) {
164 
166 
167  if (argc > 1)
168  {
169  test1(argv[1]);
170  }
171  else
172  {
173  test2();
174  }
175 
176  return 0;
177 }
std::string
tue::config::ReaderWriter::endArrayItem
bool endArrayItem()
endArrayItem go back to the array level
Definition: reader_writer.cpp:245
test4
void test4()
Definition: test_config.cpp:114
tue::config::ReaderWriter::writeArray
bool writeArray(const std::string &name)
writeArray starts writing an array. Or start extending it, if it already exists.
Definition: reader_writer.cpp:202
tue::config::ReaderWriter::loadFromYAMLFile
bool loadFromYAMLFile(const std::string &filename, const ResolveConfig &resolve_config=ResolveConfig::defaultConfig())
loadFromYAMLFile loads a yaml file into a ReaderWriter class
Definition: reader_writer.cpp:305
iostream
tue::config::ReaderWriter::writeGroup
bool writeGroup(const std::string &name)
writeGroup starts writing a group. Or start extending it, if it already exists.
Definition: reader_writer.cpp:181
test2
void test2()
Definition: test_config.cpp:28
tue::config::ReaderWriter::addArrayItem
bool addArrayItem()
addArrayItem create a new item in the array
Definition: reader_writer.cpp:223
test1
void test1(const std::string &filename)
Definition: test_config.cpp:7
tue::config::ReaderWriter::readArray
bool readArray(const std::string &name, const RequiredOrOptional opt=OPTIONAL)
readArray read the child with key 'name', which should be an array
Definition: reader_writer.h:84
std::cout
tue::config::ReaderWriter
Definition: reader_writer.h:26
tue::config::ReaderWriter::sync
bool sync()
sync re-read the source file if the file has changed since last reading time.
Definition: reader_writer.cpp:325
test5
void test5()
Definition: test_config.cpp:135
tue::config::ReaderWriter::error
const std::string & error() const
Definition: reader_writer.h:208
tue::config::ReaderWriter::endGroup
bool endGroup()
endGroup go to the parrent of current group, wrapping end() for readbility
Definition: reader_writer.h:104
tue::config::ReaderWriter::endArray
bool endArray()
endArray go to parrent of current array, wrapping end() for readibility
Definition: reader_writer.h:98
test3
void test3()
Definition: test_config.cpp:82
tue::config::ReaderWriter::value
bool value(const std::string &name, T &value, RequiredOrOptional opt=REQUIRED)
value read value of child with key 'name'
Definition: reader_writer.h:47
tue::config::ReaderWriter::setValue
void setValue(const std::string &name, const T &value)
setValue set child value with key 'name' and value 'value'
Definition: reader_writer.h:155
std::endl
T endl(T... args)
tue::config::ReaderWriter::limitScope
ReaderWriter limitScope() const
limitScope Limit the data to the current position. All parents and siblings incl. their child aren't ...
Definition: reader_writer.cpp:113
main
int main(int argc, char **argv)
Definition: test_config.cpp:163
tue::config::ReaderWriter::readGroup
bool readGroup(const std::string &name, const RequiredOrOptional opt=OPTIONAL)
readGroup read the child with key 'name', which should be a group
Definition: reader_writer.h:92
configuration.h
tue::config::ReaderWriter::nextArrayItem
bool nextArrayItem()
nextArrayItem go to the next item in the array, wrapping next() for readbility
Definition: reader_writer.h:110
config
tue::config::ReaderWriter config
Definition: sdf_gtest.cpp:9
tue::config::ReaderWriter::hasError
bool hasError() const
Definition: reader_writer.h:206