Loading...
Searching...
No Matches
ReferenceT.h
1/*
2
3MIT License
4
5Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26/*
27Part of Rack development has been done in the BALTRAD projects part-financed
28by the European Union (European Regional Development Fund and European
29Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30*/
31
32
33
34#ifndef DRAIN_REFERENCE_T
35#define DRAIN_REFERENCE_T
36
37#include "Log.h"
38#include "VariableBase.h"
39// #include "Castable.h" // not really needed
40
41namespace drain {
42
43//class Variable;
44
46
62template <class T> // =Castable
63class ReferenceT : public T {
64
65public:
66
68
69 typedef std::pair<const char *,const drain::ReferenceT<T> > init_pair_t;
70
72
75 virtual inline
76 bool isLinkable() const {
77 return true;
78 }
79
80
81 template <class T2>
82 inline
83 ReferenceT & link(ReferenceT<T2> &x){
84 this->relink(x);
85 return *this;
86 }
87
88 // "Originals"
89 // ReferenceT & link(VariableT<T2> &x){
90 //template <class T2>
91 // VariableT<VariableInitializer<ReferenceT<VariableBase> > >
92
94 inline
96 this->relink(x);
97 return *this;
98 }
99
101 inline
103 if (x.isLinking()){
104 // Valid for 1) FlexibleVariable that is 2) linking external variable.
105 // drain::Logger(__FILE__, __LINE__, __FUNCTION__).success<LOG_WARNING>("accessing external data (", x, ") of ", drain::TypeName< VariableT<VariableInitializer<ReferenceT<VariableBase> > > >::str());
106 this->relink(x);
107 }
108 else {
109 //drain::Logger(__FILE__, __LINE__, __FUNCTION__).fail(drain::TypeName< VariableT<T2> >::str(), ' ', x, ": linking internal data forbidden");
110 drain::Logger(__FILE__, __LINE__, __FUNCTION__).fail("accessing internal data (", x, ") of ", drain::TypeName< VariableT<VariableInitializer<ReferenceT<VariableBase> > > >::str(), " forbidden");
111 this->reset();
112 }
113 return *this;
114 }
115
117 inline
119 // drain::TypeName<VariableT<ReferenceT > >::str(),
120 drain::Logger(__FILE__, __LINE__, __FUNCTION__).fail(drain::TypeName< VariableT<VariableInitializer<VariableBase> > >::str(), ' ', x, ": linking internal data forbidden");
121 this->reset();
122 return *this;
123 }
124
125
126
128
131 template <class F>
132 inline
134 try {
135 this->setPtr(p);
136 }
137 catch (const std::exception & e){
138 //Logger(__FILE__, __LINE__, __FUNCTION__).error("unsupported type: ", drain::TypeName<F>::str()); // , " msg:", e.what()
139 Logger(__FILE__, __LINE__, __FUNCTION__).error("unsupported type: ", typeid(F).name(), " msg:", e.what());
140 // std::cerr << __FILE__ << ':' << __FUNCTION__ << ": unsupported type: " << typeid(F).name() << std::endl;
141 // throw std::runtime_error("unsupported type");
142 }
143 return *this;
144 }
145
147
150 template <class F>
151 inline
153 this->setPtr(p);
154 return *this;
155 }
156
158 inline
159 ReferenceT & link(void *p, const std::type_info &t, size_t count=1){
160 this->setPtr(p, t);
161 this->elementCount = count;
162 // Why not this->setPtr(p, t, count); ?
163 return *this;
164 }
165
166 // What about link(void *p){
167 inline
168 ReferenceT & link(void *p){
169 throw std::runtime_error(std::string(__FILE__) + __FUNCTION__ + ": void type unsupported");
170 return *this;
171 }
172
173 //template <class F>
174 inline
175 ReferenceT & link(Castable &x){
176 this->relink(x);
177 return *this;
178 }
179
180
181protected:
182
184 //template <class T2>
185 /*
186 void init(const ReferenceT<T> & ref) {
187 // std::cerr << __FILE__ << ':' << __LINE__ << ':' << __FUNCTION__ << " " << ref << '[' << typeid(T).name() << ']'<< std::endl;
188 std::cerr << __FILE__ << ':' << __LINE__ << ':' << __FUNCTION__ << " " << ref << '[' << TypeName< ReferenceT<T> >::str() << ']' << '[' << TypeName<T>::str() << ']'<< std::endl;
189 this->reset(); // needed?
190 //this->link(ref.getPtr(), ref.getType(), ref.getSize());
191 this->relink(ref);
192
193 //this->assignCastable(value);
194 }
195 */
196
197
198 template <class D>
199 void init(D & dst){
200 this->link(dst);
201 }
202
203
204
205 template <class S>
206 void init(const S & src){
207 std::cerr << __FILE__ << ' ' << __LINE__ << ':' << __FUNCTION__ << " " << src << std::endl;
208
209 T::init(src); // Undefined for Castable -> compile time error.
210 }
211
212
213
214 template <class D>
215 void init(D *dst){
216 this->link(dst);
217 }
218
219 inline
220 void init(void *p, const std::type_info &t, size_t count=1){
221 this->link(p, t, count);
222 }
223
224
225public:
226
227protected:
228
229 // Terminal
230 inline
231 void init(){
232 }
233
234};
235
236template <>
237template <class S>
238inline
239void ReferenceT<VariableBase>::init(const S & src){
240 this->assign(src); // Safe for VariableBase
241}
242
243
244
245
246
247}
248
249#endif
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition Log.h:312
Logger & fail(const TT &... args)
Possible error, but execution can continue. Special type of Logger::warn().
Definition Log.h:453
Logger & error(const TT &... args)
Echoes.
Definition Log.h:416
Intermediate class supporting link() in various forms.
Definition ReferenceT.h:63
ReferenceT & link(VariableT< VariableInitializer< ReferenceT< VariableBase > > > &x)
Linkage for a FlexibleVariable.
Definition ReferenceT.h:102
ReferenceT & link(void *p, const std::type_info &t, size_t count=1)
Set pointer to p, of given type.
Definition ReferenceT.h:159
ReferenceT & link(VariableT< VariableInitializer< VariableBase > > &x)
Linkage for a Variable - produces a warning, because internal variable has dynamic type.
Definition ReferenceT.h:118
ReferenceT & link(F *p)
Set pointer to &p.
Definition ReferenceT.h:152
virtual bool isLinkable() const
Tells if the internal pointer can point to an external variable.
Definition ReferenceT.h:76
void init(D &dst)
Copy constructor handler - important.
Definition ReferenceT.h:199
ReferenceT & link(F &p)
Set pointer to &p.
Definition ReferenceT.h:133
ReferenceT & link(VariableT< ReferenceT< Castable > > &x)
Linkage for Reference, above all for copy constructor.
Definition ReferenceT.h:95
Definition VariableBase.h:255
VariableT is a final class applied through typedefs Variable, Reference and FlexibleVariable.
Definition VariableT.h:87
Definition DataSelector.cpp:1277
Definition Type.h:542