Antares Simulator
Power System Simulator
columns.h
1 /*
2 ** Copyright 2007-2025, RTE (https://www.rte-france.com)
3 ** See AUTHORS.txt
4 ** SPDX-License-Identifier: MPL-2.0
5 ** This file is part of Antares-Simulator,
6 ** Adequacy and Performance assessment for interconnected energy networks.
7 **
8 ** Antares_Simulator is free software: you can redistribute it and/or modify
9 ** it under the terms of the Mozilla Public Licence 2.0 as published by
10 ** the Mozilla Foundation, either version 2 of the License, or
11 ** (at your option) any later version.
12 **
13 ** Antares_Simulator is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ** Mozilla Public Licence 2.0 for more details.
17 **
18 ** You should have received a copy of the Mozilla Public Licence 2.0
19 ** along with Antares_Simulator. If not, see <https://opensource.org/license/mpl-2-0/>.
20 */
21 #pragma once
22 #include <optional>
23 #include <string>
24 #include <type_traits>
25 #include <vector>
26 
27 #include "antares/optimisation/linear-problem-api/hasStatus.h"
28 
29 class IColumn
30 {
31 public:
32  virtual ~IColumn() = default;
33  virtual std::string toString(size_t index) const = 0;
34  virtual size_t size() const = 0;
35  virtual void reserve(size_t capacity) = 0;
36  virtual void clear() = 0;
37 };
38 
39 template<typename T>
40 struct is_optional: std::false_type
41 {
42 };
43 
44 template<typename U>
45 struct is_optional<std::optional<U>>: std::true_type
46 {
47 };
48 
49 template<typename T>
50 inline constexpr bool is_optional_v = is_optional<T>::value;
51 
52 static std::string FromDouble(const double value)
53 {
54  std::ostringstream oss;
55  oss << std::setprecision(15) << value;
56  return oss.str();
57 }
58 
59 template<typename U>
60 static std::string FormatValue(const U& v)
61 {
62  if constexpr (std::is_same_v<U, std::string>)
63  {
64  return v;
65  }
66  else if constexpr (std::is_floating_point_v<U>)
67  {
68  return FromDouble(v);
69  }
70  else if constexpr (std::is_same_v<U, Antares::Optimisation::LinearProblemApi::MipBasisStatus>)
71  {
72  return StatusToString(v);
73  }
74  else if constexpr (is_optional_v<U>)
75  {
76  return v ? FormatValue(*v) : "None";
77  }
78  else
79  {
80  return std::to_string(v);
81  }
82 }
83 
84 template<typename T>
85 class TypedColumn final: public IColumn
86 {
87 public:
88  TypedColumn() = default;
89 
90  void add(const T& value)
91  {
92  data_.push_back(value);
93  }
94 
95  std::string toString(size_t index) const override
96  {
97  return FormatValue(data_.at(index));
98  }
99 
100  const T& get(size_t index) const
101  {
102  return data_.at(index);
103  }
104 
105  size_t size() const override
106  {
107  return data_.size();
108  }
109 
110  void reserve(size_t capacity) override
111  {
112  data_.reserve(capacity);
113  }
114 
115  const std::vector<T>& data() const
116  {
117  return data_;
118  }
119 
120  void clear() override
121  {
122  data_.clear();
123  }
124 
125 private:
126  std::vector<T> data_;
127 };
128 
130 template<typename T>
131 concept Integral = std::is_integral_v<T>;
132 template<Integral T>
135 template<typename T>
Definition: columns.h:30
Definition: columns.h:86
Definition: columns.h:41