MIDAS
Loading...
Searching...
No Matches
odbxx_test.cxx File Reference
#include <string>
#include <iostream>
#include <array>
#include "midas.h"
#include "odbxx.h"
Include dependency graph for odbxx_test.cxx:

Go to the source code of this file.

Functions

int main ()
 

Function Documentation

◆ main()

int main ( void  )

Definition at line 19 of file odbxx_test.cxx.

19 {
20
21 cm_connect_experiment(nullptr, nullptr, "test", nullptr);
23
24 // delete /Test/Settings to start from scratch
25 midas::odb::delete_key("/Test/Settings");
26
27 // create ODB structure...
28 midas::odb o = {
29 {"Int32 Key", 42},
30 {"Bool Key", true},
31 {"Subdir", {
32 {"Int32 key", 123 },
33 {"Double Key", 1.2},
34 {"Subsub", {
35 {"Float key", 1.2f}, // floats must be explicitly specified
36 {"String Key", "Hello"},
37 }}
38 }},
39 {"Int Array", {1, 2, 3}},
40 {"Double Array", {1.2, 2.3, 3.4}},
41 {"String Array", {"Hello1", "Hello2", "Hello3"}},
42 {"Large Array", std::array<int, 10>{} }, // array with explicit size
43 {"Large String", std::string(63, '\0') }, // string with explicit size
44 {"String Array 10", std::array<std::string, 10>{}}, // string array with explicit size
45 // string array with 10 strings of each 63 chars
46 {"Large String Array 10", std::array<std::string, 10>{std::string(63, '\0')}}
47 };
48
49 // ...and push it to ODB. If keys are present in the
50 // ODB, their value is kept. If not, the default values
51 // from above are copied to the ODB
52 o.connect("/Test/Settings");
53
54 // alternatively, a structure can be created from an existing ODB subtree
55 midas::odb o2("/Test/Settings");
56 std::cout << o2 << std::endl;
57
58 // set, retrieve, and change ODB value
59 o["Int32 Key"] = 42;
60 int i = o["Int32 Key"];
61 o["Int32 Key"] = i+1;
62 o["Int32 Key"]++;
63 o["Int32 Key"] *= 1.3;
64 std::cout << "Should be 57: " << o["Int32 Key"] << std::endl;
65
66 // test with 64-bit variables
67 o["Int64 Key"] = -1LL;
68 int64_t i64 = o["Int64 Key"];
69 std::cout << std::hex << "0x" << i64 << std::dec << std::endl;
70
71 o["UInt64 Key"] = 0x1234567812345678u;
72 uint64_t ui64 = o["UInt64 Key"];
73 std::cout << std::hex << "0x" << ui64 << std::dec << std::endl;
74
75 // test with bool
76 o["Bool Key"] = false;
77 o["Bool Key"] = !o["Bool Key"];
78
79 // test with std::string
80 o["Subdir"]["Subsub"]["String Key"] = "Hello";
81 std::string s = o["Subdir"]["Subsub"]["String Key"];
82 s += " world!";
83 o["Subdir"]["Subsub"]["String Key"] = s;
84 s = s + o["Subdir"]["Subsub"]["String Key"].s(); // need .s() for concatenations
85 std::cout << s << std::endl;
86
87 // use std::string as index
88 std::string substr("Int32 Key");
89 o[substr] = 43;
90
91 // test with a vector
92 std::vector<int> v = o["Int Array"]; // read vector
93 std::fill(v.begin(), v.end(), 10);
94 o["Int Array"] = v; // assign vector to ODB array
95 o["Int Array"][1] = 2; // modify array element
96 i = o["Int Array"][1]; // read from array element
97 std::cout << i <<std::endl;
98 o["Int Array"].resize(5); // resize array
99 o["Int Array"]++; // increment all values of array
100 std::cout << "Arrays size is " << o["Int Array"].size() << std::endl;
101
102 // auto-enlarge arrays
103 o["Int Array"][10] = 10;
104
105 // test with a string vector
106 std::vector<std::string> sv;
107 sv = o["String Array"];
108 sv[1] = "New String";
109 o["String Array"] = sv;
110 o["String Array"][2] = "Another String";
111 o["String Array"][3] = std::string("One more");
112 s = o["String Array"][1].s(); // need .s() to explicitly convert to std::string
113 std::cout << s << std::endl;
114
115 // test with strings with given size
116 o["String Array 2"][0].set_string_size("Hello", 64);
117 o["String Array 2"][1] = "Second string";
118 o["String Array 2"][2] = "Third string";
119
120 // test with bool arrays/vectors
121 o["Bool Array"] = std::array<bool, 3>{true, false, true};
122 o["Bool Array from Vector"] = std::vector<bool>{true, false, true};
123
124 // iterate over array
125 int sum = 0;
126 for (int e : o["Int Array"])
127 sum += e;
128 std::cout << "Sum should be 37: " << sum << std::endl;
129
130 // create key from other key
131 midas::odb oi(o["Int32 Key"]);
132 oi = 123;
133
134 // test auto refresh read
135 std::cout << oi << std::endl; // each read access reads value from ODB
136 oi.set_auto_refresh_read(false); // turn off auto refresh
137 std::cout << oi << std::endl; // this does not read value from ODB
138 oi.read(); // this forces a manual read
139 std::cout << oi << std::endl;
140
141 // test auto refresh write
142 oi.set_auto_refresh_write(false); // turn off auto refresh write
143 oi = 321; // this does not write a value to the ODB
144 oi.write(); // this forces a manual write
145
146 // create ODB entries on-the-fly
148 ot.connect("/Test/Settings/OTF");// this forces /Test/OTF to be created if not already there
149 ot["Int32 Key"] = 1; // create all these keys with different types
150 ot["Double Key"] = 1.23;
151 ot["String Key"] = "Hello";
152 ot["Int Array"] = std::array<int, 10>{};
153 ot["Subdir"]["Int32 Key"] = 42;
154 ot["String Array"] = std::vector<std::string>{"S1", "S2", "S3"};
155 ot["Other String Array"][0] = "OSA0";
156 ot["Other String Array"][1] = "OSA1";
157
158 // create key with default value
159 i = ot["Int32 Key"](123); // key exists already (created above) -> use key value i=1
160 std::cout << i << std::endl;
161 i = ot["New Int32 Key"](123); // key does not exist -> set it to default value 123 i=123
162 std::cout << i << std::endl;
163
164 // std::string s1 = ot["New String Key"]("Hi"); // same for strings
165 std::cout << ot << std::endl;
166
167 o.read(); // re-read the underlying ODB tree which got changed by above OTF code
168 std::cout << o.print() << std::endl;
169
170 // iterate over sub-keys
172 for (midas::odb& oit : o)
173 std::cout << oit.get_name() << ": " << oit << std::endl;
174
175 // print whole sub-tree
176 std::cout << o.print() << std::endl;
177
178 // print whole subtree
179 std::cout << o.dump() << std::endl;
180
181 // update structure - create keys if needed, keep existing values if key already exists,
182 // delete keys that are in ODB but not the list of defaults.
183 midas::odb o3 = {
184 {"Int32 Key", 456},
185 {"New Bool Key", true},
186 {"String Array", {"Hello1", "Hello2", "Hello3"}},
187 {"Bool Key", true},
188 {"Subdir", {
189 {"Int32 key", 135 },
190 {"New Sub Bool Key", false},
191 {"Double Key", 1.5}
192 }}
193 };
194 o3.connect_and_fix_structure("/Test/Settings");
195
196 // Print new structure
197 std::cout << "After changing structure with o3:" << std::endl;
198 std::cout << o3.print() << std::endl;
199
200 // delete test key from ODB
201 o.delete_key();
202
203 // don't clutter watch callbacks
205
206 // watch ODB key for any change with lambda function
207 midas::odb ow("/Experiment");
208 ow.watch([](midas::odb &o) {
209 std::cout << "Value of key \"" + o.get_full_path() + "\" changed to " << o << std::endl;
210 });
211
212 do {
213 int status = cm_yield(100);
214 if (status == SS_ABORT || status == RPC_SHUTDOWN)
215 break;
216 } while (!ss_kbhit());
217
219 return 1;
220}
void delete_key()
Definition odbxx.cxx:1523
static void set_debug(bool flag)
Definition odbxx.h:1386
void connect_and_fix_structure(std::string path)
Definition odbxx.cxx:1509
void connect(const std::string &path, const std::string &name, bool write_defaults, bool delete_keys_not_in_defaults=false)
Definition odbxx.cxx:1430
INT cm_yield(INT millisec)
Definition midas.cxx:5664
INT cm_connect_experiment(const char *host_name, const char *exp_name, const char *client_name, void(*func)(char *))
Definition midas.cxx:2292
INT cm_disconnect_experiment(void)
Definition midas.cxx:2860
#define SS_ABORT
Definition midas.h:677
#define RPC_SHUTDOWN
Definition midas.h:707
BOOL ss_kbhit()
Definition system.cxx:3736
INT i
Definition mdump.cxx:32
DWORD status
Definition odbhist.cxx:39
TH1X EXPRT * h1_book(const char *name, const char *title, int bins, double min, double max)
Definition rmidas.h:24
static double e(void)
Definition tinyexpr.c:136
Here is the call graph for this function: