libdballe  9.14
value.h
1 #ifndef DBALLE_VALUE_H
2 #define DBALLE_VALUE_H
3 
4 #include <dballe/fwd.h>
5 #include <iosfwd>
6 #include <memory>
7 #include <wreport/varinfo.h>
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 
70 struct DBValue : public Value
71 {
72  using Value::Value;
73 
75  int data_id = MISSING_INT;
76 
77  DBValue() = default;
78  DBValue(const DBValue& o) = default;
79  DBValue(DBValue&& o) = default;
80 
82  DBValue(int data_id, const wreport::Var& var) : Value(var), data_id(data_id)
83  {
84  }
85 
87  DBValue(int data_id, std::unique_ptr<wreport::Var>&& var)
88  : Value(std::move(var)), data_id(data_id)
89  {
90  }
91 
92  DBValue& operator=(const DBValue&) = default;
93  DBValue& operator=(DBValue&&) = default;
94 
95  bool operator==(const DBValue& o) const;
96  bool operator!=(const DBValue& o) const;
97 
99  void print(FILE* out) const;
100 };
101 
102 std::ostream& operator<<(std::ostream&, const Value&);
103 std::ostream& operator<<(std::ostream&, const DBValue&);
104 
105 } // namespace dballe
106 
107 #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:75
Definition: utils.h:40
Definition: cmdline.h:18
DBValue(int data_id, const wreport::Var &var)
Construct from a wreport::Var.
Definition: value.h:82
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:70
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