Horizon
attribute_util.hpp
1 #pragma once
2 #include "attributes.hpp"
3 #include <type_traits>
4 #include <string>
5 #include <map>
6 #include <vector>
7 
8 namespace horizon::ODB {
9 
11 
12 public:
13  template <typename Tr, typename Ta> void add_attribute(Tr &r, Ta v)
14  {
15  using Tc = typename Tr::template check_type<Ta>;
16  static_assert(Tc::value);
17 
18  const auto id = get_or_create_attribute_name(attribute::attribute_name<Ta>::name);
19  if constexpr (std::is_enum_v<Ta>)
20  r.attributes.emplace_back(id, std::to_string(static_cast<int>(v)));
21  else
22  r.attributes.emplace_back(id, attr_to_string(v));
23  }
24 
25 protected:
26  unsigned int get_or_create_attribute_name(const std::string &name);
27 
28  void write_attributes(std::ostream &ost, const std::string &prefix = "") const;
29 
30 
31 private:
32  unsigned int get_or_create_attribute_text(const std::string &name);
33 
34  static std::string double_to_string(double v, unsigned int n);
35 
36  template <typename T, unsigned int n> std::string attr_to_string(attribute::float_attribute<T, n> a)
37  {
38  return double_to_string(a.value, a.ndigits);
39  }
40 
41  template <typename T> std::string attr_to_string(attribute::boolean_attribute<T> a)
42  {
43  return "";
44  }
45 
46  template <typename T> std::string attr_to_string(attribute::text_attribute<T> a)
47  {
48  return std::to_string(get_or_create_attribute_text(a.value));
49  }
50 
51 
52  std::map<std::string, unsigned int> attribute_names;
53  std::map<std::string, unsigned int> attribute_texts;
54 };
55 
57 
58 protected:
59  void write_attributes(std::ostream &ost) const;
60 
61 public:
62  std::vector<std::pair<unsigned int, std::string>> attributes;
63 };
64 } // namespace horizon::ODB
Definition: attribute_util.hpp:10
Definition: attribute_util.hpp:56
template(typename ActionFn, typename Rng)(concept(invocable_action_closure_)(ActionFn
\concept invocable_action_closure_
Definition: attributes.hpp:10
Definition: attributes.hpp:21
Definition: attributes.hpp:15
Definition: attributes.hpp:26