libdballe 9.6
values.h
Go to the documentation of this file.
1#ifndef DBALLE_VALUES_H
2#define DBALLE_VALUES_H
3
8#include <dballe/fwd.h>
9#include <dballe/value.h>
10#include <dballe/var.h>
11#include <vector>
12#include <functional>
13#include <iosfwd>
14
15namespace dballe {
16namespace impl {
17
18template<typename Value>
20{
21public:
22 typedef typename std::vector<Value>::const_iterator const_iterator;
23 typedef typename std::vector<Value>::iterator iterator;
24
25protected:
26 std::vector<Value> m_values;
27
28 iterator insert_new(Value&& val);
29
30public:
31 ValuesBase() {}
32 ValuesBase(const ValuesBase&) = default;
33 ValuesBase(ValuesBase&&) = default;
34 ValuesBase& operator=(const ValuesBase&) = default;
35 ValuesBase& operator=(ValuesBase&&) = default;
36
37 const_iterator begin() const { return m_values.begin(); }
38 const_iterator end() const { return m_values.end(); }
39 const_iterator cbegin() const { return m_values.cbegin(); }
40 const_iterator cend() const { return m_values.cend(); }
41 iterator begin() { return m_values.begin(); }
42 iterator end() { return m_values.end(); }
43 iterator find(wreport::Varcode code) noexcept;
44 const_iterator find(wreport::Varcode code) const noexcept;
45 size_t size() const { return m_values.size(); }
46 bool empty() const { return m_values.empty(); }
47 void clear() { return m_values.clear(); }
48 void reserve(typename std::vector<Value>::size_type size) { m_values.reserve(size); }
49 bool operator==(const ValuesBase<Value>& o) const;
50 bool operator!=(const ValuesBase<Value>& o) const;
51
53 void set(const wreport::Var&);
54
56 void set(std::unique_ptr<wreport::Var>&&);
57
59 void set(Value&& val);
60
63
65 void merge(const ValuesBase<Value>& vals);
66
69
71 template<typename C, typename T> void set(const C& code, const T& val) { this->set(newvar(code, val)); }
72
73 template<typename C, typename T>
74 void setf(const C& code, const T& val)
75 {
76 auto var = newvar(code);
77 var->setf(val);
78 this->set(std::move(var));
79 }
80
84 const Value& value(wreport::Varcode code) const;
85 const Value& value(const char* code) const { return value(resolve_varcode(code)); }
86 const Value& value(const std::string& code) const { return value(resolve_varcode(code)); }
87
91 const wreport::Var& var(wreport::Varcode code) const;
92 const wreport::Var& var(const char* code) const { return var(resolve_varcode(code)); }
93 const wreport::Var& var(const std::string& code) const { return var(resolve_varcode(code)); }
94
99 wreport::Var& var(const char* code) { return var(resolve_varcode(code)); }
100 wreport::Var& var(const std::string& code) { return var(resolve_varcode(code)); }
101
106 const Value* maybe_value(const char* code) const { return maybe_value(resolve_varcode(code)); }
107 const Value* maybe_value(const std::string& code) const { return maybe_value(resolve_varcode(code)); }
108
113 const wreport::Var* maybe_var(const char* code) const { return maybe_var(resolve_varcode(code)); }
114 const wreport::Var* maybe_var(const std::string& code) const { return maybe_var(resolve_varcode(code)); }
115
120 wreport::Var* maybe_var(const char* code) { return maybe_var(resolve_varcode(code)); }
121 wreport::Var* maybe_var(const std::string& code) { return maybe_var(resolve_varcode(code)); }
122
126 template<typename C, typename T> T enq(C code, const T& def)
127 {
128 if (const wreport::Var* var = maybe_var(code))
129 return var->enq(def);
130 return def;
131 }
132
139
145 void move_to(std::function<void(std::unique_ptr<wreport::Var>)> dest);
146
148 void print(FILE* out) const;
149
153 std::vector<uint8_t> encode() const;
154
158 static std::vector<uint8_t> encode_attrs(const wreport::Var& var);
159
163 static void decode(const std::vector<uint8_t>& buf, std::function<void(std::unique_ptr<wreport::Var>)> dest);
164};
165
166extern template struct ValuesBase<Value>;
167extern template struct ValuesBase<DBValue>;
168
169}
170
171struct DBValues;
172
177{
178 using ValuesBase<Value>::ValuesBase;
179 Values() = default;
180 explicit Values(const DBValues&);
181 explicit Values(DBValues&&);
182
183 Values& operator=(const DBValues&);
184 Values& operator=(DBValues&&);
185};
186
187
191struct DBValues : public impl::ValuesBase<DBValue>
192{
193public:
194 using ValuesBase<DBValue>::ValuesBase;
195 DBValues() = default;
196 explicit DBValues(const Values&);
197 explicit DBValues(Values&&);
198
199 DBValues& operator=(const Values&);
200 DBValues& operator=(Values&&);
201
203 bool vars_equal(const DBValues& o) const;
204
206 void set_data_id(wreport::Varcode code, int data_id);
207
210 {
211 for (auto& val : m_values)
212 val.data_id = MISSING_INT;
213 }
214};
215
216std::ostream& operator<<(std::ostream&, const Values&);
217std::ostream& operator<<(std::ostream&, const DBValues&);
218
219}
220
221#endif
Container for a wreport::Var pointer.
Definition: value.h:19
Definition: values.h:20
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:126
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:71
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.
T enq() const
void setf(const char *val)
uint16_t Varcode
Container for a wreport::Var pointer, and its database ID.
Definition: value.h:72
Collection of DBValue objects, indexed by wreport::Varcode.
Definition: values.h:192
void clear_ids()
Reset all the database IDs.
Definition: values.h:209
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:177
Create wreport variables from the DB-All.e B table.
std::unique_ptr< wreport::Var > newvar(C code, const T &val)
Create a new Var, from the DB-All.e B table, with value.
Definition: var.h:63
wreport::Varcode resolve_varcode(const char *name)
Resolve a variable name to a varcode proper, dealing with aliases and validation.