PropertyComposition.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Tue Jan 11 17:31:48 2011 +0100 PropertyComposition.cpp
3 
4  PropertyComposition.cpp - description
5  -------------------
6  begin : Tue Jan 11 2011
7  copyright : (C) 2011 Peter Soetens
8  email : peter@thesourceworks.com
9 
10  ***************************************************************************
11  * This library is free software; you can redistribute it and/or *
12  * modify it under the terms of the GNU General Public *
13  * License as published by the Free Software Foundation; *
14  * version 2 of the License. *
15  * *
16  * As a special exception, you may use this file as part of a free *
17  * software library without restriction. Specifically, if other files *
18  * instantiate templates or use macros or inline functions from this *
19  * file, or you compile this file and link it with other files to *
20  * produce an executable, this file does not by itself cause the *
21  * resulting executable to be covered by the GNU General Public *
22  * License. This exception does not however invalidate any other *
23  * reasons why the executable file might be covered by the GNU General *
24  * Public License. *
25  * *
26  * This library is distributed in the hope that it will be useful, *
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
29  * General Public License for more details. *
30  * *
31  * You should have received a copy of the GNU General Public *
32  * License along with this library; if not, write to the Free Software *
33  * Foundation, Inc., 59 Temple Place, *
34  * Suite 330, Boston, MA 02111-1307 USA *
35  * *
36  ***************************************************************************/
37 
38 
39 #include "PropertyComposition.hpp"
41 #include "../Logger.hpp"
42 #include "../Property.hpp"
43 #include "types/Types.hpp"
44 
45 using namespace RTT;
46 using namespace RTT::detail;
47 
48 bool RTT::types::composePropertyBag( PropertyBag const& sourcebag, PropertyBag& target )
49 {
50  if ( !target.empty() ) {
51  log(Error) <<"composePropertyBag: target bag must be empty."<< endlog();
52  return false;
53  }
54  bool has_error = false;
55  PropertyBag::const_iterator sit = sourcebag.begin();
56  for( ; sit != sourcebag.end(); ++sit) {
57  // test if it's a property bag, then we need to recurse:
59  isbag = *sit; // separate assignment avoids error message !
60  if ( isbag.ready() && isbag.value().getType() != "PropertyBag") {
61  // typed property bag, need to compose it.
62  TypeInfo* ti = Types()->type( isbag.value().getType() );
63  if ( ti == 0) {
64  log(Error) <<"Could not compose unknown type '" << isbag.value().getType() <<"'." <<endlog();
65  has_error = true;
66  continue;
67  }
68  PropertyBase* tgtprop = ti->buildProperty(isbag.getName(), isbag.getDescription());
69  if ( ti->composeType(isbag.getDataSource(), tgtprop->getDataSource()) ) {
70  log(Debug) << "Used user's composition function for " << tgtprop->getName() <<":"<<tgtprop->getType()<<endlog();
71  target.ownProperty(tgtprop);
72  } else {
73  log(Error) <<"The type '" << isbag.value().getType() <<"' did not provide a type composition function, but I need one to compose it from a PropertyBag." <<endlog();
74  delete tgtprop;
75  has_error = true;
76  continue;
77  }
78  } else {
79  if ( isbag.ready() ) {
80  // plain property bag, clone and recurse:
81  Property<PropertyBag>* newbag = new Property<PropertyBag>(isbag.getName(), isbag.getDescription());
82  if ( composePropertyBag(isbag.value(), newbag->value()) == false) {
83  delete newbag;
84  has_error = true;
85  continue;
86  }
87  target.ownProperty( newbag );
88  } else {
89  // plain property, not a bag:
90  target.ownProperty( (*sit)->clone() );
91  }
92  }
93  }
94 
95  return !has_error;
96 }
97 
99 {
100  if ( !target.empty() ) {
101  log(Error) <<"decomposePropertyBag: target bag must be empty."<< endlog();
102  return false;
103  }
104  PropertyBag::const_iterator sit = sourcebag.begin();
105  while( sit != sourcebag.end()) {
106  // test if it's a property bag, then we need to recurse:
107  Property<PropertyBag> isbag;
108  isbag = *sit; // avoid logging error
109  if ( isbag.ready() ) {
110  // create an empty instance to recurse in:
111  Property<PropertyBag>* newtarget = new Property<PropertyBag>(isbag.getName(), isbag.getDescription() );
112  newtarget->value().setType( isbag.rvalue().getType() );
113  target.ownProperty( newtarget );
114  if ( decomposePropertyBag(isbag.value(), newtarget->value() ) == false) {
115  assert(false && "internal error in decomposePropertyBag."); // this is a best effort function.
116  }
117  } else {
118  // decompose non-bag type:
119  log(Debug) << "Checking for decompose "<< (*sit)->getName() <<endlog();
120 
121  // Try decomposeType() first because this is the user's implementation of decomposition:
122  DataSourceBase::shared_ptr dsb = (*sit)->getTypeInfo()->decomposeType( (*sit)->getDataSource() );
123  if ( dsb ) {
124  // check if type returned itself to avoid decomposition:
125  if ( dsb == (*sit)->getDataSource() ) {
126  // primitive type : clone the instance
127  target.ownProperty( (*sit)->clone() );
128  } else {
130  if ( bagds ) {
131  // property bag ? -> further decompose
132  // create an empty instance to recurse in:
133  Property<PropertyBag>* newtarget = new Property<PropertyBag>((*sit)->getName(), (*sit)->getDescription() );
134  newtarget->value().setType( bagds->rvalue().getType() );
135  target.ownProperty( newtarget );
136  if ( decomposePropertyBag(bagds->rvalue(), newtarget->value() ) == false) {
137  assert(false && "internal error in decomposePropertyBag."); // this is a best effort function.
138  }
139  } else {
140  // other type ? -> add
141  base::PropertyBase* p = dsb->getTypeInfo()->buildProperty((*sit)->getName(), (*sit)->getDescription(), dsb);
142  if ( target.ownProperty( p ) == false)
143  log(Error) <<"Failed to create a property of decomposed data of type "<<(*sit)->getType() <<endlog();
144  }
145  }
146  } else {
147  // if decomposeType() returned null, try generic decomposition, based on getMember():
148  Property<PropertyBag> res((*sit)->getName(), (*sit)->getDescription() );
149  // propertyDecomposition decomposes fully, so the result does not need to be recursed:
150  if ( types::propertyDecomposition(*sit, res.value() ) ) {
151  target.ownProperty( res.clone() );
152  }
153  }
154  }
155  ++sit;
156  }
157  return true;
158 }
virtual base::DataSourceBase::shared_ptr getDataSource() const
Definition: Property.hpp:393
bool composeType(base::DataSourceBase::shared_ptr source, base::DataSourceBase::shared_ptr target) const
Definition: TypeInfo.hpp:386
bool propertyDecomposition(base::PropertyBase *source, PropertyBag &targetbag, bool recurse)
bool RTT_API composePropertyBag(PropertyBag const &sourcebag, PropertyBag &target)
const_reference_t rvalue() const
Definition: Property.hpp:285
A container for holding references to properties.
Definition: PropertyBag.hpp:96
reference_t value()
Definition: Property.hpp:277
bool ownProperty(base::PropertyBase *p)
Definition: PropertyBag.cpp:85
bool RTT_API decomposePropertyBag(PropertyBag const &sourcebag, PropertyBag &target)
virtual const_reference_t rvalue() const =0
virtual std::string getType() const =0
const std::string & getDescription() const
base::PropertyBase * buildProperty(const std::string &name, const std::string &desc, base::DataSourceBase::shared_ptr source=0) const
Definition: TypeInfo.hpp:214
TypeInfoRepository::shared_ptr Types()
Definition: Types.cpp:48
Properties::const_iterator const_iterator
bool empty() const
boost::intrusive_ptr< DataSource< T > > shared_ptr
Definition: DataSource.hpp:115
static DataSource< T > * narrow(base::DataSourceBase *db)
const std::string & getName() const
boost::intrusive_ptr< DataSourceBase > shared_ptr
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:53
static Logger & log()
Definition: Logger.hpp:350
static Logger::LogFunction endlog()
Definition: Logger.hpp:362
virtual DataSourceBase::shared_ptr getDataSource() const =0
iterator begin()
virtual const types::TypeInfo * getTypeInfo() const =0


rtt
Author(s): RTT Developers
autogenerated on Tue Jun 25 2019 19:33:27