VR-Forces Development_Version 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 <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 
71  {
72  }
73 
75  {
76  return new DtVariableParameter<_Container>(*this);
77  }
78 
79  virtual void process(const std::string& parameter, luabind::object const& parameters)
80  {
81  luabind::object tableEntry = parameters[parameter];
82  if (tableEntry.is_valid())
83  {
84  if (luabind::type(tableEntry) != LUA_TNIL)
85  {
86  myValue = luabind::object_cast<_Container>(tableEntry);
87  mySpecified = true;
88  }
89  }
90  }
91 
92  const _Container& value() const
93  {
94  return myValue;
95  }
96 
97  void setValue(const _Container& value)
98  {
99  myValue = value;
100  mySpecified = true;
101  }
102 
103  virtual bool specified() const
104  {
105  return mySpecified;
106  }
107 
108  bool required() const
109  {
110  return myRequired;
111  }
112 
113  virtual bool isValid() const
114  {
115  return (!myRequired || (myRequired && mySpecified));
116  }
117 
118 protected:
119  _Container myValue;
122 };
123 
125 template <typename _Container>
126 class DtVariableParameterVector : public DtVariableParameter<std::vector<_Container> >
127 {
128 public:
130  : DtVariableParameter<std::vector<_Container> >(required)
131  {
132  }
133 
135  : DtVariableParameter<std::vector<_Container> >(orig)
136  {
137  }
138 
140  {
141  }
142 
144  {
145  return new DtVariableParameterVector<_Container>(*this);
146  }
147 
148  virtual void process(const std::string& parameter, luabind::object const& parameters)
149  {
150  luabind::object tableEntry = parameters[parameter];
151  if (tableEntry.is_valid())
152  {
153  if (luabind::type(tableEntry) == LUA_TTABLE)
154  {
155  for (luabind::iterator paramItr(tableEntry), end; paramItr != end; ++paramItr)
156  {
157  _Container value = luabind::object_cast<_Container>(*paramItr);
158  this->myValue.push_back(value);
159  }
160 
161  this->mySpecified = true;
162  }
163  }
164  }
165 };
166 
167 
169 template <typename _Container>
171 {
172 public:
174  : DtVariableParameter<_Container*>(required)
175  {
176  this->myValue = 0;
177  }
178 
180  : DtVariableParameter<_Container*>(orig)
181  {
182  if (orig.myValue)
183  {
184  this->myValue = DtReaderWriter::factory()->cloneReaderWriter(orig.myValue->name().c_str(),
185  orig.myValue, 0);
186  }
187  else
188  {
189  this->myValue = 0;
190  }
191  }
192 
194  {
195  delete this->myValue;
196  }
197 
199  {
201  }
202 
203  virtual DtReaderWriter* createRwStructure(DtReaderWriter* parentRw, luabind::object const& tableEntry) const
204  {
205  DtReaderWriter* createdRw = 0;
206  for (luabind::iterator paramItr(tableEntry), end; paramItr != end; ++paramItr)
207  {
208  DtReaderWriterRegistry* parentReg = 0;
209  if(parentRw)
210  {
211  parentReg = &parentRw->registry();
212  }
213 
219  createdRw = DtReaderWriter::factory()->createItem(luabind::object_cast<std::string>(paramItr.key()).c_str(),
220  DtString(""), parentReg);
221 
222  if (createdRw)
223  {
224  DtPropertySetter::setRwFromLua(*paramItr, createdRw);
225  }
226  else
227  {
228  if (luabind::type(*paramItr) == LUA_TSTRING)
229  {
230  DtRwString* createdRw = new DtRwString(luabind::object_cast<std::string>(paramItr.key()).c_str(), parentReg);
231 
232  *createdRw = luabind::object_cast<std::string>(*paramItr).c_str();
233  }
234  else if (luabind::type(*paramItr) == LUA_TNUMBER)
235  {
236  DtRwReal* createdRw = new DtRwReal(luabind::object_cast<std::string>(paramItr.key()).c_str(), parentReg);
237 
238  *createdRw = luabind::object_cast<double>(*paramItr);
239  }
240  else if (luabind::type(*paramItr) == LUA_TTABLE)
241  {
242  DtReaderWriter tmpRw("");
243 
244  createRwStructure(&tmpRw, *paramItr);
245 
246  if (tmpRw.registry().registryData().size())
247  {
248  createdRw = tmpRw.registry().registryData()[0]->readerWriter();
249 
250  tmpRw.registry().removeEntry(createdRw);
251 
252  createdRw->setName(luabind::object_cast<std::string>(paramItr.key()).c_str());
253  if(parentRw)
254  {
255  parentRw->registry().addEntry(createdRw->name(), createdRw);
256  }
257  }
258  }
259  }
260  }
261 
262  return createdRw;
263  }
264 
265  virtual void process(const std::string& parameter, luabind::object const& parameters)
266  {
267  delete this->myValue;
268  this->myValue = 0;
269 
270  luabind::object tableEntry = parameters[parameter];
271  if (tableEntry.is_valid())
272  {
273  if (luabind::type(tableEntry) == LUA_TTABLE)
274  {
275  this->myValue = createRwStructure(0, tableEntry);
276 
277  this->mySpecified = true;
278  }
279  }
280  }
281 };
282 
285 {
286 public:
289  {
290  }
291 
294  {
295  }
296 
298  {
299  }
300 
302  {
303  return new DtVariableParameterNumberOrString(*this);
304  }
305 
306  virtual void process(const std::string& parameter, luabind::object const& parameters)
307  {
308  luabind::object tableEntry = parameters[parameter];
309  if (tableEntry.is_valid())
310  {
311  if (luabind::type(tableEntry) == LUA_TNUMBER)
312  {
313  myValue = DtString(luabind::object_cast<double>(tableEntry));
314  mySpecified = true;
315  }
316  else if (luabind::type(tableEntry) != LUA_TNIL)
317  {
318  myValue = DtString(luabind::object_cast<std::string>(tableEntry).c_str());
319  mySpecified = true;
320  }
321  }
322  }
323 };
324 
328 {
329 private:
331 
335 
336 public:
339 
341  virtual ~DtVariableParameterBuilder();
342 
343  void addVariableParameter(const std::string&, const DtVariableParameterInterface&);
344  void removeVariableParameter(const std::string&);
345 
346  template <typename _Container> void addVariableParameter(const std::string& s)
347  {
348  addVariableParameter(s, DtVariableParameter<_Container>());
349  }
350 
351  template <typename _Container> void addRequiredVariableParameter(const std::string& s)
352  {
353  addVariableParameter(s, DtVariableParameter<_Container>(true));
354  }
355 
356  template <typename _Container> void addVariableParameterVector(const std::string& s)
357  {
358  addVariableParameter(s, DtVariableParameterVector<_Container>());
359  }
360 
361  template <typename _Container> void addRequiredVariableParameterVector(const std::string& s)
362  {
363  addVariableParameter(s, DtVariableParameterVector<_Container>(true));
364  }
365 
366  template <typename _Container> void addVariableParameterReaderWriter(const std::string& s)
367  {
368  addVariableParameter(s, DtVariableParameterReaderWriter<_Container>());
369  }
370 
371  template <typename _Container> void addRequiredVariableParameterReaderWriter(const std::string& s)
372  {
373  addVariableParameter(s, DtVariableParameterReaderWriter<_Container>(true));
374  }
375 
376  template <typename _Container> _Container value(const std::string& key) const
377  {
378  VariableParameters::const_iterator iter = myVariableParameters.find(key);
379 
380  if (iter != myVariableParameters.end())
381  {
382  return (dynamic_cast<DtVariableParameter<_Container>*>(iter->second)->value());
383  }
384 
385  return _Container();
386  }
387 
388  template <typename _Container> _Container* valuePointer(const std::string& key) const
389  {
390  VariableParameters::const_iterator iter = myVariableParameters.find(key);
391 
392  if (iter != myVariableParameters.end())
393  {
394  return (dynamic_cast<DtVariableParameter<_Container*>*>(iter->second)->value());
395  }
396 
397  return 0;
398  }
399 
400  template <typename _Container> void set(const std::string& key, const _Container& value)
401  {
402  VariableParameters::iterator iter = myVariableParameters.find(key);
403 
404  if (iter != myVariableParameters.end())
405  {
406  dynamic_cast<DtVariableParameter<_Container>*>(iter->second)->setValue(value);
407  }
408  }
409 
411  virtual bool valueIsSpecified(const std::string&) const;
412 
414  template <typename _Container>
415  std::vector<_Container> valueToStdVector(const std::string& key) const
416  {
417  std::vector<_Container> results;
418  luabind::object object = value<luabind::object>(key);
419 
420  if (luabind::type(object) == LUA_TTABLE)
421  {
422  for (luabind::iterator paramItr(object), end; paramItr != end; ++paramItr)
423  {
424  if (luabind::type(*paramItr) == LUA_TUSERDATA)
425  {
426  if (DtScriptVrfObject* so = vrfLua::luaTypeCast<DtScriptVrfObject>(*paramItr))
427  {
428  results.push_back(static_cast<_Container>(so->getUUID()));
429  }
430  else
431  {
432  results.push_back(luabind::object_cast<_Container>(*paramItr));
433  }
434  }
435  else
436  {
437  results.push_back(luabind::object_cast<_Container>(*paramItr));
438  }
439  }
440  }
441  else if (luabind::type(object) == LUA_TSTRING)
442  {
443  results.push_back(luabind::object_cast<_Container>(object));
444  }
445  else if (luabind::type(object) == LUA_TUSERDATA)
446  {
447  if (DtScriptVrfObject* so = vrfLua::luaTypeCast<DtScriptVrfObject>(object))
448  {
449  results.push_back(static_cast<_Container>(so->getUUID()));
450  }
451  else
452  {
453  results.push_back(luabind::object_cast<_Container>(object));
454  }
455  }
456 
457  return results;
458  }
459 
460  template <typename _Container>
461  std::set<_Container> valueToStdSet(const std::string& key) const
462  {
463  std::set<_Container> results;
464  luabind::object object = value<luabind::object>(key);
465 
466  if (luabind::type(object) == LUA_TTABLE)
467  {
468  for (luabind::iterator paramItr(object), end; paramItr != end; ++paramItr)
469  {
470  if (luabind::type(*paramItr) == LUA_TUSERDATA)
471  {
472  if (DtScriptVrfObject* so = vrfLua::luaTypeCast<DtScriptVrfObject>(*paramItr))
473  {
474  results.insert(static_cast<_Container>(so->getUUID()));
475  }
476  else
477  {
478  results.insert(luabind::object_cast<_Container>(*paramItr));
479  }
480  }
481  else
482  {
483  results.insert(luabind::object_cast<_Container>(*paramItr));
484  }
485  }
486  }
487  else if (luabind::type(object) == LUA_TSTRING)
488  {
489  results.insert(luabind::object_cast<_Container>(object));
490  }
491  else if (luabind::type(object) == LUA_TUSERDATA)
492  {
493  if (DtScriptVrfObject* so = vrfLua::luaTypeCast<DtScriptVrfObject>(object))
494  {
495  results.insert(static_cast<_Container>(so->getUUID()));
496  }
497  else
498  {
499  results.insert(luabind::object_cast<_Container>(object));
500  }
501  }
502 
503  return results;
504  }
505 
508  virtual void process(luabind::object const& parameters);
509 
510 protected:
511  typedef std::map<std::string, DtVariableParameterInterface*> VariableParameters;
513 };

Document ID: Generated on Tue Mar 8 22:13:38 EST 2016 from SVN revision 162938
Copyright © 2005-2015 VT MÄK. All Rights Reserved (www.mak.com)