dReal4
option_value.h
1 #pragma once
2 
3 #include <cassert>
4 #include <ostream>
5 #include <utility>
6 
7 namespace dreal {
8 
9 /// Represents an optional value in dReal. There are four ways that an
10 /// option can have its value -- by default, by a command-line
11 /// argument, by a set-info/set-option command from a .smt2 file, and
12 /// a manual update in a code. We define an order in these types and
13 /// make sure that an update is executed only if it is requested by
14 /// the same type or a higher type. For example, a value set by
15 /// command-line cannot be changed by an updated requested from a file.
16 template <typename T>
17 class OptionValue {
18  public:
19  enum class Type {
20  DEFAULT, ///< Default value
21  FROM_FILE, ///< Updated by a set-option/set-info in a file
22  FROM_COMMAND_LINE, ///< Updated by a command-line argument
23  FROM_CODE, ///< Explicitly updated by a code
24  };
25 
26  /// Constructs an option value with @p value.
27  explicit OptionValue(T value)
28  : value_{std::move(value)}, type_{Type::DEFAULT} {}
29 
30  /// Default copy constructor.
31  OptionValue(const OptionValue&) = default;
32 
33  /// Default move constructor.
34  OptionValue(OptionValue&&) noexcept = default;
35 
36  /// Default copy assign operator.
37  OptionValue& operator=(const OptionValue&) = default;
38 
39  /// Default move assign operator.
40  OptionValue& operator=(OptionValue&&) noexcept = default;
41 
42  /// Default destructor.
43  ~OptionValue() = default;
44 
45  /// Copy-assign operator for T.
46  ///
47  /// Note: It sets value with `Type::FROM_CODE` type.
48  OptionValue& operator=(const T& value) {
49  value_ = value;
50  type_ = Type::FROM_CODE;
51  return *this;
52  }
53 
54  /// Move-assign operator for T.
55  ///
56  /// Note: It sets value with `Type::FROM_CODE` type.
57  OptionValue& operator=(T&& value) {
58  value_ = std::move(value);
59  type_ = Type::FROM_CODE;
60  return *this;
61  }
62 
63  /// Returns the value.
64  const T& get() const { return value_; }
65 
66  /// Sets the value to @p value which is given by a command-line argument.
67  void set_from_command_line(const T& value) {
68  if (type_ != Type::FROM_CODE) {
69  value_ = value;
71  }
72  }
73 
74  /// Sets the value to @p value which is provided from a file.
75  ///
76  /// @note This operation is ignored if the current value is set by
77  /// command-line.
78  void set_from_file(const T& value) {
79  switch (type_) {
80  case Type::DEFAULT:
81  case Type::FROM_FILE:
82  value_ = value;
83  type_ = Type::FROM_FILE;
84  return;
85 
87  case Type::FROM_CODE:
88  // No operation.
89  return;
90  }
91  }
92 
93  friend std::ostream& operator<<(std::ostream& os, Type type) {
94  switch (type) {
96  return os << "DEFAULT";
98  return os << "FROM_FILE";
100  return os << "FROM_COMMAND_LINE";
102  return os << "FROM_CODE";
103  }
104  }
105 
106  private:
107  T value_;
108  Type type_;
109 };
110 } // namespace dreal
OptionValue & operator=(const OptionValue &)=default
Default copy assign operator.
Type
Definition: option_value.h:19
Sum type of symbolic::Expression and symbolic::Formula.
Definition: api.cc:9
Updated by a command-line argument.
OptionValue(T value)
Constructs an option value with value.
Definition: option_value.h:27
~OptionValue()=default
Default destructor.
OptionValue & operator=(const T &value)
Copy-assign operator for T.
Definition: option_value.h:48
Represents an optional value in dReal.
Definition: option_value.h:17
void set_from_file(const T &value)
Sets the value to value which is provided from a file.
Definition: option_value.h:78
Explicitly updated by a code.
void set_from_command_line(const T &value)
Sets the value to value which is given by a command-line argument.
Definition: option_value.h:67
Updated by a set-option/set-info in a file.
OptionValue & operator=(T &&value)
Move-assign operator for T.
Definition: option_value.h:57