idol
A C++ Framework for Optimization
Loading...
Searching...
No Matches
Object.h
1//
2// Created by henri on 27/01/23.
3//
4
5#ifndef IDOL_OBJECT_H
6#define IDOL_OBJECT_H
7
8#include <memory>
9#include "ObjectId.h"
10#include "idol/general/utils/Vector.h"
11#include "idol/general/utils/Pair.h"
12#include "idol/mixed-integer/modeling/annotations/Annotation.h"
13#include "idol/general/utils/exceptions/Exception.h"
14
15namespace idol {
16 class Model;
17
18 template<class T, class CRTP>
19 class Object;
20
21 class Var;
22 class Ctr;
23}
24
25template<class T, class CRTP>
27 std::shared_ptr<ObjectId<T>> m_object_id;
28protected:
29 [[nodiscard]] auto& versions() { return m_object_id->versions(); }
30
31 [[nodiscard]] const auto& versions() const { return m_object_id->versions(); }
32
33 template<class ...ArgsT>
34 void create_version(const Model& t_model, unsigned int t_index, ArgsT&& ...t_args) const {
35 m_object_id->versions().create(t_model, t_index, std::forward<ArgsT>(t_args)...);
36 }
37
38 void remove_version(const Model& t_model) const {
39 m_object_id->versions().remove(t_model);
40 }
41public:
42 explicit Object(ObjectId<T>&& t_object_id) : m_object_id(std::make_shared<ObjectId<T>>(std::move(t_object_id))) {}
43
44 Object(const Object&) = default;
45 Object(Object&&) noexcept = default;
46
47 Object& operator=(const Object&) = default;
48 Object& operator=(Object&&) noexcept = default;
49
54 [[nodiscard]] const std::string& name() const { return m_object_id->name(); }
55
60 [[nodiscard]] unsigned int id() const { return m_object_id->id(); }
61
67 [[nodiscard]] bool is_in(const Model& t_model) const { return m_object_id->versions().has(t_model); }
68
78 template<class ValueT> const ValueT& get(const Annotation<ValueT>& t_annotation) const {
79 const auto& result = m_object_id->versions().template get_annotation<ValueT>(t_annotation.id());
80 if (result) {
81 return *result;
82 }
83 if (t_annotation.has_default()) {
84 return t_annotation.default_value();
85 }
86 throw Exception("No value could be found and no default value was given for annotation " + t_annotation.name());
87 }
88
96 template<class ValueT, class ...ArgsT> void set(const Annotation<ValueT>& t_annotation, ArgsT&& ...t_args) const { m_object_id->versions().template set_annotation<ValueT, ArgsT...>(t_annotation.id(), std::forward<ArgsT>(t_args)...); }
97};
98
99namespace idol {
100
101 template<class T, class CRTP>
102 static std::ostream &operator<<(std::ostream &t_os, const Object<T, CRTP> &t_var) {
103 return t_os << t_var.name();
104 }
105
106}
107
108namespace idol::impl {
109
110 template<class U, unsigned int N, unsigned int I = 0>
111 static ::idol::Vector<U, N - I> create_many(const Dim<N>& t_dims, const std::string& t_name, const std::function<U(const std::string& t_name)>& t_add_one) {
112 ::idol::Vector<U, N - I> result;
113 const unsigned int size = t_dims[I];
114 result.reserve(size);
115 for (unsigned int i = 0 ; i < size ; ++i) {
116 const std::string name = t_name + "_" + std::to_string(i);
117 if constexpr (I == N - 1) {
118 result.emplace_back( t_add_one(name) );
119 } else {
120 result.emplace_back( create_many<U, N, I+1>(t_dims, name, t_add_one) );
121 }
122 }
123 return result;
124 }
125
126}
127
128#define IDOL_MAKE_HASHABLE(name) \
129template<> \
130struct std::hash<idol::name> { \
131 std::size_t operator()(const idol::name& t_variable) const { \
132 return std::hash<unsigned int>()(t_variable.id()); \
133 } \
134}; \
135\
136template<> \
137struct std::equal_to<idol::name> { \
138 std::size_t operator()(const idol::name& t_a, const idol::name& t_b) const { \
139 return t_a.id() == t_b.id(); \
140 } \
141}; \
142template<> \
143struct std::less<idol::name> { \
144 std::size_t operator()(const idol::name& t_a, const idol::name& t_b) const { \
145 return t_a.id() < t_b.id(); \
146 } \
147}; \
148template<> \
149struct std::equal_to<idol::Pair<idol::name, idol::name>> { \
150 std::size_t operator()(const idol::Pair<idol::name, idol::name>& t_a, const idol::Pair<idol::name, idol::name>& t_b) const { \
151 return std::equal_to<idol::name>()(t_a.first, t_b.first) && std::equal_to<idol::name>()(t_a.second, t_b.second); \
152 } \
153};
154
155#endif //IDOL_OBJECT_H
const ValueT & get(const Annotation< ValueT > &t_annotation) const
Definition Object.h:78
void set(const Annotation< ValueT > &t_annotation, ArgsT &&...t_args) const
Definition Object.h:96
const std::string & name() const
Definition Object.h:54
bool is_in(const Model &t_model) const
Definition Object.h:67
unsigned int id() const
Definition Object.h:60