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(), ", value=", p, " 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 inline
181 void unlink(){
182 this->reset();
183 //this->unlink();
184 }
185
186protected:
187
189 //template <class T2>
190 /*
191 void init(const ReferenceT<T> & ref) {
192 // std::cerr << __FILE__ << ':' << __LINE__ << ':' << __FUNCTION__ << " " << ref << '[' << typeid(T).name() << ']'<< std::endl;
193 std::cerr << __FILE__ << ':' << __LINE__ << ':' << __FUNCTION__ << " " << ref << '[' << TypeName< ReferenceT<T> >::str() << ']' << '[' << TypeName<T>::str() << ']'<< std::endl;
194 this->reset(); // needed?
195 //this->link(ref.getPtr(), ref.getType(), ref.getSize());
196 this->relink(ref);
197
198 //this->assignCastable(value);
199 }
200 */
201
202
203 template <class D>
204 void init(D & dst){
205 this->link(dst);
206 }
207
208
209
210 template <class S>
211 void init(const S & src){
212 std::cerr << __FILE__ << ' ' << __LINE__ << ':' << __FUNCTION__ << " " << src << std::endl;
213
214 T::init(src); // Undefined for Castable -> compile time error.
215 }
216
217
218
219 template <class D>
220 void init(D *dst){
221 this->link(dst);
222 }
223
224 inline
225 void init(void *p, const std::type_info &t, size_t count=1){
226 this->link(p, t, count);
227 }
228
229
230public:
231
232protected:
233
234 // Terminal
235 inline
236 void init(){
237 }
238
239};
240
241template <>
242template <class S>
243inline
244void ReferenceT<VariableBase>::init(const S & src){
245 this->assign(src); // Safe for VariableBase
246}
247
248
249
250
251
252}
253
254#endif
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition Log.h:313
Logger & fail(const TT &... args)
Possible error, but execution can continue. Special type of Logger::warn().
Definition Log.h:454
Logger & error(const TT &... args)
Echoes.
Definition Log.h:417
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:204
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
Default implementation.
Definition Type.h:541