VR-Forces 4.2 Class Documentation
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 <vlutil/vlString.h>
13 #include <luabind/luabind.hpp>
14 #include <stdexcept>
15 #include <vector>
16 #include <set>
18 {
19 public:
21  {
22  }
23 
24 public:
25  virtual DtVariableParameterInterface* clone() const = 0;
26 
29  virtual void process(const std::string&, luabind::object const& parameters) = 0;
30 
32  virtual bool isValid() const = 0;
33 
35  virtual bool specified() const = 0;
36 };
37 
38 template <typename _Container>
40 {
41 public:
44  , mySpecified(false)
45  {
46  }
47 
49  : myRequired(orig.myRequired)
50  , mySpecified(orig.mySpecified)
51  , myValue(orig.myValue)
52  {
53  }
54 
56  {
57  }
58 
60  {
61  return new DtVariableParameter<_Container>(*this);
62  }
63 
64  virtual void process(const std::string& parameter, luabind::object const& parameters)
65  {
66  luabind::object tableEntry = parameters[parameter];
67  if (tableEntry.is_valid())
68  {
69  if (luabind::type(tableEntry) != LUA_TNIL)
70  {
71  myValue = luabind::object_cast<_Container>(tableEntry);
72  mySpecified = true;
73  }
74  }
75  }
76 
77  const _Container& value() const
78  {
79  return myValue;
80  }
81 
82  void setValue(const _Container& value)
83  {
84  myValue = value;
85  mySpecified = true;
86  }
87 
88  virtual bool specified() const
89  {
90  return mySpecified;
91  }
92 
93  bool required() const
94  {
95  return myRequired;
96  }
97 
98  virtual bool isValid() const
99  {
100  return (!myRequired || (myRequired && mySpecified));
101  }
102 
103 protected:
104  _Container myValue;
107 };
108 
110 template <typename _Container>
111 class DtVariableParameterVector : public DtVariableParameter<std::vector<_Container> >
112 {
113 public:
115  : DtVariableParameter<std::vector<_Container> >(required)
116  {
117  }
118 
120  : DtVariableParameter<std::vector<_Container> >(orig)
121  {
122  }
123 
125  {
126  }
127 
129  {
130  return new DtVariableParameterVector<_Container>(*this);
131  }
132 
133  virtual void process(const std::string& parameter, luabind::object const& parameters)
134  {
135  luabind::object tableEntry = parameters[parameter];
136  if (tableEntry.is_valid())
137  {
138  if (luabind::type(tableEntry) == LUA_TTABLE)
139  {
140  for (luabind::iterator paramItr(tableEntry), end; paramItr != end; ++paramItr)
141  {
142  _Container value = luabind::object_cast<_Container>(*paramItr);
143  this->myValue.push_back(value);
144  }
145 
146  this->mySpecified = true;
147  }
148  }
149  }
150 };
151 
154 {
155 public:
158  {
159  }
160 
163  {
164  }
165 
167  {
168  }
169 
171  {
172  return new DtVariableParameterNumberOrString(*this);
173  }
174 
175  virtual void process(const std::string& parameter, luabind::object const& parameters)
176  {
177  luabind::object tableEntry = parameters[parameter];
178  if (tableEntry.is_valid())
179  {
180  if (luabind::type(tableEntry) == LUA_TNUMBER)
181  {
182  myValue = DtString(luabind::object_cast<double>(tableEntry));
183  mySpecified = true;
184  }
185  else if (luabind::type(tableEntry) != LUA_TNIL)
186  {
187  myValue = DtString(luabind::object_cast<std::string>(tableEntry).c_str());
188  mySpecified = true;
189  }
190  }
191  }
192 };
193 
197 {
198 private:
200 
204 
205 public:
208 
210  virtual ~DtVariableParameterBuilder();
211 
212  void addVariableParameter(const std::string&, const DtVariableParameterInterface&);
213  void removeVariableParameter(const std::string&);
214 
215  template <typename _Container> void addVariableParameter(const std::string& s)
216  {
217  addVariableParameter(s, DtVariableParameter<_Container>());
218  }
219 
220  template <typename _Container> void addRequiredVariableParameter(const std::string& s)
221  {
222  addVariableParameter(s, DtVariableParameter<_Container>(true));
223  }
224 
225  template <typename _Container> void addVariableParameterVector(const std::string& s)
226  {
227  addVariableParameter(s, DtVariableParameterVector<_Container>());
228  }
229 
230  template <typename _Container> void addRequiredVariableParameterVector(const std::string& s)
231  {
232  addVariableParameter(s, DtVariableParameterVector<_Container>(true));
233  }
234 
235  template <typename _Container> _Container value(const std::string& key) const
236  {
237  VariableParameters::const_iterator iter = myVariableParameters.find(key);
238 
239  if (iter != myVariableParameters.end())
240  {
241  return (dynamic_cast<DtVariableParameter<_Container>*>(iter->second)->value());
242  }
243 
244  return _Container();
245  }
246 
247  template <typename _Container> void set(const std::string& key, const _Container& value)
248  {
249  VariableParameters::iterator iter = myVariableParameters.find(key);
250 
251  if (iter != myVariableParameters.end())
252  {
253  dynamic_cast<DtVariableParameter<_Container>*>(iter->second)->setValue(value);
254  }
255  }
256 
258  virtual bool valueIsSpecified(const std::string&) const;
259 
261  template <typename _Container>
262  std::vector<_Container> valueToStdVector(const std::string& key) const
263  {
264  std::vector<_Container> results;
265  luabind::object object = value<luabind::object>(key);
266 
267  if (luabind::type(object) == LUA_TTABLE)
268  {
269  for (luabind::iterator paramItr(object), end; paramItr != end; ++paramItr)
270  {
271  results.push_back(luabind::object_cast<_Container>(*paramItr));
272  }
273  }
274  else if (luabind::type(object) == LUA_TSTRING)
275  {
276  results.push_back(luabind::object_cast<_Container>(object));
277  }
278 
279  return results;
280  }
281 
282  template <typename _Container>
283  std::set<_Container> valueToStdSet(const std::string& key) const
284  {
285  std::set<_Container> results;
286  luabind::object object = value<luabind::object>(key);
287 
288  if (luabind::type(object) == LUA_TTABLE)
289  {
290  for (luabind::iterator paramItr(object), end; paramItr != end; ++paramItr)
291  {
292  results.insert(luabind::object_cast<_Container>(*paramItr));
293  }
294  }
295  else if (luabind::type(object) == LUA_TSTRING)
296  {
297  results.insert(luabind::object_cast<_Container>(object));
298  }
299 
300  return results;
301  }
302 
305  virtual void process(luabind::object const& parameters);
306 
307 protected:
308  typedef std::map<std::string, DtVariableParameterInterface*> VariableParameters;
310 };

Document ID: Generated on Sun Nov 24 19:49:21 EST 2013 from SVN revision 133924
Copyright © 2005-2013 VT MÄK. All Rights Reserved (www.mak.com)