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