alpaqa 1.0.0a18
Nonconvex constrained optimization
Loading...
Searching...
No Matches
structs.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <any>
4#include <map>
5#include <string_view>
6#include <type_traits>
7#include <typeinfo>
8
9namespace alpaqa::params {
10
11/// Function wrapper to set attributes of a struct, type-erasing the type of the
12/// attribute.
13template <class S>
15
16/// Like std::any, but storing just the pointer, without any dynamic allocation.
17class any_ptr {
18 public:
19 any_ptr() = default;
20 template <class T>
24
25 template <class T>
26 T *cast() const {
27 if (!ptr_type)
28 return nullptr;
29 if (typeid(T) != *ptr_type)
30 throw std::bad_any_cast();
31 if (std::is_const_v<T> != is_const)
32 throw std::bad_any_cast();
33 return reinterpret_cast<T *>(ptr);
34 }
35
36 private:
37 void *ptr = nullptr;
38 const std::type_info *ptr_type = nullptr;
39 bool is_const = true;
40};
41
42/// Dictionary that maps struct attribute names to type-erased functions that
43/// set those attributes.
44template <class S>
45using attribute_table_t = std::map<std::string_view, attribute_accessor<S>>;
46
47/// Specialize this type to define the attribute name to attribute setters
48/// dictionaries for a struct type @p T.
49template <class T, class S>
51
52/// Helper macro to easily specialize @ref alpaqa::params::attribute_table.
53#define PARAMS_TABLE(type_, ...) \
54 template <class S> \
55 struct attribute_table<type_, S> { \
56 using type = type_; \
57 inline static const attribute_table_t<S> table{__VA_ARGS__}; \
58 }
59
60/// Helper macro to easily initialize a
61/// @ref alpaqa::params::attribute_table_t.
62#define PARAMS_MEMBER(name, ...) \
63 { \
64 #name, attribute_accessor<S>::template make<type>(&type::name, \
65 __VA_ARGS__) \
66 }
67
68/// Dictionary that maps struct attribute names to type-erased functions that
69/// set those attributes.
70template <class S>
71using attribute_alias_table_t = std::map<std::string_view, std::string_view>;
72
73/// Specialize this type to define the alternative attribute name to attribute
74/// setters dictionaries for a struct type @p T.
75template <class T, class S>
77
78/// Helper macro to easily specialize @ref alpaqa::params::attribute_alias_table.
79#define PARAMS_ALIAS_TABLE(type_, ...) \
80 template <class S> \
81 struct attribute_alias_table<type_, S> { \
82 using type = type_; \
83 inline static const attribute_alias_table_t<S> table{__VA_ARGS__}; \
84 }
85
86/// Helper macro to easily initialize a
87/// @ref alpaqa::params::attribute_alias_table_t.
88#define PARAMS_MEMBER_ALIAS(alias, name) \
89 { #alias, #name }
90
91/// Function wrapper access the enumerators of an enum, type-erasing the type of
92/// the enum.
93template <class T, class S>
95
96/// Dictionary that maps enumerator names to their values and documentation.
97template <class T, class S>
98using enum_table_t = std::map<std::string_view, enum_accessor<T, S>>;
99
100/// Specialize this type to define the enumerator name to value dictionaries for
101/// an enum type @p T.
102template <class T, class S>
104
105/// Helper macro to easily specialize @ref alpaqa::params::enum_table.
106#define ENUM_TABLE(type_, ...) \
107 template <class S> \
108 struct enum_table<type_, S> { \
109 using type = type_; \
110 inline static const enum_table_t<type, S> table{__VA_ARGS__}; \
111 }
112
113/// Helper macro to easily initialize a
114/// @ref alpaqa::params::enum_table_t.
115#define ENUM_MEMBER(name, ...) \
116 { \
117 #name, { type::name, __VA_ARGS__ } \
118 }
119
120} // namespace alpaqa::params
Like std::any, but storing just the pointer, without any dynamic allocation.
Definition structs.hpp:17
const std::type_info * ptr_type
Definition structs.hpp:38
std::map< std::string_view, std::string_view > attribute_alias_table_t
Dictionary that maps struct attribute names to type-erased functions that set those attributes.
Definition structs.hpp:71
std::map< std::string_view, attribute_accessor< S > > attribute_table_t
Dictionary that maps struct attribute names to type-erased functions that set those attributes.
Definition structs.hpp:45
std::map< std::string_view, enum_accessor< T, S > > enum_table_t
Dictionary that maps enumerator names to their values and documentation.
Definition structs.hpp:98
Function wrapper to set attributes of a struct, type-erasing the type of the attribute.
Definition structs.hpp:14
Specialize this type to define the alternative attribute name to attribute setters dictionaries for a...
Definition structs.hpp:76
Specialize this type to define the attribute name to attribute setters dictionaries for a struct type...
Definition structs.hpp:50
Function wrapper access the enumerators of an enum, type-erasing the type of the enum.
Definition structs.hpp:94
Specialize this type to define the enumerator name to value dictionaries for an enum type T.
Definition structs.hpp:103
constexpr const auto inf
Definition config.hpp:112