Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prop remove item #3403

Merged
merged 4 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Bindings/Java/tests/TestEditProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,10 @@ public static void main(String[] args) {
System.out.println(propertyStringList.toString());
propertyStringList.setValue(0, "Output0");
System.out.println(propertyStringList.toString());
System.out.println("Test finished!");
assert propertyStringList.getValue(0).equals("Output0");
propertyStringList.removeValueAtIndex(0);
assert propertyStringList.size()==1;
System.out.println("Test finished!");
System.exit(0);
}
catch(IOException ex){
Expand Down
19 changes: 17 additions & 2 deletions OpenSim/Common/Property.h
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,16 @@ class Property : public AbstractProperty {
setValueIsDefault(false);
return adoptAndAppendValueVirtual(value);
}

/** Remove specific entry of the list at index **/
void removeValueAtIndex(int index) {
if (index > getNumValues() || index < 0)
throw OpenSim::Exception("Property<T>::removeValueAtIndex(): Property " + getName()
+ "invalid index, out of range, ignored.");
if (getNumValues() - 1 < this->getMinListSize() || getNumValues() - 1 > this->getMaxListSize())
throw OpenSim::Exception("Property<T>::removeValueAtIndex(): Property " + getName()
+ "resulting list has improper size, ignored.");
removeValueAtIndexVirtual(index);
}
/** Search the value list for an element that has the given \a value and
return its index if found, otherwise -1. This requires only that the
template type T supports operator==(). This is a linear search so will
Expand Down Expand Up @@ -589,6 +598,7 @@ class Property : public AbstractProperty {
virtual void setValueVirtual(int index, const T& value) = 0;
virtual int appendValueVirtual(const T& value) = 0;
virtual int adoptAndAppendValueVirtual(T* value) = 0;
virtual void removeValueAtIndexVirtual(int index) = 0;
/** @endcond **/
#endif
};
Expand Down Expand Up @@ -979,7 +989,8 @@ class SimpleProperty : public Property<T> {
{ values.push_back(*valuep); // make a copy
delete valuep; // throw out the old one
return values.size()-1; }

void removeValueAtIndexVirtual(int index) override final
{ values.erase(&values[index]); }
// This is the default implementation; specialization is required if
// the Simbody default behavior is different than OpenSim's; e.g. for
// Transform serialization.
Expand Down Expand Up @@ -1156,6 +1167,10 @@ class ObjectProperty : public Property<T> {
return i;
return -1;
}
// Remove value at specific index
void removeValueAtIndexVirtual(int index) override {
objects.erase(&objects[index]);
}
private:
// Base class checks the index.
const T& getValueVirtual(int index) const override final
Expand Down