1 /* -------------------------------------------------------------------------- *
2  *                        OpenSim:  PropertyGroup.cpp                         *
3  * -------------------------------------------------------------------------- *
4  * The OpenSim API is a toolkit for musculoskeletal modeling and simulation.  *
5  * See http://opensim.stanford.edu and the NOTICE file for more information.  *
6  * OpenSim is developed at Stanford University and supported by the US        *
7  * National Institutes of Health (U54 GM072970, R24 HD065690) and by DARPA    *
8  * through the Warrior Web program.                                           *
9  *                                                                            *
10  * Copyright (c) 2005-2017 Stanford University and the Authors                *
11  * Author(s): Peter Loan                                                      *
12  *                                                                            *
13  * Licensed under the Apache License, Version 2.0 (the "License"); you may    *
14  * not use this file except in compliance with the License. You may obtain a  *
15  * copy of the License at http://www.apache.org/licenses/LICENSE-2.0.         *
16  *                                                                            *
17  * Unless required by applicable law or agreed to in writing, software        *
18  * distributed under the License is distributed on an "AS IS" BASIS,          *
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   *
20  * See the License for the specific language governing permissions and        *
21  * limitations under the License.                                             *
22  * -------------------------------------------------------------------------- */
23 
24 //=============================================================================
25 // INCLUDES
26 //=============================================================================
27 #include "PropertyGroup.h"
28 
29 //=============================================================================
30 // STATICS
31 //=============================================================================
32 using namespace std;
33 using namespace OpenSim;
34 
35 //=============================================================================
36 // CONSTRUCTOR(S) AND DESTRUCTOR
37 //=============================================================================
38 //_____________________________________________________________________________
39 /**
40  * Default constructor.
41  */
42 PropertyGroup::
PropertyGroup()43 PropertyGroup() :
44     _properties(NULL)
45 {
46     setNull();
47 }
48 
49 //_____________________________________________________________________________
50 /**
51  * Constructor taking the group name but no member names.
52  */
53 PropertyGroup::
PropertyGroup(string & aName)54 PropertyGroup(string& aName) :
55     _properties(NULL)
56 {
57     setName(aName);
58     setNull();
59 }
60 
61 //_____________________________________________________________________________
62 /**
63  * Destructor.
64  */
65 PropertyGroup::
~PropertyGroup()66 ~PropertyGroup()
67 {
68 }
69 
70 //_____________________________________________________________________________
71 /**
72  * Copy constructor.
73  *
74  * @param aGroup Group to be copied.
75  */
76 PropertyGroup::
PropertyGroup(const PropertyGroup & aGroup)77 PropertyGroup(const PropertyGroup &aGroup) :
78     _properties(NULL)
79 {
80     copyData(aGroup);
81 }
82 
83 //=============================================================================
84 // CONSTRUCTION METHODS
85 //=============================================================================
86 //_____________________________________________________________________________
87 /**
88  * Copy data members from one PropertyGroup to another.
89  *
90  * @param aGroup PropertyGroup to be copied.
91  */
92 void PropertyGroup::
copyData(const PropertyGroup & aGroup)93 copyData(const PropertyGroup &aGroup)
94 {
95     _name = aGroup._name;
96     _properties = aGroup._properties;
97 }
98 
99 //_____________________________________________________________________________
100 /**
101  * Clear the group (remove all property members).
102  */
103 void PropertyGroup::
clear()104 clear()
105 {
106     _properties.setSize(0);
107     _properties.trim();
108 }
109 
110 //_____________________________________________________________________________
111 /**
112  * Set the data members of this PropertyGroup to their null values.
113  */
114 void PropertyGroup::
setNull()115 setNull()
116 {
117 }
118 
119 //_____________________________________________________________________________
120 /**
121  * Construct and return a copy of this object.
122  *
123  * The object is allocated using the new operator, so the caller is
124  * responsible for deleting the returned object.
125  *
126  * @return Copy of this object.
127  */
clone() const128 PropertyGroup* PropertyGroup::clone() const
129 {
130     PropertyGroup *propertyGroup = new PropertyGroup(*this);
131     return(propertyGroup);
132 }
133 
134 //=============================================================================
135 // OPERATORS
136 //=============================================================================
137 //_____________________________________________________________________________
138 /**
139  * Assignment operator.
140  *
141  * @return Reference to this object.
142  */
143 PropertyGroup& PropertyGroup::
operator =(const PropertyGroup & aGroup)144 operator=(const PropertyGroup &aGroup)
145 {
146     copyData(aGroup);
147 
148     return(*this);
149 }
150 
151 //_____________________________________________________________________________
152 /**
153  * Less than operator.
154  *
155  * @param aGroup Group with which to evaluate less than.
156  * @return True if number of properties in this group is less than the number
157  *         in aGroup.
158  */
159 bool PropertyGroup::
operator <(const PropertyGroup & aGroup) const160 operator<(const PropertyGroup& aGroup) const
161 {
162     if (_properties.getSize() < aGroup.getProperties().getSize())
163         return true;
164     else
165         return false;
166 }
167 //_____________________________________________________________________________
168 /**
169  * Equality operator.
170  *
171  * @param aGroup Group with which to evaluate less than.
172  * @return True if the two groups have the same number of properties.
173  */
174 bool PropertyGroup::
operator ==(const PropertyGroup & aGroup) const175 operator==(const PropertyGroup& aGroup) const
176 {
177     if (_properties.getSize() == aGroup.getProperties().getSize())
178         return true;
179     else
180         return false;
181 }
182 
183 //_____________________________________________________________________________
184 /**
185  * Check if the group contains an object with a certain name.
186  *
187  * @param aName the name of the object.
188  * @return Boolean indicating whether or not the group contains the object.
189  */
190 bool PropertyGroup::
contains(const string & aName) const191 contains(const string& aName) const
192 {
193     for (int i = 0; i < _properties.getSize(); i++)
194         if (_properties.get(i)->getName() == aName)
195             return true;
196 
197     return false;
198 }
199 
200 //_____________________________________________________________________________
201 /**
202  * Add a property, if it is not already in the group.
203  *
204  */
205 void PropertyGroup::
add(Property_Deprecated * aProperty)206 add(Property_Deprecated* aProperty)
207 {
208     if (_properties.findIndex(aProperty) < 0)
209         _properties.append(aProperty);
210 }
211 
212 //_____________________________________________________________________________
213 /**
214  * Remove a property.
215  *
216  */
217 void PropertyGroup::
remove(Property_Deprecated * aProperty)218 remove(Property_Deprecated* aProperty)
219 {
220     int index = getPropertyIndex(aProperty);
221     if (index >= 0)
222         _properties.remove(index);
223 }
224 
225 //_____________________________________________________________________________
226 /**
227  * Get a property by index.
228  *
229  */
230 Property_Deprecated* PropertyGroup::
get(int aIndex)231 get(int aIndex)
232 {
233     if (aIndex >= 0 && aIndex < _properties.getSize())
234         return _properties.get(aIndex);
235 
236     return NULL;
237 }
238 
239 //_____________________________________________________________________________
240 /**
241  * Get the index of a property.
242  *
243  */
244 int PropertyGroup::
getPropertyIndex(Property_Deprecated * aProperty) const245 getPropertyIndex(Property_Deprecated* aProperty) const
246 {
247     for (int i = 0; i < _properties.getSize(); i++)
248         if (_properties.get(i) == aProperty)
249             return i;
250 
251     return -1;
252 }
253