libdballe  9.11
value.h
1 #ifndef DBALLE_VALUE_H
2 #define DBALLE_VALUE_H
3 
4 #include <dballe/fwd.h>
5 #include <wreport/varinfo.h>
6 #include <memory>
7 #include <iosfwd>
8 
9 namespace wreport {
10 struct Var;
11 }
12 
13 namespace dballe {
14 
18 class Value
19 {
20 protected:
21  wreport::Var* m_var = nullptr;
22 
23 public:
24  Value() = default;
25  Value(const Value& o);
26  Value(Value&& o) : m_var(o.m_var) { o.m_var = nullptr; }
27 
29  Value(const wreport::Var& var);
30 
32  Value(std::unique_ptr<wreport::Var>&& var) : m_var(var.release()) {}
33 
34  ~Value();
35 
36  Value& operator=(const Value& o);
37  Value& operator=(Value&& o);
38 
39  bool operator==(const Value& o) const;
40  bool operator!=(const Value& o) const;
41 
42  const wreport::Var* get() const { return m_var; }
43  wreport::Var* get() { return m_var; }
44 
45  const wreport::Var* operator->() const { return m_var; }
46  wreport::Var* operator->() { return m_var; }
47 
48  const wreport::Var& operator*() const { return *m_var; }
49  wreport::Var& operator*() { return *m_var; }
50 
52  wreport::Varcode code() const;
53 
55  void reset(const wreport::Var& var);
56 
58  void reset(std::unique_ptr<wreport::Var>&& var);
59 
61  std::unique_ptr<wreport::Var> release();
62 
64  void print(FILE* out) const;
65 };
66 
67 
71 struct DBValue : public Value
72 {
73  using Value::Value;
74 
76  int data_id = MISSING_INT;
77 
78  DBValue() = default;
79  DBValue(const DBValue& o) = default;
80  DBValue(DBValue&& o) = default;
81 
83  DBValue(int data_id, const wreport::Var& var)
84  : Value(var), data_id(data_id) {}
85 
87  DBValue(int data_id, std::unique_ptr<wreport::Var>&& var)
88  : Value(std::move(var)), data_id(data_id) {}
89 
90  DBValue& operator=(const DBValue&) = default;
91  DBValue& operator=(DBValue&&) = default;
92 
93  bool operator==(const DBValue& o) const;
94  bool operator!=(const DBValue& o) const;
95 
97  void print(FILE* out) const;
98 };
99 
100 std::ostream& operator<<(std::ostream&, const Value&);
101 std::ostream& operator<<(std::ostream&, const DBValue&);
102 
103 }
104 
105 #endif
wreport::Varcode code() const
Return the varcode of the variable, or 0 if no variable has been set.
Value(std::unique_ptr< wreport::Var > &&var)
Construct from a wreport::Var, taking ownership of it.
Definition: value.h:32
int data_id
Database ID of the value.
Definition: value.h:76
Definition: utils.h:31
Definition: cmdline.h:18
uint16_t Varcode
DBValue(int data_id, const wreport::Var &var)
Construct from a wreport::Var.
Definition: value.h:83
std::unique_ptr< wreport::Var > release()
Return the Var pointer, setting the Value to undefined.
void print(FILE *out) const
Print the contents of this Value.
void reset(const wreport::Var &var)
Fill from a wreport::Var.
Container for a wreport::Var pointer.
Definition: value.h:18
Container for a wreport::Var pointer, and its database ID.
Definition: value.h:71
void print(FILE *out) const
Print the contents of this Value.
DBValue(int data_id, std::unique_ptr< wreport::Var > &&var)
Construct from a wreport::Var, taking ownership of it.
Definition: value.h:87