libdballe  9.11
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 
15 namespace dballe {
16 namespace impl {
17 
18 template<typename Value>
20 {
21 public:
22  typedef typename std::vector<Value>::const_iterator const_iterator;
23  typedef typename std::vector<Value>::iterator iterator;
24 
25 protected:
26  std::vector<Value> m_values;
27 
28  iterator insert_new(Value&& val);
29 
30 public:
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 
62  void unset(wreport::Varcode code);
63 
65  void merge(const ValuesBase<Value>& vals);
66 
68  void merge(ValuesBase<Value>&& vals);
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 
105  const Value* maybe_value(wreport::Varcode code) const;
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 
112  const wreport::Var* maybe_var(wreport::Varcode code) const;
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 
138  void move_to_attributes(wreport::Var& dest);
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 
166 extern template struct ValuesBase<Value>;
167 extern template struct ValuesBase<DBValue>;
168 
169 }
170 
171 struct 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 
192 {
193 public:
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 
209  void clear_ids()
210  {
211  for (auto& val : m_values)
212  val.data_id = MISSING_INT;
213  }
214 };
215 
216 std::ostream& operator<<(std::ostream&, const Values&);
217 std::ostream& operator<<(std::ostream&, const DBValues&);
218 
219 }
220 
221 #endif
void move_to(std::function< void(std::unique_ptr< wreport::Var >)> dest)
Move all the Var passing them to the given function.
void move_to_attributes(wreport::Var &dest)
Move all the Var as attributes to dest.
const wreport::Var & var(wreport::Varcode code) const
Lookup a wreport::Var, throwing an exception if not found.
void unset(wreport::Varcode code)
Remove one variable.
T enq() const
const wreport::Var * maybe_var(wreport::Varcode code) const
Lookup a variable, returning nullptr if not found.
std::vector< uint8_t > encode() const
Encode these values in a DB-All.e specific binary representation.
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
Definition: cmdline.h:18
Collection of DBValue objects, indexed by wreport::Varcode.
Definition: values.h:191
void merge(const ValuesBase< Value > &vals)
Add all the variables from vals.
wreport::Varcode resolve_varcode(const char *name)
Resolve a variable name to a varcode proper, dealing with aliases and validation. ...
const Value & value(wreport::Varcode code) const
Lookup a value, throwing an exception if not found.
uint16_t Varcode
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.
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 print(FILE *out) const
Print the contents of this Values.
Container for a wreport::Var pointer.
Definition: value.h:18
void setf(const char *val)
Definition: values.h:19
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 clear_ids()
Reset all the database IDs.
Definition: values.h:209
const Value * maybe_value(wreport::Varcode code) const
Lookup a value, returning nullptr if not found.
Collection of Value objects, indexed by wreport::Varcode.
Definition: values.h:176