Skip to content

Commit

Permalink
compare Parameters by their internal objects
Browse files Browse the repository at this point in the history
  • Loading branch information
underdoeg committed Apr 28, 2016
1 parent fe8f885 commit 2508389
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 22 deletions.
4 changes: 4 additions & 0 deletions libs/openFrameworks/types/ofParameter.cpp
Expand Up @@ -43,6 +43,10 @@ vector<string> ofAbstractParameter::getGroupHierarchyNames() const{
return hierarchy;
}

bool ofAbstractParameter::isReferenceTo(const ofAbstractParameter &other) const{
return getInternalObject() == other.getInternalObject();
}

ostream& operator<<(ostream& os, const ofAbstractParameter& p){
os << p.toString();
return os;
Expand Down
44 changes: 24 additions & 20 deletions libs/openFrameworks/types/ofParameter.h
Expand Up @@ -52,10 +52,13 @@ class ofAbstractParameter{
virtual bool isReadOnly() const = 0;
virtual shared_ptr<ofAbstractParameter> newReference() const = 0;

virtual bool isReferenceTo(const ofAbstractParameter& other) const;

protected:
virtual const ofParameterGroup getFirstParent() const = 0;
virtual void setSerializable(bool serializable)=0;
virtual string escape(const string& str) const;
virtual const void* getInternalObject() const = 0;
};


Expand Down Expand Up @@ -88,19 +91,6 @@ class ofParameterGroup: public ofAbstractParameter {
void remove(std::size_t index);
void remove(const std::string& name);

template<typename ParameterType>
void remove(ofParameter<ParameterType>& param){
std::for_each(obj->parameters.begin(), obj->parameters.end(), [&](shared_ptr<ofAbstractParameter>& p){
if(p->type() == param.type()){
auto other = static_pointer_cast<ofParameter<ParameterType>>(p);
if(param.isReferenceTo(*other)){
remove(param.getName());
}
}
});
}


void clear();

const ofParameter<bool> & getBool(const string& name) const;
Expand Down Expand Up @@ -226,6 +216,9 @@ class ofParameterGroup: public ofAbstractParameter {
vector<shared_ptr<ofAbstractParameter> >::const_reverse_iterator rbegin() const;
vector<shared_ptr<ofAbstractParameter> >::const_reverse_iterator rend() const;

protected:
const void* getInternalObject() const;

private:
class Value{
public:
Expand Down Expand Up @@ -501,8 +494,6 @@ class ofParameter: public ofAbstractParameter{
void setSerializable(bool serializable);
shared_ptr<ofAbstractParameter> newReference() const;

bool isReferenceTo(const ofParameter<ParameterType>& other);

void setParent(ofParameterGroup & _parent);

const ofParameterGroup getFirstParent() const{
Expand All @@ -518,6 +509,9 @@ class ofParameter: public ofAbstractParameter{

size_t getNumListeners() const;

protected:
const void* getInternalObject() const;

private:
class Value{
public:
Expand Down Expand Up @@ -903,11 +897,6 @@ shared_ptr<ofAbstractParameter> ofParameter<ParameterType>::newReference() const
return std::make_shared<ofParameter<ParameterType>>(*this);
}

template<typename ParameterType>
bool ofParameter<ParameterType>::isReferenceTo(const ofParameter<ParameterType> &other){
return obj == other.obj;
}

template<typename ParameterType>
void ofParameter<ParameterType>::setParent(ofParameterGroup & parent){
obj->parents.emplace_back(parent.obj);
Expand All @@ -918,6 +907,11 @@ size_t ofParameter<ParameterType>::getNumListeners() const{
return obj->changedE.size();
}

template<typename ParameterType>
const void* ofParameter<ParameterType>::getInternalObject() const{
return obj.get();
}

template<>
class ofParameter<void>: public ofAbstractParameter{
public:
Expand Down Expand Up @@ -965,6 +959,12 @@ class ofParameter<void>: public ofAbstractParameter{
}
}
size_t getNumListeners() const;

protected:
const void* getInternalObject() const{
return obj.get();
}

private:
class Value{
public:
Expand Down Expand Up @@ -1084,6 +1084,10 @@ class ofReadOnlyParameter: public ofAbstractParameter{
return parameter.getFirstParent();
}

const void* getInternalObject() const{
return parameter.getInternalObject();
}


ofParameter<ParameterType> parameter;

Expand Down
8 changes: 6 additions & 2 deletions libs/openFrameworks/types/ofParameterGroup.cpp
Expand Up @@ -20,8 +20,7 @@ void ofParameterGroup::add(ofAbstractParameter & parameter){

void ofParameterGroup::remove(ofAbstractParameter &param){
std::for_each(obj->parameters.begin(), obj->parameters.end(), [&](shared_ptr<ofAbstractParameter>& p){
//TODO: this is unsafe because it does not actually compare the two parameters
if(p->type() == param.type() && p->getName() == param.getName()){
if(p->isReferenceTo(param)){
remove(param.getName());
}
});
Expand Down Expand Up @@ -431,6 +430,10 @@ bool ofParameterGroup::isReadOnly() const{
return false;
}

const void* ofParameterGroup::getInternalObject() const{
return obj.get();
}

shared_ptr<ofAbstractParameter> ofParameterGroup::newReference() const{
return std::make_shared<ofParameterGroup>(*this);
}
Expand Down Expand Up @@ -475,3 +478,4 @@ vector<shared_ptr<ofAbstractParameter> >::const_reverse_iterator ofParameterGrou
return obj->parameters.rend();
}


0 comments on commit 2508389

Please sign in to comment.