ReferenceT.h
1 /*
2 
3 MIT License
4 
5 Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24 
25 */
26 /*
27 Part of Rack development has been done in the BALTRAD projects part-financed
28 by the European Union (European Regional Development Fund and European
29 Neighbourhood 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 
41 namespace drain {
42 
43 //class Variable;
44 
46 
62 template <class T> // =Castable
63 class ReferenceT : public T {
64 
65 public:
66 
67  typedef const drain::ReferenceT<T> reference_t;
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
133  ReferenceT & link(F &p){
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
152  ReferenceT & link(F *p){
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 
181 protected:
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 
225 public:
226 
227 protected:
228 
229  // Terminal
230  inline
231  void init(){
232  }
233 
234 };
235 
236 template <>
237 template <class S>
238 inline
239 void 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:308
Logger & error(const TT &... args)
Echoes.
Definition: Log.h:412
Logger & fail(const TT &... args)
Possible error, but execution can continue. Special type of Logger::warn().
Definition: Log.h:449
Intermediate class supporting link() in various forms.
Definition: ReferenceT.h:63
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(void *p, const std::type_info &t, size_t count=1)
Set pointer to p, of given type.
Definition: ReferenceT.h:159
ReferenceT & link(F &p)
Set pointer to &p.
Definition: ReferenceT.h:133
virtual bool isLinkable() const
Tells if the internal pointer can point to an external variable.
Definition: ReferenceT.h:76
ReferenceT & link(F *p)
Set pointer to &p.
Definition: ReferenceT.h:152
ReferenceT & link(VariableT< VariableInitializer< ReferenceT< VariableBase > > > &x)
Linkage for a FlexibleVariable.
Definition: ReferenceT.h:102
void init(D &dst)
Copy constructor handler - important.
Definition: ReferenceT.h:199
ReferenceT & link(VariableT< ReferenceT< Castable > > &x)
Linkage for Reference, above all for copy constructor.
Definition: ReferenceT.h:95
Definition: VariableBase.h:246
VariableT is a final class applied through typedefs Variable, Reference and FlexibleVariable.
Definition: VariableT.h:87
Definition: DataSelector.cpp:1277
Definition: Type.h:542