tudocomp
– The TU Dortmund Compression Framework
OptionValue.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <map>
4 #include <set>
5 #include <sstream>
6 #include <stack>
7 #include <stdexcept>
8 #include <string>
9 #include <vector>
10 
11 #include <glog/logging.h>
12 
14 #include <tudocomp/util.hpp>
15 
16 namespace tdc {
17 
18 // Forward declaration to be used by the parser
20 namespace pattern {
21  class Algorithm;
22 
23  inline std::ostream& operator<<(std::ostream&, const Algorithm&);
24 }
26 
27 //DIY lexical cast
28 template<typename T> T lexical_cast(const std::string& s) {
29  T val;
30  std::stringstream(s) >> val;
31  return val;
32 }
33 
34 class OptionValue;
36 public:
37  using ArgumentMap = std::map<std::string, OptionValue>;
38  using StatMap = std::map<std::string, std::string>;
39 private:
40  std::string m_name;
41  ArgumentMap m_arguments;
42  // Aways valid!
43  std::unique_ptr<pattern::Algorithm> m_static_selection;
44  friend class OptionValue;
46 public:
47  inline ~AlgorithmValue();
48  inline AlgorithmValue(const AlgorithmValue& other);
49  inline AlgorithmValue(AlgorithmValue&& other);
50  inline AlgorithmValue(std::string&& name,
51  ArgumentMap&& arguments,
52  std::unique_ptr<pattern::Algorithm>&& static_selection,
54 
55  inline const std::string& name() const;
56  inline const ArgumentMap& arguments() const;
57  inline ds::InputRestrictionsAndFlags textds_flags() const;
58  inline const pattern::Algorithm& static_selection() const;
59  inline AlgorithmValue& operator=(AlgorithmValue&& other);
60 };
61 
62 inline std::ostream& operator<<(std::ostream& os, const AlgorithmValue& av);
63 
64 class OptionValue {
65  bool m_is_value;
66  AlgorithmValue m_value_or_algorithm;
67  friend class AlgorithmValue;
68 public:
69  inline ~OptionValue();
70  inline OptionValue();
71  inline OptionValue(std::string&& value);
72  inline OptionValue(AlgorithmValue&& algorithm);
73  inline OptionValue(const OptionValue& other);
74  inline OptionValue(OptionValue&& other);
75 
76  inline bool is_algorithm() const;
77  inline const AlgorithmValue& as_algorithm() const;
78  inline AlgorithmValue to_algorithm() &&;
79  inline const std::string& as_string() const;
80  inline uint64_t as_integer() const;
81  inline double as_floating() const;
82  inline bool as_bool() const;
83  template<class T>
84  inline T as() const;
85  inline OptionValue& operator=(OptionValue&& other);
86 };
87 
88 inline std::ostream& operator<<(std::ostream& os, const OptionValue& ov);
89 
91 
93  m_name(other.m_name),
94  m_arguments(other.m_arguments),
95  m_ds_flags(other.m_ds_flags)
96 {
97  if (other.m_static_selection != nullptr) {
98  m_static_selection = std::make_unique<pattern::Algorithm>(
99  *other.m_static_selection);
100  }
101 }
102 
104  m_name(std::move(other.m_name)),
105  m_arguments(std::move(other.m_arguments)),
106  m_static_selection(std::move(other.m_static_selection)),
107  m_ds_flags(other.m_ds_flags) {}
108 
111  std::unique_ptr<pattern::Algorithm>&& static_selection,
113  m_name(std::move(name)),
114  m_arguments(std::move(arguments)),
115  m_static_selection(std::move(static_selection)),
116  m_ds_flags(flags) {}
117 
118 inline const std::string& AlgorithmValue::name() const {
119  return m_name;
120 }
122  return m_arguments;
123 }
124 inline const pattern::Algorithm& AlgorithmValue::static_selection() const {
125  CHECK(m_static_selection != nullptr);
126  return *m_static_selection;
127 }
129  this->m_name = std::move(other.m_name);
130  this->m_arguments = std::move(other.m_arguments);
131  this->m_static_selection = std::move(other.m_static_selection);
132  this->m_ds_flags = std::move(other.m_ds_flags);
133  return *this;
134 }
136  return m_ds_flags;
137 }
138 
140 
142 inline OptionValue::OptionValue(std::string&& value):
143  m_is_value(true),
144  m_value_or_algorithm(AlgorithmValue(std::move(value), {}, std::make_unique<pattern::Algorithm>(), ds::InputRestrictionsAndFlags())) {}
146  m_is_value(false),
147  m_value_or_algorithm(std::move(algorithm)) {}
148 
150  m_is_value(other.m_is_value),
151  m_value_or_algorithm(other.m_value_or_algorithm) {}
152 
154  m_is_value(other.m_is_value),
155  m_value_or_algorithm(std::move(other.m_value_or_algorithm)) {}
156 
157 inline bool OptionValue::is_algorithm() const {
158  return !m_is_value;
159 }
161  CHECK(is_algorithm());
162  return m_value_or_algorithm;
163 }
164 
166  CHECK(is_algorithm());
167  return std::move(m_value_or_algorithm);
168 }
169 
170 inline const std::string& OptionValue::as_string() const {
171  CHECK(m_is_value);
172  return m_value_or_algorithm.m_name;
173 }
174 inline uint64_t OptionValue::as_integer() const {
175  return lexical_cast<uint64_t>(as_string());
176 }
177 inline double OptionValue::as_floating() const {
178  return lexical_cast<double>(as_string());
179 }
180 inline bool OptionValue::as_bool() const {
181  auto& s = as_string();
182  if (s == "true") return true;
183  if (s == "false") return false;
184  throw std::runtime_error(std::string("option with string value '")
185  + s
186  + "' can not be converted to an boolean value!");
187 }
188 template<class T>
189 inline T OptionValue::as() const {
190  return lexical_cast<T>(as_string());
191 }
193  this->m_is_value = other.m_is_value;
194  this->m_value_or_algorithm = std::move(other.m_value_or_algorithm);
195  return *this;
196 }
197 
198 inline std::ostream& operator<<(std::ostream& os, const AlgorithmValue& av) {
199  os << av.name() << " {";
200 
201  for (auto& e : av.arguments()) {
202  os << e.first << " : " << e.second << ", ";
203  }
204 
205  return os << "; ds_flags: " << av.textds_flags()
206  << ", static_selection: " << av.static_selection()
207  << "}";
208 }
209 
210 inline std::ostream& operator<<(std::ostream& os, const OptionValue& ov) {
211  if (!ov.is_algorithm()) {
212  return os << "string: " << ov.as_string();
213  } else {
214  return os << ov.as_algorithm();
215  }
216 }
217 
218 }
219 
Contains the text compression and encoding framework.
Definition: namespaces.hpp:11
AlgorithmValue(const AlgorithmValue &other)
Definition: OptionValue.hpp:92
uint_impl_t & operator=(const uint_impl_t &b)
Definition: uint_t.hpp:43
std::ostream & operator<<(std::ostream &os, const uint_impl_t< N > &v)
Definition: uint_t.hpp:135
const std::string & as_string() const
const std::string & name() const
const pattern::Algorithm & static_selection() const
std::map< std::string, std::string > StatMap
Definition: OptionValue.hpp:38
uint64_t as_integer() const
AlgorithmValue & operator=(AlgorithmValue &&other)
std::map< std::string, OptionValue > ArgumentMap
Definition: OptionValue.hpp:37
AlgorithmValue to_algorithm() &&
bool is_algorithm() const
OptionValue & operator=(OptionValue &&other)
const ArgumentMap & arguments() const
ds::InputRestrictionsAndFlags textds_flags() const
bool as_bool() const
const AlgorithmValue & as_algorithm() const
double as_floating() const
T lexical_cast(const std::string &s)
Definition: OptionValue.hpp:28