Antares Simulator
Power System Simulator
storage.h
1 #pragma once
2 #include <stdexcept>
3 #include <unordered_map>
4 
5 #include "antares/io/outputs/columns.h"
6 
8 {
9 public:
10  void addStringColumn(const std::string& name)
11  {
12  addColumn<StringColumn>(name);
13  }
14 
15  template<Integral T>
16  void addIntegralColumn(const std::string& name)
17  {
18  addColumn<IntegralColumn<T>>(name);
19  }
20 
21  void addDoubleColumn(const std::string& name)
22  {
23  addColumn<DoubleColumn>(name);
24  }
25 
26  template<typename T>
27  void addOptionalColumn(const std::string& name)
28  {
29  addColumn<OptionalColumn<T>>(name);
30  }
31 
32  template<typename T>
33  void addValue(const std::string& column_name, const T& value)
34  {
35  if constexpr (std::is_same_v<T, std::string>)
36  {
37  getColumn<StringColumn>(column_name).add(value);
38  }
39  else if constexpr (std::is_integral_v<T>)
40  {
41  getColumn<IntegralColumn<T>>(column_name).add(value);
42  }
43  else if constexpr (std::is_floating_point_v<T>)
44  {
45  getColumn<DoubleColumn>(column_name).add(static_cast<double>(value));
46  }
47  else if constexpr (is_optional_v<T>)
48  {
49  using Inner = typename T::value_type;
50  getColumn<OptionalColumn<Inner>>(column_name).add(value);
51  }
52  else
53  {
54  throw std::runtime_error("Unsupported type"); // TODO
55  }
56  }
57 
58  size_t rowCount() const
59  {
60  return columns_.empty() ? 0 : (*columns_.begin())->size();
61  }
62 
63  const std::unordered_map<std::string, size_t>& columnsNameToIndex() const
64  {
65  return name_to_index_;
66  }
67 
68  const IColumn& getColumn(const std::string& name) const
69  {
70  const auto it = name_to_index_.find(name);
71  if (it == name_to_index_.end())
72  {
73  throw std::runtime_error("Column not found: " + name);
74  }
75  return *columns_.at(it->second);
76  }
77 
78  [[nodiscard]] const std::vector<std::unique_ptr<IColumn>>& columns() const
79  {
80  return columns_;
81  }
82 
83  [[nodiscard]] const std::vector<std::string>& columnNames() const
84  {
85  return columnNames_;
86  }
87 
88  void clear() const
89  {
90  for (auto& column: columns_)
91  {
92  column->clear();
93  }
94  }
95 
96 private:
97  template<typename ColumnType>
98  void addColumn(const std::string& name)
99  {
100  if (name_to_index_.contains(name))
101  {
102  throw std::runtime_error("Column already exists: " + name);
103  }
104 
105  std::size_t index = columns_.size();
106  auto col = std::make_unique<ColumnType>();
107  columns_.push_back(std::move(col));
108  columnNames_.push_back(name);
109  name_to_index_.emplace(std::move(name), index);
110  }
111 
112  // Access column by name
113  template<typename ColumnType>
114  ColumnType& getColumn(const std::string& name)
115  {
116  const auto it = name_to_index_.find(name);
117  if (it == name_to_index_.end())
118  {
119  throw std::runtime_error("Column not found: " + name);
120  }
121  return dynamic_cast<ColumnType&>(*columns_[it->second]);
122  }
123 
124  // Access column by index
125  template<typename ColumnType>
126  ColumnType& getColumn(const size_t index)
127  {
128  if (index >= columns_.size())
129  {
130  throw std::out_of_range("Column index out of range");
131  }
132  return dynamic_cast<ColumnType&>(*columns_[index]);
133  }
134 
135  std::vector<std::unique_ptr<IColumn>> columns_;
136  std::vector<std::string> columnNames_;
137  std::unordered_map<std::string, size_t> name_to_index_;
138 };
Definition: storage.h:8
Definition: columns.h:30