libdballe 9.13
values.h
Go to the documentation of this file.
1#ifndef DBALLE_VALUES_H
2#define DBALLE_VALUES_H
3
7
8#include <dballe/fwd.h>
9#include <dballe/value.h>
10#include <dballe/var.h>
11#include <functional>
12#include <iosfwd>
13#include <vector>
14
15namespace dballe {
16namespace impl {
17
18template <typename Value> class ValuesBase
19{
20public:
21 typedef typename std::vector<Value>::const_iterator const_iterator;
22 typedef typename std::vector<Value>::iterator iterator;
23
24protected:
25 std::vector<Value> m_values;
26
27 iterator insert_new(Value&& val);
28
29public:
30 ValuesBase() {}
31 ValuesBase(const ValuesBase&) = default;
32 ValuesBase(ValuesBase&&) = default;
33 ValuesBase& operator=(const ValuesBase&) = default;
34 ValuesBase& operator=(ValuesBase&&) = default;
35
36 const_iterator begin() const { return m_values.begin(); }
37 const_iterator end() const { return m_values.end(); }
38 const_iterator cbegin() const { return m_values.cbegin(); }
39 const_iterator cend() const { return m_values.cend(); }
40 iterator begin() { return m_values.begin(); }
41 iterator end() { return m_values.end(); }
42 iterator find(wreport::Varcode code) noexcept;
43 const_iterator find(wreport::Varcode code) const noexcept;
44 size_t size() const { return m_values.size(); }
45 bool empty() const { return m_values.empty(); }
46 void clear() { return m_values.clear(); }
47 void reserve(typename std::vector<Value>::size_type size)
48 {
49 m_values.reserve(size);
50 }
51 bool operator==(const ValuesBase<Value>& o) const;
52 bool operator!=(const ValuesBase<Value>& o) const;
53
55 void set(const wreport::Var&);
56
58 void set(std::unique_ptr<wreport::Var>&&);
59
61 void set(Value&& val);
62
64 void unset(wreport::Varcode code);
65
67 void merge(const ValuesBase<Value>& vals);
68
70 void merge(ValuesBase<Value>&& vals);
71
73 template <typename C, typename T> void set(const C& code, const T& val)
74 {
75 this->set(newvar(code, val));
76 }
77
78 template <typename C, typename T> void setf(const C& code, const T& val)
79 {
80 auto var = newvar(code);
81 var->setf(val);
82 this->set(std::move(var));
83 }
84
88 const Value& value(wreport::Varcode code) const;
89 const Value& value(const char* code) const
90 {
91 return value(resolve_varcode(code));
92 }
93 const Value& value(const std::string& code) const
94 {
95 return value(resolve_varcode(code));
96 }
97
101 const wreport::Var& var(wreport::Varcode code) const;
102 const wreport::Var& var(const char* code) const
103 {
104 return var(resolve_varcode(code));
105 }
106 const wreport::Var& var(const std::string& code) const
107 {
108 return var(resolve_varcode(code));
109 }
110
115 wreport::Var& var(wreport::Varcode code);
116 wreport::Var& var(const char* code) { return var(resolve_varcode(code)); }
117 wreport::Var& var(const std::string& code)
118 {
119 return var(resolve_varcode(code));
120 }
121
125 const Value* maybe_value(wreport::Varcode code) const;
126 const Value* maybe_value(const char* code) const
127 {
128 return maybe_value(resolve_varcode(code));
129 }
130 const Value* maybe_value(const std::string& code) const
131 {
132 return maybe_value(resolve_varcode(code));
133 }
134
138 const wreport::Var* maybe_var(wreport::Varcode code) const;
139 const wreport::Var* maybe_var(const char* code) const
140 {
141 return maybe_var(resolve_varcode(code));
142 }
143 const wreport::Var* maybe_var(const std::string& code) const
144 {
145 return maybe_var(resolve_varcode(code));
146 }
147
151 wreport::Var* maybe_var(wreport::Varcode code);
152 wreport::Var* maybe_var(const char* code)
153 {
154 return maybe_var(resolve_varcode(code));
155 }
156 wreport::Var* maybe_var(const std::string& code)
157 {
158 return maybe_var(resolve_varcode(code));
159 }
160
164 template <typename C, typename T> T enq(C code, const T& def)
165 {
166 if (const wreport::Var* var = maybe_var(code))
167 return var->enq(def);
168 return def;
169 }
170
177
183 void move_to(std::function<void(std::unique_ptr<wreport::Var>)> dest);
184
186 void print(FILE* out) const;
187
191 std::vector<uint8_t> encode() const;
192
196 static std::vector<uint8_t> encode_attrs(const wreport::Var& var);
197
201 static void decode(const std::vector<uint8_t>& buf,
202 std::function<void(std::unique_ptr<wreport::Var>)> dest);
203};
204
205extern template struct ValuesBase<Value>;
206extern template struct ValuesBase<DBValue>;
207
208} // namespace impl
209
210struct DBValues;
211
215struct Values : public impl::ValuesBase<Value>
216{
217 using ValuesBase<Value>::ValuesBase;
218 Values() = default;
219 explicit Values(const DBValues&);
220 explicit Values(DBValues&&);
221
222 Values& operator=(const DBValues&);
223 Values& operator=(DBValues&&);
224};
225
229struct DBValues : public impl::ValuesBase<DBValue>
230{
231public:
232 using ValuesBase<DBValue>::ValuesBase;
233 DBValues() = default;
234 explicit DBValues(const Values&);
235 explicit DBValues(Values&&);
236
237 DBValues& operator=(const Values&);
238 DBValues& operator=(Values&&);
239
241 bool vars_equal(const DBValues& o) const;
242
244 void set_data_id(wreport::Varcode code, int data_id);
245
248 {
249 for (auto& val : m_values)
250 val.data_id = MISSING_INT;
251 }
252};
253
254std::ostream& operator<<(std::ostream&, const Values&);
255std::ostream& operator<<(std::ostream&, const DBValues&);
256
257} // namespace dballe
258
259#endif
Container for a wreport::Var pointer.
Definition value.h:19
Definition values.h:19
void print(FILE *out) const
Print the contents of this Values.
static void decode(const std::vector< uint8_t > &buf, std::function< void(std::unique_ptr< wreport::Var >)> dest)
Decode variables from a DB-All.e specific binary representation.
T enq(C code, const T &def)
Get the value of a variable, or def if it is not set.
Definition values.h:164
void set(std::unique_ptr< wreport::Var > &&)
Set from a wreport::Var, taking ownership of it.
std::vector< uint8_t > encode() const
Encode these values in a DB-All.e specific binary representation.
const Value * maybe_value(wreport::Varcode code) const
Lookup a value, returning nullptr if not found.
void merge(ValuesBase< Value > &&vals)
Add all the variables from vals.
wreport::Var & var(wreport::Varcode code)
Lookup a wreport::Var, throwing an exception if not found (non-const version)
static std::vector< uint8_t > encode_attrs(const wreport::Var &var)
Encode the attributes of var in a DB-All.e specific binary representation.
void move_to(std::function< void(std::unique_ptr< wreport::Var >)> dest)
Move all the Var passing them to the given function.
const wreport::Var & var(wreport::Varcode code) const
Lookup a wreport::Var, throwing an exception if not found.
wreport::Var * maybe_var(wreport::Varcode code)
Lookup a variable, returning nullptr if not found (non-const version)
void set(const C &code, const T &val)
Set a variable value, creating it if it does not exist.
Definition values.h:73
const wreport::Var * maybe_var(wreport::Varcode code) const
Lookup a variable, returning nullptr if not found.
const Value & value(wreport::Varcode code) const
Lookup a value, throwing an exception if not found.
void merge(const ValuesBase< Value > &vals)
Add all the variables from vals.
void unset(wreport::Varcode code)
Remove one variable.
void move_to_attributes(wreport::Var &dest)
Move all the Var as attributes to dest.
void set(const wreport::Var &)
Set from a wreport::Var.
void set(Value &&val)
Set with a Value.
Container for a wreport::Var pointer, and its database ID.
Definition value.h:71
Collection of DBValue objects, indexed by wreport::Varcode.
Definition values.h:230
void clear_ids()
Reset all the database IDs.
Definition values.h:247
bool vars_equal(const DBValues &o) const
Check if the variables are the same, regardless of the data_id.
void set_data_id(wreport::Varcode code, int data_id)
Set the database ID for the Value with this wreport::Varcode.
Collection of Value objects, indexed by wreport::Varcode.
Definition values.h:216
Create wreport variables from the DB-All.e B table.
wreport::Varcode resolve_varcode(const char *name)
Resolve a variable name to a varcode proper, dealing with aliases and validation.