1 #ifndef OPENSIM_PROPERTY_DEPRECATED_H_
2 #define OPENSIM_PROPERTY_DEPRECATED_H_
3 /* -------------------------------------------------------------------------- *
4 * OpenSim: Property_Deprecated.h *
5 * -------------------------------------------------------------------------- *
6 * The OpenSim API is a toolkit for musculoskeletal modeling and simulation. *
7 * See http://opensim.stanford.edu and the NOTICE file for more information. *
8 * OpenSim is developed at Stanford University and supported by the US *
9 * National Institutes of Health (U54 GM072970, R24 HD065690) and by DARPA *
10 * through the Warrior Web program. *
11 * *
12 * Copyright (c) 2005-2017 Stanford University and the Authors *
13 * Author(s): Frank C. Anderson *
14 * *
15 * Licensed under the Apache License, Version 2.0 (the "License"); you may *
16 * not use this file except in compliance with the License. You may obtain a *
17 * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. *
18 * *
19 * Unless required by applicable law or agreed to in writing, software *
20 * distributed under the License is distributed on an "AS IS" BASIS, *
21 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
22 * See the License for the specific language governing permissions and *
23 * limitations under the License. *
24 * -------------------------------------------------------------------------- */
25
26 /* Note: This code was originally developed by Realistic Dynamics Inc.
27 * Author: Frank C. Anderson
28 */
29
30
31 // INCLUDES
32 #include "osimCommonDLL.h"
33 #include "AbstractProperty.h"
34 #include "Exception.h"
35
36 #include <string>
37
38 namespace OpenSim {
39 /**
40 * A property consists of a type, name, and a value or an array of values.
41 *
42 * Class Property is an abstract base class that provides the functionality
43 * common to all property types.
44 *
45 * At the time of the first formulation of the property classes, the only
46 * property types that were envisioned are for a few fundamental data types
47 * and for Object:\n\n
48 * \tbool\n
49 * \tint\n
50 * \tfloat\n
51 * \tdouble\n
52 * \tstring\n
53 * \tObject\n
54 * \tObject pointer\n
55 * \tarray of bools\n
56 * \tarray of ints\n
57 * \tarray of floats\n
58 * \tarray of doubles\n
59 * \tarray of strings\n
60 * \tarray of Objects\n\n
61 *
62 * As additional property types are needed, they may be added however.
63 *
64 * @todo Make default constructors for all derived classes
65 * @version 1.0
66 * @author Frank C. Anderson
67 */
68
69 class Object;
70 template <class T> class Array;
71
72 #ifdef SWIG
73 #ifdef OSIMCOMMON_API
74 #undef OSIMCOMMON_API
75 #endif
76 #define OSIMCOMMON_API
77 #endif
78
79 #ifdef _WIN32
80 #pragma warning( disable : 4290 ) // VC++ non-ANSI Exception handling
81 #pragma warning( disable : 4251 ) // VC2010 no-dll export of std::string
82
83 #endif
84
85 #define Property_PROPERTY_TYPE_MISMATCH() \
86 throw Exception(std::string(__FUNCTION__)+": Property type mismatch. This property is of type "+getTypeName()+".",__FILE__,__LINE__);
87
88 class OSIMCOMMON_API Property_Deprecated : public AbstractProperty
89 {
90 public:
91 /** Enumeration of recognized types. */
92 enum PropertyType
93 {
94 None=0, Bool, Int, Dbl, Str, Obj, ObjPtr,
95 BoolArray, IntArray, DblArray, StrArray, ObjArray,
96 DblVec, DblVec3,
97 Transform // 3 BodyFixed X,Y,Z Rotations followed by 3 Translations
98 };
99
100 //=============================================================================
101 // DATA
102 //=============================================================================
103 private:
104 /** Type of the property. */
105 PropertyType _propertyType;
106 bool _matchName;
107
108 //=============================================================================
109 // METHODS
110 //=============================================================================
111 //--------------------------------------------------------------------------
112 // CONSTRUCTION
113 //--------------------------------------------------------------------------
114 public:
115 Property_Deprecated();
116 Property_Deprecated(PropertyType aType,const std::string &aName);
117 Property_Deprecated(const Property_Deprecated &aProperty);
118
119
120 /** Return the enum value corresponding to the concrete property. **/
getPropertyType()121 virtual PropertyType getPropertyType() const {return _propertyType;}
122
123 /** Return true if this is an array property. **/
isArrayProperty()124 virtual bool isArrayProperty() const {return false;}
125
126 /** By default deprecated PropertyObj properties will ignore the name
127 associated with the read-in object. This forces the name to match one
128 specified for the property. Note that this is handled differently in
129 the new Property system. */
setMatchName(bool aMatchName)130 void setMatchName(bool aMatchName) { _matchName = aMatchName; }
131 /** Return the value of the matchName flag for this (deprecated)
132 property. **/
getMatchName()133 bool getMatchName() const { return _matchName; }
134
135 //--------------------------------------------------------------------------
136 // Implement the AbstractProperty interface.
137
isEqualTo(const AbstractProperty & other)138 bool isEqualTo(const AbstractProperty& other) const override {
139 return operator==(dynamic_cast<const Property_Deprecated&>(other));
140 }
141
142 // Property_Deprecated does not implement AbstractProperty::clone(); that
143 // is left to concrete Property_Deprecated objects like PropertyInt.
144 Property_Deprecated* clone() const override = 0;
145
readFromXMLElement(SimTK::Xml::Element & propertyElement,int versionNumber)146 virtual void readFromXMLElement
147 (SimTK::Xml::Element& propertyElement,
148 int versionNumber) override
149 {assert(!"Property_Deprecated::readFromXMLElement not implemented");}
150
writeToXMLElement(SimTK::Xml::Element & propertyElement)151 virtual void writeToXMLElement
152 (SimTK::Xml::Element& propertyElement) const override
153 {assert(!"Property_Deprecated::writeToXMLElement not implemented");}
154
155 // Override for array types.
getNumValues()156 int getNumValues() const override {return 1;}
clearValues()157 void clearValues() override {assert(!"implemented");}
158
isUnnamedProperty()159 bool isUnnamedProperty() const override {return false;}
isObjectProperty()160 bool isObjectProperty() const override {return false;}
isAcceptableObjectTag(const std::string & objectTypeTag)161 bool isAcceptableObjectTag
162 (const std::string& objectTypeTag) const override {return false;}
163 const Object& getValueAsObject(int index=-1) const override
164 { Property_PROPERTY_TYPE_MISMATCH(); }
165 Object& updValueAsObject(int index=-1) override
166 { Property_PROPERTY_TYPE_MISMATCH(); }
167 void setValueAsObject(const Object& obj, int index=-1) override
168 { Property_PROPERTY_TYPE_MISMATCH(); }
169
170 //--------------------------------------------------------------------------
171
172 void setNull();
173
174 //--------------------------------------------------------------------------
175 // OPERATORS
176 //--------------------------------------------------------------------------
177 public:
178 #ifndef SWIG
179 Property_Deprecated& operator=(const Property_Deprecated &aProperty);
180 virtual bool operator==(const Property_Deprecated &aProperty) const;
181 virtual bool operator<(const Property_Deprecated &aProperty) const;
182 friend std::ostream& operator<<(std::ostream &aOut,const Property_Deprecated &aProperty) {
183 aOut << aProperty.getTypeName() << " " << aProperty.getName();
184 return(aOut);
185 };
186 #endif
187 //--------------------------------------------------------------------------
188 // GET AND SET
189 //--------------------------------------------------------------------------
190 public:
191
192 // TYPE
193 void setType(PropertyType aType);
194 PropertyType getType() const;
195 std::string getTypeName() const override =0;
196
197 // VALUE
198 // Textual representation
199 std::string toString() const override =0;
200
201 // These methods have been given default implementations, rather than being made pure virtual
202 // so that all classes derived from Property will not have to implement each method.
203 // Bool
setValue(bool aValue)204 virtual void setValue(bool aValue) { Property_PROPERTY_TYPE_MISMATCH(); }
205 #ifndef SWIG
getValueBool()206 virtual bool& getValueBool() { Property_PROPERTY_TYPE_MISMATCH(); }
207 #endif
getValueBool()208 virtual const bool& getValueBool() const { Property_PROPERTY_TYPE_MISMATCH(); }
209 // Int
setValue(int aValue)210 virtual void setValue(int aValue) { Property_PROPERTY_TYPE_MISMATCH(); }
211 #ifndef SWIG
getValueInt()212 virtual int& getValueInt() { Property_PROPERTY_TYPE_MISMATCH(); }
213 #endif
getValueInt()214 virtual const int& getValueInt() const { Property_PROPERTY_TYPE_MISMATCH(); }
215 // Dbl
setValue(double aValue)216 virtual void setValue(double aValue) { Property_PROPERTY_TYPE_MISMATCH(); }
217 #ifndef SWIG
getValueDbl()218 virtual double& getValueDbl() { Property_PROPERTY_TYPE_MISMATCH(); }
219 #endif
getValueDbl()220 virtual const double& getValueDbl() const { Property_PROPERTY_TYPE_MISMATCH(); }
221 // Str
setValue(const std::string & aValue)222 virtual void setValue(const std::string &aValue) { Property_PROPERTY_TYPE_MISMATCH(); }
223 #ifndef SWIG
getValueStr()224 virtual std::string& getValueStr() { Property_PROPERTY_TYPE_MISMATCH(); }
225 #endif
getValueStr()226 virtual const std::string& getValueStr() const { Property_PROPERTY_TYPE_MISMATCH(); }
227 // Bool Array
setValue(int aSize,const bool aArray[])228 virtual void setValue(int aSize,const bool aArray[]) { Property_PROPERTY_TYPE_MISMATCH(); }
setValue(const Array<bool> & aArray)229 virtual void setValue(const Array<bool> &aArray) { Property_PROPERTY_TYPE_MISMATCH(); }
getValueBoolArray()230 virtual Array<bool>& getValueBoolArray() { Property_PROPERTY_TYPE_MISMATCH(); }
231 #ifndef SWIG
getValueBoolArray()232 virtual const Array<bool>& getValueBoolArray() const { Property_PROPERTY_TYPE_MISMATCH(); }
233 #endif
234 // Int Array
setValue(int aSize,const int aArray[])235 virtual void setValue(int aSize,const int aArray[]) { Property_PROPERTY_TYPE_MISMATCH(); }
setValue(const Array<int> & aArray)236 virtual void setValue(const Array<int> &aArray) { Property_PROPERTY_TYPE_MISMATCH(); }
getValueIntArray()237 virtual Array<int>& getValueIntArray() { Property_PROPERTY_TYPE_MISMATCH(); }
238 #ifndef SWIG
getValueIntArray()239 virtual const Array<int>& getValueIntArray() const { Property_PROPERTY_TYPE_MISMATCH(); }
240 #endif
241 // Dbl Array
setValue(int aSize,const double aArray[])242 virtual void setValue(int aSize,const double aArray[]) { Property_PROPERTY_TYPE_MISMATCH(); }
setValue(const Array<double> & aArray)243 virtual void setValue(const Array<double> &aArray) { Property_PROPERTY_TYPE_MISMATCH(); }
getValueDblArray()244 virtual Array<double>& getValueDblArray() { Property_PROPERTY_TYPE_MISMATCH(); }
245 #ifndef SWIG
getValueDblArray()246 virtual const Array<double>& getValueDblArray() const { Property_PROPERTY_TYPE_MISMATCH(); }
247 #endif
248 // Str Array
setValue(int aSize,const std::string aArray[])249 virtual void setValue(int aSize,const std::string aArray[]) { Property_PROPERTY_TYPE_MISMATCH(); }
setValue(const Array<std::string> & aArray)250 virtual void setValue(const Array<std::string> &aArray) { Property_PROPERTY_TYPE_MISMATCH(); }
getValueStrArray()251 virtual Array<std::string>& getValueStrArray() { Property_PROPERTY_TYPE_MISMATCH(); }
252 #ifndef SWIG
getValueStrArray()253 virtual const Array<std::string>& getValueStrArray() const { Property_PROPERTY_TYPE_MISMATCH(); }
254 #endif
255
256 //--------------------------------------------------------------------------
257 // Obj, ObjPtr, and ObjArray require more careful treatment
258 //--------------------------------------------------------------------------
isValidObject(const Object * aValue)259 virtual bool isValidObject(const Object *aValue) const { Property_PROPERTY_TYPE_MISMATCH(); }
260 // Obj
261 // Got rid of setValue(Obj) since it would be dangerous to do so given that users of
262 // PropertyObj would continue to hold a reference to the (deleted) object - Eran.
getValueObj()263 virtual Object& getValueObj() { Property_PROPERTY_TYPE_MISMATCH(); }
264 #ifndef SWIG
getValueObj()265 virtual const Object& getValueObj() const { Property_PROPERTY_TYPE_MISMATCH(); }
266 #endif
267 // ObjPtr
setValue(Object * aValue)268 virtual void setValue(Object *aValue) { Property_PROPERTY_TYPE_MISMATCH(); }
getValueObjPtr()269 virtual const Object* getValueObjPtr() const { Property_PROPERTY_TYPE_MISMATCH(); }
270
271 // Obj Array
getValueObjPtr(int index)272 virtual const Object* getValueObjPtr(int index) const { Property_PROPERTY_TYPE_MISMATCH(); }
appendValue(Object * obj)273 virtual void appendValue(Object *obj) { Property_PROPERTY_TYPE_MISMATCH(); }
clearObjArray()274 virtual void clearObjArray() { Property_PROPERTY_TYPE_MISMATCH(); }
275
276 // Generic way to get number of elements
getArraySize()277 virtual int getArraySize() const { Property_PROPERTY_TYPE_MISMATCH(); }
278
279 // Templates for get & set
280 template<class T> T &getValue();
281 template<class T> const T &getValue() const;
282 template<class T> Array<T> &getValueArray();
283 template<class T> const Array<T> &getValueArray() const;
284
285 //=============================================================================
286 }; // END of class Property_Deprecated
287
288 // Specializations of template get/set
289 // Must be inline! (Trying to put function bodies in cpp fails with an internal compiler error in VC7.1)
getValue()290 template<> inline bool& Property_Deprecated::getValue() { return getValueBool(); }
getValue()291 template<> inline const bool& Property_Deprecated::getValue() const { return getValueBool(); }
getValue()292 template<> inline int& Property_Deprecated::getValue() { return getValueInt(); }
getValue()293 template<> inline const int& Property_Deprecated::getValue() const { return getValueInt(); }
getValue()294 template<> inline double& Property_Deprecated::getValue() { return getValueDbl(); }
getValue()295 template<> inline const double& Property_Deprecated::getValue() const { return getValueDbl(); }
getValue()296 template<> inline std::string& Property_Deprecated::getValue() { return getValueStr(); }
getValue()297 template<> inline const std::string& Property_Deprecated::getValue() const { return getValueStr(); }
298
getValue()299 template<> inline Array<bool>& Property_Deprecated::getValue() { return getValueBoolArray(); }
getValue()300 template<> inline const Array<bool>& Property_Deprecated::getValue() const { return getValueBoolArray(); }
getValue()301 template<> inline Array<int>& Property_Deprecated::getValue() { return getValueIntArray(); }
getValue()302 template<> inline const Array<int>& Property_Deprecated::getValue() const { return getValueIntArray(); }
getValue()303 template<> inline Array<double>& Property_Deprecated::getValue() { return getValueDblArray(); }
getValue()304 template<> inline const Array<double>& Property_Deprecated::getValue() const { return getValueDblArray(); }
getValue()305 template<> inline Array<std::string>& Property_Deprecated::getValue() { return getValueStrArray(); }
getValue()306 template<> inline const Array<std::string>& Property_Deprecated::getValue() const { return getValueStrArray(); }
307
getValueArray()308 template<> inline Array<bool>& Property_Deprecated::getValueArray() { return getValueBoolArray(); }
getValueArray()309 template<> inline const Array<bool>& Property_Deprecated::getValueArray() const { return getValueBoolArray(); }
getValueArray()310 template<> inline Array<int>& Property_Deprecated::getValueArray() { return getValueIntArray(); }
getValueArray()311 template<> inline const Array<int>& Property_Deprecated::getValueArray() const { return getValueIntArray(); }
getValueArray()312 template<> inline Array<double>& Property_Deprecated::getValueArray() { return getValueDblArray(); }
getValueArray()313 template<> inline const Array<double>& Property_Deprecated::getValueArray() const { return getValueDblArray(); }
getValueArray()314 template<> inline Array<std::string>& Property_Deprecated::getValueArray() { return getValueStrArray(); }
getValueArray()315 template<> inline const Array<std::string>& Property_Deprecated::getValueArray() const { return getValueStrArray(); }
316
317 }; //namespace
318 //=============================================================================
319 //=============================================================================
320
321 #endif // OPENSIM_PROPERTY_DEPRECATED_H_
322