Itasca C++ Interface
Loading...
Searching...
No Matches
type_selector.h
1#pragma once
2
3namespace itasca
4{
16 template<bool condition, typename if_true, typename if_false>
18 {
19 static const bool result;
20 };
21
28 template<typename if_true, typename if_false>
29 struct type_selector<true, if_true, if_false>
30 {
32 typedef if_true type;
34 static const bool result = true;
35 };
36
43 template<typename if_true, typename if_false>
44 class type_selector<false, if_true, if_false>
45 {
47 typedef if_false type;
49 static const bool result = false;
50 };
51
58 template<bool condition, typename T>
59 class conditional_container: public type_selector<condition, T, T *> {};
60
61 template<typename T>
62 class conditional_container<true, T>
63 {
64 public:
66 conditional_container(const T &t) : value_(t) {}
67
68 conditional_container(const conditional_container<true, T> & other) : value_(other.value_) {}
69 conditional_container(const conditional_container<false, T> & other); // needs to be defined after false
70
71 void operator= (const T & t) { value_ = t; }
72 void operator= (T && t) { value_ = std::move(t); }
73
74 void operator= (const conditional_container<true, T> & other) { value_ = other.value(); }
75 void operator= (const conditional_container<false, T> & other); // see above
76
77 T & value () { return value_; }
78 const T & value () const { return value_; }
79
80 static inline bool isValue () { return true; }
81 private:
82 T value_;
83 };
84
85 template<typename T>
86 class conditional_container<false, T>
87 {
88 public:
89 conditional_container() { value_ = new T; }
90 conditional_container(const T & t) : value_(nullptr) { value_ = new T(t); }
91 conditional_container(T && t) : value_(nullptr) { value_ = new T(std::move(t)); }
92
93 conditional_container(const conditional_container<true, T> & other) { value_ = new T(other.value()); }
94 conditional_container(const conditional_container<false, T> & other) { value_ = new T(other.value()); }
96 {
97 value_ = other.value_;
98 other.value_ = nullptr;
99 }
100
101 ~conditional_container() { if ( value_ != nullptr ) { delete value_; } }
102
103 void operator= (const T & t) { *value_ = t; }
104 void operator= (T && t) { *value_ = std::move(t); }
105
106 void operator= (const conditional_container<true, T> & other) { *value_ = other.value(); }
107 void operator= (const conditional_container<false, T> & other) { *value_ = other.value(); }
108 void operator= (conditional_container<false, T> && other)
109 {
110 T * temp = value_;
111 value_ = other.value_;
112 other.value_ = temp;
113 }
114
115 T & value () { return *value_; }
116 const T & value () const { return *value_; }
117
118 static inline bool isValue () { return false; }
119 private:
120 T * value_;
121 };
122
123 template<typename T>
124 conditional_container<true, T>::conditional_container (const conditional_container<false, T> & other) { value_ = other.conditional_container<false, T>::value(); }
125
126 template<typename T>
127 void conditional_container<true, T>::operator= (const conditional_container<false, T> & other) { value_ = other.conditional_container<false, T>::value(); }
128}
A container, which either stores its value as a value or a pointer depending on the condition.
Definition type_selector.h:59
namespace Itasca
Definition basememory.cpp:10
if_true type
The actual "return value" of the type_selector.
Definition type_selector.h:32
Class used to select between two types based on a given condition.
Definition type_selector.h:18