VR-Forces 4.3 Class Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
variableParameterBuilder.h
Go to the documentation of this file.
1 /*******************************************************************************
2 ** Copyright (c) 2012 MAK Technologies, Inc.
3 ** All rights reserved.
4 *******************************************************************************/
5 
8 
9 #pragma once
10 
11 #include <vrfLua/vrfLuaDefines.h>
12 #include <vrfLua/propertySetter.h>
13 #include <vlutil/vlString.h>
14 #include <vrfutil/readerWriter.h>
15 #include <vrfutil/rwString.h>
16 #include <vrfutil/rwReal.h>
17 #include <vrfutil/rwInt.h>
20 
21 #include <luabind/luabind.hpp>
22 #include <stdexcept>
23 #include <vector>
24 #include <set>
25 
27 {
28 public:
30  {
31  }
32 
34  {
35  }
36 
37 public:
38  virtual DtVariableParameterInterface* clone() const = 0;
39 
42  virtual void process(const std::string&, luabind::object const& parameters) = 0;
43 
45  virtual bool isValid() const = 0;
46 
48  virtual bool specified() const = 0;
49 };
50 
51 template <typename _Container>
53 {
54 public:
57  , mySpecified(false)
58  {
59  }
60 
62  : myRequired(orig.myRequired)
63  , mySpecified(orig.mySpecified)
64  , myValue(orig.myValue)
65  {
66  }
67 
69  {
70  }
71 
73  {
74  return new DtVariableParameter<_Container>(*this);
75  }
76 
77  virtual void process(const std::string& parameter, luabind::object const& parameters)
78  {
79  luabind::object tableEntry = parameters[parameter];
80  if (tableEntry.is_valid())
81  {
82  if (luabind::type(tableEntry) != LUA_TNIL)
83  {
84  myValue = luabind::object_cast<_Container>(tableEntry);
85  mySpecified = true;
86  }
87  }
88  }
89 
90  const _Container& value() const
91  {
92  return myValue;
93  }
94 
95  void setValue(const _Container& value)
96  {
97  myValue = value;
98  mySpecified = true;
99  }
100 
101  virtual bool specified() const
102  {
103  return mySpecified;
104  }
105 
106  bool required() const
107  {
108  return myRequired;
109  }
110 
111  virtual bool isValid() const
112  {
113  return (!myRequired || (myRequired && mySpecified));
114  }
115 
116 protected:
117  _Container myValue;
120 };
121 
123 template <typename _Container>
124 class DtVariableParameterVector : public DtVariableParameter<std::vector<_Container> >
125 {
126 public:
128  : DtVariableParameter<std::vector<_Container> >(required)
129  {
130  }
131 
133  : DtVariableParameter<std::vector<_Container> >(orig)
134  {
135  }
136 
138  {
139  }
140 
142  {
143  return new DtVariableParameterVector<_Container>(*this);
144  }
145 
146  virtual void process(const std::string& parameter, luabind::object const& parameters)
147  {
148  luabind::object tableEntry = parameters[parameter];
149  if (tableEntry.is_valid())
150  {
151  if (luabind::type(tableEntry) == LUA_TTABLE)
152  {
153  for (luabind::iterator paramItr(tableEntry), end; paramItr != end; ++paramItr)
154  {
155  _Container value = luabind::object_cast<_Container>(*paramItr);
156  this->myValue.push_back(value);
157  }
158 
159  this->mySpecified = true;
160  }
161  }
162  }
163 };
164 
165 
167 template <typename _Container>
169 {
170 public:
172  : DtVariableParameter<_Container*>(required)
173  {
174  this->myValue = 0;
175  }
176 
178  : DtVariableParameter<_Container*>(orig)
179  {
180  if (orig.myValue)
181  {
182  this->myValue = DtReaderWriter::factory()->cloneReaderWriter(orig.myValue->name().c_str(),
183  orig.myValue, 0);
184  }
185  else
186  {
187  this->myValue = 0;
188  }
189  }
190 
192  {
193  delete this->myValue;
194  }
195 
197  {
199  }
200 
201  virtual DtReaderWriter* createRwStructure(DtReaderWriter* parentRw, luabind::object const& tableEntry) const
202  {
203  DtReaderWriter* createdRw = 0;
204  for (luabind::iterator paramItr(tableEntry), end; paramItr != end; ++paramItr)
205  {
206  DtReaderWriterRegistry* parentReg = 0;
207  if(parentRw)
208  {
209  parentReg = &parentRw->registry();
210  }
211 
217  createdRw = DtReaderWriter::factory()->createItem(luabind::object_cast<std::string>(paramItr.key()).c_str(),
218  DtString(""), parentReg);
219 
220  if (createdRw)
221  {
222  DtPropertySetter::setRwFromLua(*paramItr, createdRw);
223  }
224  else
225  {
226  if (luabind::type(*paramItr) == LUA_TSTRING)
227  {
228  DtRwString* createdRw = new DtRwString(luabind::object_cast<std::string>(paramItr.key()).c_str(), parentReg);
229 
230  *createdRw = luabind::object_cast<std::string>(*paramItr).c_str();
231  }
232  else if (luabind::type(*paramItr) == LUA_TNUMBER)
233  {
234  DtRwReal* createdRw = new DtRwReal(luabind::object_cast<std::string>(paramItr.key()).c_str(), parentReg);
235 
236  *createdRw = luabind::object_cast<double>(*paramItr);
237  }
238  else if (luabind::type(*paramItr) == LUA_TTABLE)
239  {
240  DtReaderWriter tmpRw("");
241 
242  createRwStructure(&tmpRw, *paramItr);
243 
244  if (tmpRw.registry().registryData().size())
245  {
246  createdRw = tmpRw.registry().registryData()[0]->readerWriter();
247 
248  tmpRw.registry().removeEntry(createdRw);
249 
250  createdRw->setName(luabind::object_cast<std::string>(paramItr.key()).c_str());
251  if(parentRw)
252  {
253  parentRw->registry().addEntry(createdRw->name(), createdRw);
254  }
255  }
256  }
257  }
258  }
259 
260  return createdRw;
261  }
262 
263  virtual void process(const std::string& parameter, luabind::object const& parameters)
264  {
265  delete this->myValue;
266  this->myValue = 0;
267 
268  luabind::object tableEntry = parameters[parameter];
269  if (tableEntry.is_valid())
270  {
271  if (luabind::type(tableEntry) == LUA_TTABLE)
272  {
273  this->myValue = createRwStructure(0, tableEntry);
274 
275  this->mySpecified = true;
276  }
277  }
278  }
279 };
280 
283 {
284 public:
287  {
288  }
289 
292  {
293  }
294 
296  {
297  }
298 
300  {
301  return new DtVariableParameterNumberOrString(*this);
302  }
303 
304  virtual void process(const std::string& parameter, luabind::object const& parameters)
305  {
306  luabind::object tableEntry = parameters[parameter];
307  if (tableEntry.is_valid())
308  {
309  if (luabind::type(tableEntry) == LUA_TNUMBER)
310  {
311  myValue = DtString(luabind::object_cast<double>(tableEntry));
312  mySpecified = true;
313  }
314  else if (luabind::type(tableEntry) != LUA_TNIL)
315  {
316  myValue = DtString(luabind::object_cast<std::string>(tableEntry).c_str());
317  mySpecified = true;
318  }
319  }
320  }
321 };
322 
326 {
327 private:
329 
333 
334 public:
337 
339  virtual ~DtVariableParameterBuilder();
340 
341  void addVariableParameter(const std::string&, const DtVariableParameterInterface&);
342  void removeVariableParameter(const std::string&);
343 
344  template <typename _Container> void addVariableParameter(const std::string& s)
345  {
346  addVariableParameter(s, DtVariableParameter<_Container>());
347  }
348 
349  template <typename _Container> void addRequiredVariableParameter(const std::string& s)
350  {
351  addVariableParameter(s, DtVariableParameter<_Container>(true));
352  }
353 
354  template <typename _Container> void addVariableParameterVector(const std::string& s)
355  {
356  addVariableParameter(s, DtVariableParameterVector<_Container>());
357  }
358 
359  template <typename _Container> void addRequiredVariableParameterVector(const std::string& s)
360  {
361  addVariableParameter(s, DtVariableParameterVector<_Container>(true));
362  }
363 
364  template <typename _Container> void addVariableParameterReaderWriter(const std::string& s)
365  {
366  addVariableParameter(s, DtVariableParameterReaderWriter<_Container>());
367  }
368 
369  template <typename _Container> void addRequiredVariableParameterReaderWriter(const std::string& s)
370  {
371  addVariableParameter(s, DtVariableParameterReaderWriter<_Container>(true));
372  }
373 
374  template <typename _Container> _Container value(const std::string& key) const
375  {
376  VariableParameters::const_iterator iter = myVariableParameters.find(key);
377 
378  if (iter != myVariableParameters.end())
379  {
380  return (dynamic_cast<DtVariableParameter<_Container>*>(iter->second)->value());
381  }
382 
383  return _Container();
384  }
385 
386  template <typename _Container> _Container* valuePointer(const std::string& key) const
387  {
388  VariableParameters::const_iterator iter = myVariableParameters.find(key);
389 
390  if (iter != myVariableParameters.end())
391  {
392  return (dynamic_cast<DtVariableParameter<_Container*>*>(iter->second)->value());
393  }
394 
395  return 0;
396  }
397 
398  template <typename _Container> void set(const std::string& key, const _Container& value)
399  {
400  VariableParameters::iterator iter = myVariableParameters.find(key);
401 
402  if (iter != myVariableParameters.end())
403  {
404  dynamic_cast<DtVariableParameter<_Container>*>(iter->second)->setValue(value);
405  }
406  }
407 
409  virtual bool valueIsSpecified(const std::string&) const;
410 
412  template <typename _Container>
413  std::vector<_Container> valueToStdVector(const std::string& key) const
414  {
415  std::vector<_Container> results;
416  luabind::object object = value<luabind::object>(key);
417 
418  if (luabind::type(object) == LUA_TTABLE)
419  {
420  for (luabind::iterator paramItr(object), end; paramItr != end; ++paramItr)
421  {
422  results.push_back(luabind::object_cast<_Container>(*paramItr));
423  }
424  }
425  else if (luabind::type(object) == LUA_TSTRING)
426  {
427  results.push_back(luabind::object_cast<_Container>(object));
428  }
429 
430  return results;
431  }
432 
433  template <typename _Container>
434  std::set<_Container> valueToStdSet(const std::string& key) const
435  {
436  std::set<_Container> results;
437  luabind::object object = value<luabind::object>(key);
438 
439  if (luabind::type(object) == LUA_TTABLE)
440  {
441  for (luabind::iterator paramItr(object), end; paramItr != end; ++paramItr)
442  {
443  results.insert(luabind::object_cast<_Container>(*paramItr));
444  }
445  }
446  else if (luabind::type(object) == LUA_TSTRING)
447  {
448  results.insert(luabind::object_cast<_Container>(object));
449  }
450 
451  return results;
452  }
453 
456  virtual void process(luabind::object const& parameters);
457 
458 protected:
459  typedef std::map<std::string, DtVariableParameterInterface*> VariableParameters;
461 };

Document ID: Generated on Wed Mar 11 21:20:57 EDT 2015 from SVN revision 150940
Copyright © 2005-2014 VT MÄK. All Rights Reserved (www.mak.com)