SpecificProperties.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use,
  7. copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the
  9. Software is furnished to do so, subject to the following
  10. conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  15. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #include "SpecificProperties.h"
  23. #include "Log.h"
  24. #include "Point32_t2.h"
  25. #include "XMLReader.h"
  26. namespace Crown
  27. {
  28. inline Str SerializeToStr(const Margins& in)
  29. {
  30. return Str(in.left) + ", " + Str(in.top) + ", " + Str(in.right) + ", " + Str(in.bottom);
  31. }
  32. bool DeserializeFromStr(Margins& out, Str& input)
  33. {
  34. List<Str> coords;
  35. input.Split(',', coords);
  36. if (coords.GetSize() != 4)
  37. return false;
  38. if (!coords[0].ParseInt(&out.left))
  39. return false;
  40. if (!coords[1].ParseInt(&out.top))
  41. return false;
  42. if (!coords[2].ParseInt(&out.right))
  43. return false;
  44. if (!coords[3].ParseInt(&out.bottom))
  45. return false;
  46. return true;
  47. }
  48. IntProperty::IntProperty(const Str& name, int32_t* valuePtr):
  49. Property(name), mValuePtr(valuePtr)
  50. {
  51. }
  52. PropertyType IntProperty::GetType() const
  53. {
  54. return PT_Int;
  55. }
  56. Generic IntProperty::GetValue() const
  57. {
  58. return Generic(*mValuePtr);
  59. }
  60. void IntProperty::SetValue(const Generic& value)
  61. {
  62. if (!value.asInt(*mValuePtr))
  63. {
  64. Log::E("Value not valid for Int property '" + GetName() + "'");
  65. }
  66. }
  67. UIntProperty::UIntProperty(const Str& name, uint32_t* valuePtr):
  68. Property(name), mValuePtr(valuePtr)
  69. {
  70. }
  71. PropertyType UIntProperty::GetType() const
  72. {
  73. return PT_UInt;
  74. }
  75. Generic UIntProperty::GetValue() const
  76. {
  77. return Generic(*mValuePtr);
  78. }
  79. void UIntProperty::SetValue(const Generic& value)
  80. {
  81. if (!value.asUInt(*mValuePtr))
  82. {
  83. Log::E("Value not valid for UInt property '" + GetName() + "'");
  84. }
  85. }
  86. UShortProperty::UShortProperty(const Str& name, uint16_t* valuePtr):
  87. Property(name), mValuePtr(valuePtr)
  88. {
  89. }
  90. PropertyType UShortProperty::GetType() const
  91. {
  92. return PT_UShort;
  93. }
  94. Generic UShortProperty::GetValue() const
  95. {
  96. return Generic(*mValuePtr);
  97. }
  98. void UShortProperty::SetValue(const Generic& value)
  99. {
  100. if (!value.asUShort(*mValuePtr))
  101. {
  102. Log::E("Value not valid for UShort property '" + GetName() + "'");
  103. }
  104. }
  105. FloatProperty::FloatProperty(const Str& name, float* valuePtr):
  106. Property(name), mValuePtr(valuePtr)
  107. {
  108. }
  109. PropertyType FloatProperty::GetType() const
  110. {
  111. return PT_Float;
  112. }
  113. Generic FloatProperty::GetValue() const
  114. {
  115. return Generic(*mValuePtr);
  116. }
  117. void FloatProperty::SetValue(const Generic& value)
  118. {
  119. if (!value.asFloat(*mValuePtr))
  120. {
  121. Log::E("Value not valid for Float property '" + GetName() + "'");
  122. }
  123. }
  124. Point32_t2Property::Point32_t2Property(const Str& name, Point32_t2* valuePtr):
  125. Property(name), mValuePtr(valuePtr)
  126. {
  127. }
  128. PropertyType Point32_t2Property::GetType() const
  129. {
  130. return PT_Point32_t2;
  131. }
  132. Generic Point32_t2Property::GetValue() const
  133. {
  134. return Generic(SerializeToStr(*mValuePtr));
  135. }
  136. void Point32_t2Property::SetValue(const Generic& value)
  137. {
  138. Str val = value.asStr();
  139. if (!DeserializeFromStr(*mValuePtr, (Str&)val))
  140. {
  141. Log::E("Value not valid for Point32_t2 property '" + GetName() + "'");
  142. }
  143. }
  144. StrProperty::StrProperty(const Str& name, Str* valuePtr):
  145. Property(name), mValuePtr(valuePtr)
  146. {
  147. }
  148. PropertyType StrProperty::GetType() const
  149. {
  150. return PT_Str;
  151. }
  152. Generic StrProperty::GetValue() const
  153. {
  154. return Generic(*mValuePtr);
  155. }
  156. void StrProperty::SetValue(const Generic& value)
  157. {
  158. if (!value.asStr(*mValuePtr))
  159. {
  160. Log::E("Value not valid for Str property '" + GetName() + "'");
  161. }
  162. }
  163. MarginsProperty::MarginsProperty(const Str& name, Margins* valuePtr):
  164. Property(name), mValuePtr(valuePtr)
  165. {
  166. }
  167. PropertyType MarginsProperty::GetType() const
  168. {
  169. return PT_Margins;
  170. }
  171. Generic MarginsProperty::GetValue() const
  172. {
  173. return Generic(SerializeToStr(*mValuePtr));
  174. }
  175. void MarginsProperty::SetValue(const Generic& value)
  176. {
  177. Str val = value.asStr();
  178. if (!DeserializeFromStr(*mValuePtr, (Str&)val))
  179. {
  180. Log::E("Value not valid for Margins property '" + GetName() + "'");
  181. }
  182. }
  183. TemplateProperty::TemplateProperty(const Str& name, XMLNode** valuePtr):
  184. Property(name), mValuePtr(valuePtr)
  185. {
  186. }
  187. PropertyType TemplateProperty::GetType() const
  188. {
  189. return PT_Template;
  190. }
  191. Generic TemplateProperty::GetValue() const
  192. {
  193. return Generic(*mValuePtr);
  194. }
  195. void TemplateProperty::SetValue(const Generic& value)
  196. {
  197. XMLNode* node = NULL;
  198. if (!value.asType<XMLNode*>(&node))
  199. {
  200. Log::E("The TemplateProperty wants an XMLNode representing the template informations");
  201. return;
  202. }
  203. if (*mValuePtr != NULL)
  204. {
  205. delete *mValuePtr;
  206. }
  207. *mValuePtr = node->Clone();
  208. }
  209. IWithPropertiesProperty::IWithPropertiesProperty(const Str& name, IWithProperties** valuePtr):
  210. Property(name), mValuePtr(valuePtr)
  211. {
  212. }
  213. PropertyType IWithPropertiesProperty::GetType() const
  214. {
  215. return PT_Template;
  216. }
  217. Generic IWithPropertiesProperty::GetValue() const
  218. {
  219. return Generic(*mValuePtr);
  220. }
  221. void IWithPropertiesProperty::SetValue(const Generic& value)
  222. {
  223. IWithProperties* castedValue = NULL;
  224. if (!value.asType<IWithProperties*>(&castedValue))
  225. {
  226. Log::E("The IWithPropertiesProperty wants an object implementing the int32_terface IWithProperties");
  227. return;
  228. }
  229. *mValuePtr = castedValue;
  230. }
  231. //GenericListProperty::GenericListProperty(const Str& name, Shared<IList<Generic> >* valuePtr):
  232. // Property(name), mValuePtr(valuePtr)
  233. //{
  234. //}
  235. //PropertyType GenericListProperty::GetType() const
  236. //{
  237. // return PT_GenericList;
  238. //}
  239. //Generic GenericListProperty::GetValue() const
  240. //{
  241. // return Generic(mValuePtr->GetPoint32_ter());
  242. //}
  243. //void GenericListProperty::SetValue(const Generic& value)
  244. //{
  245. // if (value.GetType() == Generic::GT_List)
  246. // {
  247. // if (value.asList(*mValuePtr))
  248. // {
  249. // return;
  250. // }
  251. // }
  252. // //if (!DeserializeFromStr(*mValuePtr, value.asStr()))
  253. // //{
  254. // // Log::E("Value not valid for Margins property '" + GetName() + "'");
  255. // //}
  256. //}
  257. EnumProperty::EnumProperty(const Str& name, int32_t* valuePtr):
  258. Property(name), mValuePtr(valuePtr)
  259. {
  260. }
  261. PropertyType EnumProperty::GetType() const
  262. {
  263. return PT_Int;
  264. }
  265. Generic EnumProperty::GetValue() const
  266. {
  267. return Generic(*mValuePtr);
  268. }
  269. void EnumProperty::AddValueMapping(const Str& valueName, int32_t enumValue)
  270. {
  271. EnumPropertyPair pair = {valueName, enumValue};
  272. mValuesMapping.Append(pair);
  273. }
  274. void EnumProperty::SetValue(const Generic& value)
  275. {
  276. const Str& strValue = value.asStr();
  277. for(int32_t i = 0; i < mValuesMapping.GetSize(); i++)
  278. {
  279. if (strValue == mValuesMapping[i].valueName)
  280. {
  281. *mValuePtr = mValuesMapping[i].enumValue;
  282. return;
  283. }
  284. }
  285. Log::E("Value '" + strValue + "' not valid for Enum property '" + GetName() + "'");
  286. }
  287. }