VR-Forces 4.6 Class Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties 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 <vrfLua/luaUtils.h>
15 #include <vlutil/vlString.h>
16 #include <vrfutil/readerWriter.h>
17 #include <vrfutil/rwString.h>
18 #include <vrfutil/rwReal.h>
19 #include <vrfutil/rwInt.h>
22 
23 #include <luabind/luabind.hpp>
24 #include <stdexcept>
25 #include <vector>
26 #include <set>
27 
29 {
30 public:
32  {
33  }
34 
36  {
37  }
38 
39 public:
40  virtual DtVariableParameterInterface* clone() const = 0;
41 
44  virtual void process(const std::string&, luabind::object const& parameters) = 0;
45 
47  virtual bool isValid() const = 0;
48 
50  virtual bool specified() const = 0;
51 };
52 
53 template <typename _Container>
55 {
56 public:
59  , mySpecified(false)
60  {
61  }
62 
64  : myRequired(orig.myRequired)
65  , mySpecified(orig.mySpecified)
66  , myValue(orig.myValue)
67  {
68  }
69 
70  DtVariableParameter(const _Container& orig, bool required = false)
72  , mySpecified(true)
73  , myValue(orig)
74  {
75  }
76 
78  {
79  }
80 
82  {
83  return new DtVariableParameter<_Container>(*this);
84  }
85 
86  virtual void process(const std::string& parameter, luabind::object const& parameters)
87  {
88  luabind::object tableEntry = parameters[parameter];
89  if (tableEntry.is_valid())
90  {
91  if (luabind::type(tableEntry) != LUA_TNIL)
92  {
93  myValue = luabind::object_cast<_Container>(tableEntry);
94  mySpecified = true;
95  }
96  }
97  }
98 
99  const _Container& value() const
100  {
101  return myValue;
102  }
103 
104  void setValue(const _Container& value)
105  {
106  myValue = value;
107  mySpecified = true;
108  }
109 
110  virtual bool specified() const
111  {
112  return mySpecified;
113  }
114 
115  bool required() const
116  {
117  return myRequired;
118  }
119 
120  virtual bool isValid() const
121  {
122  return (!myRequired || (myRequired && mySpecified));
123  }
124 
125 protected:
126  _Container myValue;
129 };
130 
132 template <typename _Container>
133 class DtVariableParameterVector : public DtVariableParameter<std::vector<_Container> >
134 {
135 public:
137  : DtVariableParameter<std::vector<_Container> >(required)
138  {
139  }
140 
142  : DtVariableParameter<std::vector<_Container> >(orig)
143  {
144  }
145 
147  {
148  }
149 
151  {
152  return new DtVariableParameterVector<_Container>(*this);
153  }
154 
155  virtual void process(const std::string& parameter, luabind::object const& parameters)
156  {
157  luabind::object tableEntry = parameters[parameter];
158  if (tableEntry.is_valid())
159  {
160  if (luabind::type(tableEntry) == LUA_TTABLE)
161  {
162  for (luabind::iterator paramItr(tableEntry), end; paramItr != end; ++paramItr)
163  {
164  _Container value = luabind::object_cast<_Container>(*paramItr);
165  this->myValue.push_back(value);
166  }
167 
168  this->mySpecified = true;
169  }
170  }
171  }
172 };
173 
174 
176 template <typename _Container>
178 {
179 public:
181  : DtVariableParameter<_Container*>(required)
182  {
183  this->myValue = 0;
184  }
185 
187  : DtVariableParameter<_Container*>(orig)
188  {
189  if (orig.myValue)
190  {
191  this->myValue = DtReaderWriter::factory()->cloneReaderWriter(orig.myValue->name().c_str(),
192  orig.myValue, 0);
193  }
194  else
195  {
196  this->myValue = 0;
197  }
198  }
199 
201  {
202  delete this->myValue;
203  }
204 
206  {
208  }
209 
210  virtual DtReaderWriter* createRwStructure(DtReaderWriter* parentRw, luabind::object const& tableEntry) const
211  {
212  DtReaderWriter* createdRw = 0;
213  for (luabind::iterator paramItr(tableEntry), end; paramItr != end; ++paramItr)
214  {
215  DtReaderWriterRegistry* parentReg = 0;
216  if(parentRw)
217  {
218  parentReg = &parentRw->registry();
219  }
220 
226  createdRw = DtReaderWriter::factory()->createItem(luabind::object_cast<std::string>(paramItr.key()).c_str(),
227  DtString(""), parentReg);
228 
229  if (createdRw)
230  {
231  DtPropertySetter::setRwFromLua(*paramItr, createdRw);
232  }
233  else
234  {
235  if (luabind::type(*paramItr) == LUA_TSTRING)
236  {
237  createdRw = new DtRwString(luabind::object_cast<std::string>(paramItr.key()).c_str(), parentReg);
238 
239  ((DtRwString&)*createdRw) = luabind::object_cast<std::string>(*paramItr).c_str();
240  }
241  else if (luabind::type(*paramItr) == LUA_TNUMBER)
242  {
243  createdRw = new DtRwReal(luabind::object_cast<std::string>(paramItr.key()).c_str(), parentReg);
244 
245  ((DtRwReal&)*createdRw) = luabind::object_cast<double>(*paramItr);
246  }
247  else if (luabind::type(*paramItr) == LUA_TTABLE)
248  {
249  DtReaderWriter tmpRw("");
250 
251  createRwStructure(&tmpRw, *paramItr);
252 
253  if (tmpRw.registry().registryData().size())
254  {
255  createdRw = tmpRw.registry().registryData()[0]->readerWriter();
256 
257  tmpRw.registry().removeEntry(createdRw);
258 
259  createdRw->setName(luabind::object_cast<std::string>(paramItr.key()).c_str());
260  if(parentRw)
261  {
262  parentRw->registry().addEntry(createdRw->name(), createdRw);
263  }
264  }
265  }
266  }
267  }
268 
269  return createdRw;
270  }
271 
272  virtual void process(const std::string& parameter, luabind::object const& parameters)
273  {
274  delete this->myValue;
275  this->myValue = 0;
276 
277  luabind::object tableEntry = parameters[parameter];
278  if (tableEntry.is_valid())
279  {
280  if (luabind::type(tableEntry) == LUA_TTABLE)
281  {
282  this->myValue = createRwStructure(0, tableEntry);
283 
284  this->mySpecified = true;
285  }
286  }
287  }
288 };
289 
292 {
293 public:
296  {
297  }
298 
301  {
302  }
303 
305  {
306  }
307 
309  {
310  return new DtVariableParameterNumberOrString(*this);
311  }
312 
313  virtual void process(const std::string& parameter, luabind::object const& parameters)
314  {
315  luabind::object tableEntry = parameters[parameter];
316  if (tableEntry.is_valid())
317  {
318  if (luabind::type(tableEntry) == LUA_TNUMBER)
319  {
320  myValue = DtString(luabind::object_cast<double>(tableEntry));
321  mySpecified = true;
322  }
323  else if (luabind::type(tableEntry) != LUA_TNIL)
324  {
325  myValue = DtString(luabind::object_cast<std::string>(tableEntry).c_str());
326  mySpecified = true;
327  }
328  }
329  }
330 };
331 
333 class DtVariableParameterSimObject : public DtVariableParameter < DtScriptVrfObject >
334 {
335 public:
338  {
339  mySpecified = false;
340  }
341 
344  {
345  }
346 
348  {
349  }
350 
352  {
353  return new DtVariableParameterSimObject(*this);
354  }
355 };
356 
360 {
361 private:
363 
367 
368 public:
371 
373  virtual ~DtVariableParameterBuilder();
374 
375  void addVariableParameter(const std::string&, const DtVariableParameterInterface&);
376  void removeVariableParameter(const std::string&);
377 
378  template <typename _Container> void addVariableParameter(const std::string& s)
379  {
380  addVariableParameter(s, DtVariableParameter<_Container>());
381  }
382 
383 
384  template <typename _Container> void addRequiredVariableParameter(const std::string& s)
385  {
386  addVariableParameter(s, DtVariableParameter<_Container>(true));
387  }
388 
389  template <typename _Container> void addVariableParameterVector(const std::string& s)
390  {
391  addVariableParameter(s, DtVariableParameterVector<_Container>());
392  }
393 
394  template <typename _Container> void addRequiredVariableParameterVector(const std::string& s)
395  {
396  addVariableParameter(s, DtVariableParameterVector<_Container>(true));
397  }
398 
399  template <typename _Container> void addVariableParameterReaderWriter(const std::string& s)
400  {
401  addVariableParameter(s, DtVariableParameterReaderWriter<_Container>());
402  }
403 
404  template <typename _Container> void addRequiredVariableParameterReaderWriter(const std::string& s)
405  {
406  addVariableParameter(s, DtVariableParameterReaderWriter<_Container>(true));
407  }
408 
409  template <typename _Container> _Container value(const std::string& key) const
410  {
411  VariableParameters::const_iterator iter = myVariableParameters.find(key);
412 
413  if (iter != myVariableParameters.end())
414  {
415  return (dynamic_cast<DtVariableParameter<_Container>*>(iter->second)->value());
416  }
417 
418  return _Container();
419  }
420 
421  template <typename _Container> _Container* valuePointer(const std::string& key) const
422  {
423  VariableParameters::const_iterator iter = myVariableParameters.find(key);
424 
425  if (iter != myVariableParameters.end())
426  {
427  return (dynamic_cast<DtVariableParameter<_Container*>*>(iter->second)->value());
428  }
429 
430  return 0;
431  }
432 
433  template <typename _Container> void set(const std::string& key, const _Container& value)
434  {
435  VariableParameters::iterator iter = myVariableParameters.find(key);
436 
437  if (iter != myVariableParameters.end())
438  {
439  dynamic_cast<DtVariableParameter<_Container>*>(iter->second)->setValue(value);
440  }
441  }
442 
444  virtual bool valueIsSpecified(const std::string&) const;
445 
447  template <typename _Container>
448  std::vector<_Container> valueToStdVector(const std::string& key) const
449  {
450  std::vector<_Container> results;
451  luabind::object object = value<luabind::object>(key);
452 
453  if (luabind::type(object) == LUA_TTABLE)
454  {
455  for (luabind::iterator paramItr(object), end; paramItr != end; ++paramItr)
456  {
457  if (luabind::type(*paramItr) == LUA_TUSERDATA)
458  {
459  if (DtScriptVrfObject* so = vrfLua::luaTypeCast<DtScriptVrfObject>(*paramItr))
460  {
461  results.push_back(static_cast<_Container>(so->getUUID()));
462  }
463  else
464  {
465  results.push_back(luabind::object_cast<_Container>(*paramItr));
466  }
467  }
468  else
469  {
470  results.push_back(luabind::object_cast<_Container>(*paramItr));
471  }
472  }
473  }
474  else if (luabind::type(object) == LUA_TSTRING)
475  {
476  results.push_back(luabind::object_cast<_Container>(object));
477  }
478  else if (luabind::type(object) == LUA_TUSERDATA)
479  {
480  if (DtScriptVrfObject* so = vrfLua::luaTypeCast<DtScriptVrfObject>(object))
481  {
482  results.push_back(static_cast<_Container>(so->getUUID()));
483  }
484  else
485  {
486  results.push_back(luabind::object_cast<_Container>(object));
487  }
488  }
489 
490  return results;
491  }
492 
493  template <typename _Container>
494  std::set<_Container> valueToStdSet(const std::string& key) const
495  {
496  std::set<_Container> results;
497  luabind::object object = value<luabind::object>(key);
498 
499  if (luabind::type(object) == LUA_TTABLE)
500  {
501  for (luabind::iterator paramItr(object), end; paramItr != end; ++paramItr)
502  {
503  if (luabind::type(*paramItr) == LUA_TUSERDATA)
504  {
505  if (DtScriptVrfObject* so = vrfLua::luaTypeCast<DtScriptVrfObject>(*paramItr))
506  {
507  results.insert(static_cast<_Container>(so->getUUID()));
508  }
509  else
510  {
511  results.insert(luabind::object_cast<_Container>(*paramItr));
512  }
513  }
514  else
515  {
516  results.insert(luabind::object_cast<_Container>(*paramItr));
517  }
518  }
519  }
520  else if (luabind::type(object) == LUA_TSTRING)
521  {
522  results.insert(luabind::object_cast<_Container>(object));
523  }
524  else if (luabind::type(object) == LUA_TUSERDATA)
525  {
526  if (DtScriptVrfObject* so = vrfLua::luaTypeCast<DtScriptVrfObject>(object))
527  {
528  results.insert(static_cast<_Container>(so->getUUID()));
529  }
530  else
531  {
532  results.insert(luabind::object_cast<_Container>(object));
533  }
534  }
535 
536  return results;
537  }
538 
541  virtual void process(luabind::object const& parameters);
542 
543 protected:
544  typedef std::map<std::string, DtVariableParameterInterface*> VariableParameters;
546 };
547 
548 
549 template <>
550 inline void DtVariableParameterBuilder::addVariableParameter<DtScriptVrfObject>(const std::string& s)
551 {
552  addVariableParameter(s, DtVariableParameterSimObject());
553 }
554 
555 template <>
556 inline DtScriptVrfObject DtVariableParameterBuilder::value<DtScriptVrfObject>(const std::string& key) const
557 {
558  VariableParameters::const_iterator iter = myVariableParameters.find(key);
559 
560  if (iter != myVariableParameters.end())
561  {
562  return (dynamic_cast<DtVariableParameter<DtScriptVrfObject>*>(iter->second)->value());
563  }
564 
566 }
567 

Document ID: Generated on Thu Apr 12 03:15:37 EDT 2018 from SVN revision 187986
Copyright © 2005-2018 VT MÄK. All Rights Reserved (www.mak.com)