MIDAS
Loading...
Searching...
No Matches
odbxx_test.cxx
Go to the documentation of this file.
1/********************************************************************\
2
3 Name: odbxx_test.cxx
4 Created by: Stefan Ritt
5
6 Contents: Test and Demo of Object oriented interface to ODB
7
8\********************************************************************/
9
10#include <string>
11#include <iostream>
12#include <array>
13#include <functional>
14
15#include "midas.h"
16#include "odbxx.h"
17
18/*------------------------------------------------------------------*/
19
20int main() {
21
24
25 // create ODB structure...
26 midas::odb o = {
27 {"Int32 Key", 42},
28 {"Bool Key", true},
29 {"Subdir", {
30 {"Int32 key", 123 },
31 {"Double Key", 1.2},
32 {"Subsub", {
33 {"Float key", 1.2f}, // floats must be explicitly specified
34 {"String Key", "Hello"},
35 }}
36 }},
37 {"Int Array", {1, 2, 3}},
38 {"Double Array", {1.2, 2.3, 3.4}},
39 {"String Array", {"Hello1", "Hello2", "Hello3"}},
40 {"Large Array", std::array<int, 10>{} }, // array with explicit size
41 {"Large String", std::string(63, '\0') }, // string with explicit size
42 {"String Array 10", std::array<std::string, 10>{}}, // string array with explicit size
43 // string array with 10 strings of each 63 chars
44 {"Large String Array 10", std::array<std::string, 10>{std::string(63, '\0')}}
45 };
46
47 // ...and push it to ODB. If keys are present in the
48 // ODB, their value is kept. If not, the default values
49 // from above are copied to the ODB
50 o.connect("/Test/Settings");
51
52 // alternatively, a structure can be created from an existing ODB subtree
53 midas::odb o2("/Test/Settings/Subdir");
54 std::cout << o2 << std::endl;
55
56 // set, retrieve, and change ODB value
57 o["Int32 Key"] = 42;
58 int i = o["Int32 Key"];
59 o["Int32 Key"] = i+1;
60 o["Int32 Key"]++;
61 o["Int32 Key"] *= 1.3;
62 std::cout << "Should be 57: " << o["Int32 Key"] << std::endl;
63
64 // test with bool
65 o["Bool Key"] = false;
66 o["Bool Key"] = !o["Bool Key"];
67
68 // test with std::string
69 o["Subdir"]["Subsub"]["String Key"] = "Hello";
70 std::string s = o["Subdir"]["Subsub"]["String Key"];
71 s += " world!";
72 o["Subdir"]["Subsub"]["String Key"] = s;
73
74 // test with a vector
75 std::vector<int> v = o["Int Array"]; // read vector
76 std::fill(v.begin(), v.end(), 10);
77 o["Int Array"] = v; // assign vector to ODB array
78 o["Int Array"][1] = 2; // modify array element
79 i = o["Int Array"][1]; // read from array element
80 o["Int Array"].resize(5); // resize array
81 o["Int Array"]++; // increment all values of array
82
83 // test with a string vector
84 std::vector<std::string> sv;
85 sv = o["String Array"];
86 sv[1] = "New String";
87 o["String Array"] = sv;
88 o["String Array"][2] = "Another String";
89
90 // iterate over array
91 int sum = 0;
92 for (int e : o["Int Array"])
93 sum += e;
94 std::cout << "Sum should be 27: " << sum << std::endl;
95
96 // creat key from other key
97 midas::odb oi(o["Int32 Key"]);
98 oi = 123;
99
100 // test auto refresh
101 std::cout << oi << std::endl; // each read access reads value from ODB
102 oi.set_auto_refresh_read(false); // turn off auto refresh
103 std::cout << oi << std::endl; // this does not read value from ODB
104 oi.read(); // this forces a manual read
105 std::cout << oi << std::endl;
106
107 // create ODB entries on-the-fly
109 ot.connect("/Test/Settings/OTF");// this forces /Test/OTF to be created if not already there
110 ot["Int32 Key"] = 1; // create all these keys with different types
111 ot["Double Key"] = 1.23;
112 ot["String Key"] = "Hello";
113 ot["Int Array"] = std::array<int, 10>{};
114 ot["Subdir"]["Int32 Key"] = 42;
115 ot["String Array"] = std::vector<std::string>{"S1", "S2", "S3"};
116
117 // create key with default value
118 i = ot["Int32 Key"](123); // key exists already -> use key value
119 i = ot["New Int32 Key"](123); // key does not exist -> set it to default value 123
120 std::string s1 = ot["New String Key"]("Hi"); // same for strings
121 std::cout << ot << std::endl;
122
123 o.read(); // re-read the underlying ODB tree which got changed by above OTF code
124 std::cout << o.print() << std::endl;
125
126 // iterate over sub-keys
127 for (midas::odb& oit : o)
128 std::cout << oit.get_name() << std::endl;
129
130 // print whole sub-tree
131 std::cout << o.print() << std::endl;
132
133 // dump whole subtree
134 std::cout << o.dump() << std::endl;
135
136 // delete test key from ODB
137 o.delete_key();
138
139 // watch ODB key for any change with lambda function
140 midas::odb ow("/Experiment");
141 ow.watch([](midas::odb &o) {
142 std::cout << "Value of key \"" + o.get_full_path() + "\" changed to " << o << std::endl;
143 });
144
145 do {
146 int status = cm_yield(100);
147 if (status == SS_ABORT || status == RPC_SHUTDOWN)
148 break;
149 } while (!ss_kbhit());
150
152 return 1;
153}
static void set_debug(bool flag)
Definition odbxx.h:1239
void connect(const std::string &path, const std::string &name, bool write_defaults, bool delete_keys_not_in_defaults=false)
Definition odbxx.cxx:1291
int main()
INT cm_yield(INT millisec)
Definition midas.cxx:5642
INT cm_connect_experiment(const char *host_name, const char *exp_name, const char *client_name, void(*func)(char *))
Definition midas.cxx:2278
INT cm_disconnect_experiment(void)
Definition midas.cxx:2846
#define SS_ABORT
Definition midas.h:677
#define RPC_SHUTDOWN
Definition midas.h:707
BOOL ss_kbhit()
Definition system.cxx:3664
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