Skip to content

Commit

Permalink
property: remove copy() method and allow to construct or create a pro…
Browse files Browse the repository at this point in the history
…perty with a custom data source

PropertyBag assignment is supposed to have linking semantics (changing the value of a property in the copy
also changes the value in the original bag), which is important for property composition and decomposition.

The PropertyBase::copy() method introduced in 44b9970 had this linking
semantics and was used for PropertyBag assignment, but the term `copy` was actually always
associated with deep-copy semantics in RTT, like in PropertyBase::copy(const PropertyBase *) or in
DataSourceBase::copy().

This patch introduces an alternative signature for the create() method, which accepts a custom data source
instead of creating a default-initialized ValueDataSource. PropertyBag assignment now calls this create()
method with the DataSource pointer of the original property to be copied.

This reverts commit 44b9970.

Signed-off-by: Johannes Meyer <[email protected]>
  • Loading branch information
meyerj committed Jul 5, 2016
1 parent 40c71a2 commit 91a848f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
17 changes: 12 additions & 5 deletions rtt/Property.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ namespace RTT
* @post ready() will be true if datasource is a valid pointer.
*/
Property(const std::string& name, const std::string& description,
typename internal::AssignableDataSource<DataSourceType>::shared_ptr datasource )
const typename internal::AssignableDataSource<DataSourceType>::shared_ptr& datasource )
: base::PropertyBase(name, description), _value( datasource )
{
// need to do this on the datasource in order to have access to set()/rvalue() of the data source.
Expand Down Expand Up @@ -370,14 +370,21 @@ namespace RTT
return new Property<T>(*this);
}

virtual Property<T>* copy() const
virtual Property<T>* create() const
{
return new Property<T>( _name, _description, _value );
return new Property<T>( _name, _description, T() );
}

virtual Property<T>* create() const
virtual Property<T>* create( const base::DataSourceBase::shared_ptr& datasource ) const
{
return new Property<T>( _name, _description, T() );
typename internal::AssignableDataSource<DataSourceType>::shared_ptr value
= internal::AssignableDataSource<DataSourceType>::narrow( datasource.get() );
Property<T>* prop = new Property<T>( _name, _description, value );
if ( datasource && !prop->ready() ) {
log(Error) << "Cannot initialize Property: "
<< "incompatible type ( destination type: " << getType() << ", source type: " << datasource->getTypeName() << ")." << endlog();
}
return prop;
}

virtual base::DataSourceBase::shared_ptr getDataSource() const {
Expand Down
2 changes: 1 addition & 1 deletion rtt/PropertyBag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ namespace RTT

for( const_iterator i = orig.mproperties.begin(); i != orig.mproperties.end(); ++i) {
if ( orig.ownsProperty( *i ) ) {
PropertyBase* copy = (*i)->copy();
PropertyBase* copy = (*i)->create( (*i)->getDataSource() );
this->ownProperty( copy );
} else {
this->add( *i );
Expand Down
12 changes: 6 additions & 6 deletions rtt/base/PropertyBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,18 +173,18 @@ namespace RTT
virtual PropertyBase* clone() const = 0;

/**
* Deliver a shallow copy of this PropertyBase with a shared
* DataSource. The original may be deleted and the clone can be
* transparantly used in its place or vice versa.
* Create a new default instance of the PropertyBase.
* This is a factory method to 'make something of the same type'.
* The new PropertyBase has the same name and description as this.
*/
virtual PropertyBase* copy() const = 0;
virtual PropertyBase* create() const = 0;

/**
* Create a new default instance of the PropertyBase.
* Create a new instance of the PropertyBase with a custom data source.
* This is a factory method to 'make something of the same type'.
* The new PropertyBase has the same name and description as this.
*/
virtual PropertyBase* create() const = 0;
virtual PropertyBase* create( const base::DataSourceBase::shared_ptr& datasource ) const = 0;

/**
* Get a internal::DataSource through which this PropertyBase can be
Expand Down

0 comments on commit 91a848f

Please sign in to comment.