VR-Forces 4.1 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 
17 {
18 public:
20  {
21  }
22 
23 public:
24  virtual DtVariableParameterInterface* clone() const = 0;
25 
28  virtual void process(const std::string&, luabind::object const& parameters) = 0;
29 
31  virtual bool isValid() const = 0;
32 
34  virtual bool specified() const = 0;
35 };
36 
37 template <typename _Container>
39 {
40 public:
43  , mySpecified(false)
44  {
45  }
46 
48  : myRequired(orig.myRequired)
49  , mySpecified(orig.mySpecified)
50  , myValue(orig.myValue)
51  {
52  }
53 
55  {
56  }
57 
59  {
60  return new DtVariableParameter<_Container>(*this);
61  }
62 
63  virtual void process(const std::string& parameter, luabind::object const& parameters)
64  {
65  luabind::object tableEntry = parameters[parameter];
66  if (tableEntry.is_valid())
67  {
68  if (luabind::type(tableEntry) != LUA_TNIL)
69  {
70  myValue = luabind::object_cast<_Container>(tableEntry);
71  mySpecified = true;
72  }
73  }
74  }
75 
76  const _Container& value() const
77  {
78  return myValue;
79  }
80 
81  void setValue(const _Container& value)
82  {
83  myValue = value;
84  mySpecified = true;
85  }
86 
87  virtual bool specified() const
88  {
89  return mySpecified;
90  }
91 
92  bool required() const
93  {
94  return myRequired;
95  }
96 
97  virtual bool isValid() const
98  {
99  return (!myRequired || (myRequired && mySpecified));
100  }
101 
102 protected:
103  _Container myValue;
106 };
107 
109 template <typename _Container>
110 class DtVariableParameterVector : public DtVariableParameter<std::vector<_Container> >
111 {
112 public:
114  : DtVariableParameter<std::vector<_Container> >(required)
115  {
116  }
117 
119  : DtVariableParameter<std::vector<_Container> >(orig)
120  {
121  }
122 
124  {
125  }
126 
128  {
129  return new DtVariableParameterVector<_Container>(*this);
130  }
131 
132  virtual void process(const std::string& parameter, luabind::object const& parameters)
133  {
134  luabind::object tableEntry = parameters[parameter];
135  if (tableEntry.is_valid())
136  {
137  if (luabind::type(tableEntry) == LUA_TTABLE)
138  {
139  for (luabind::iterator paramItr(tableEntry), end; paramItr != end; ++paramItr)
140  {
141  _Container value = luabind::object_cast<_Container>(*paramItr);
142  this->myValue.push_back(value);
143  }
144 
145  this->mySpecified = true;
146  }
147  }
148  }
149 };
150 
153 {
154 public:
157  {
158  }
159 
162  {
163  }
164 
166  {
167  }
168 
170  {
171  return new DtVariableParameterNumberOrString(*this);
172  }
173 
174  virtual void process(const std::string& parameter, luabind::object const& parameters)
175  {
176  luabind::object tableEntry = parameters[parameter];
177  if (tableEntry.is_valid())
178  {
179  if (luabind::type(tableEntry) == LUA_TNUMBER)
180  {
181  myValue = DtString(luabind::object_cast<double>(tableEntry));
182  mySpecified = true;
183  }
184  else if (luabind::type(tableEntry) != LUA_TNIL)
185  {
186  myValue = DtString(luabind::object_cast<std::string>(tableEntry).c_str());
187  mySpecified = true;
188  }
189  }
190  }
191 };
192 
196 {
197 private:
199 
203 
204 public:
207 
209  virtual ~DtVariableParameterBuilder();
210 
211  void addVariableParameter(const std::string&, const DtVariableParameterInterface&);
212  void removeVariableParameter(const std::string&);
213 
214  template <typename _Container> void addVariableParameter(const std::string& s)
215  {
216  addVariableParameter(s, DtVariableParameter<_Container>());
217  }
218 
219  template <typename _Container> void addRequiredVariableParameter(const std::string& s)
220  {
221  addVariableParameter(s, DtVariableParameter<_Container>(true));
222  }
223 
224  template <typename _Container> void addVariableParameterVector(const std::string& s)
225  {
226  addVariableParameter(s, DtVariableParameterVector<_Container>());
227  }
228 
229  template <typename _Container> void addRequiredVariableParameterVector(const std::string& s)
230  {
231  addVariableParameter(s, DtVariableParameterVector<_Container>(true));
232  }
233 
234  template <typename _Container> _Container value(const std::string& key) const
235  {
236  VariableParameters::const_iterator iter = myVariableParameters.find(key);
237 
238  if (iter != myVariableParameters.end())
239  {
240  return (dynamic_cast<DtVariableParameter<_Container>*>(iter->second)->value());
241  }
242 
243  return _Container();
244  }
245 
246  template <typename _Container> void set(const std::string& key, const _Container& value)
247  {
248  VariableParameters::iterator iter = myVariableParameters.find(key);
249 
250  if (iter != myVariableParameters.end())
251  {
252  dynamic_cast<DtVariableParameter<_Container>*>(iter->second)->setValue(value);
253  }
254  }
255 
257  virtual bool valueIsSpecified(const std::string&) const;
258 
261  virtual void process(luabind::object const& parameters);
262 
263 protected:
264  typedef std::map<std::string, DtVariableParameterInterface*> VariableParameters;
266 };

Document ID: Generated on Tue Jan 29 18:21:16 EST 2013 from SVN revision 123193
Copyright © 2005-2013 VT MÄK Inc. All Rights Reserved (www.mak.com)