Orocos Real-Time Toolkit  2.6.0
StructTypeInfo.hpp
00001 /***************************************************************************
00002   tag: The SourceWorks  Tue Sep 7 00:55:18 CEST 2010  StructTypeInfo.hpp
00003 
00004                         StructTypeInfo.hpp -  description
00005                            -------------------
00006     begin                : Tue September 07 2010
00007     copyright            : (C) 2010 The SourceWorks
00008     email                : peter@thesourceworks.com
00009 
00010  ***************************************************************************
00011  *   This library is free software; you can redistribute it and/or         *
00012  *   modify it under the terms of the GNU General Public                   *
00013  *   License as published by the Free Software Foundation;                 *
00014  *   version 2 of the License.                                             *
00015  *                                                                         *
00016  *   As a special exception, you may use this file as part of a free       *
00017  *   software library without restriction.  Specifically, if other files   *
00018  *   instantiate templates or use macros or inline functions from this     *
00019  *   file, or you compile this file and link it with other files to        *
00020  *   produce an executable, this file does not by itself cause the         *
00021  *   resulting executable to be covered by the GNU General Public          *
00022  *   License.  This exception does not however invalidate any other        *
00023  *   reasons why the executable file might be covered by the GNU General   *
00024  *   Public License.                                                       *
00025  *                                                                         *
00026  *   This library is distributed in the hope that it will be useful,       *
00027  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00028  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00029  *   Lesser General Public License for more details.                       *
00030  *                                                                         *
00031  *   You should have received a copy of the GNU General Public             *
00032  *   License along with this library; if not, write to the Free Software   *
00033  *   Foundation, Inc., 59 Temple Place,                                    *
00034  *   Suite 330, Boston, MA  02111-1307  USA                                *
00035  *                                                                         *
00036  ***************************************************************************/
00037 
00038 
00039 #ifndef ORO_STRUCT_TYPE_INFO_HPP
00040 #define ORO_STRUCT_TYPE_INFO_HPP
00041 
00042 #include "TemplateTypeInfo.hpp"
00043 #include "PropertyDecomposition.hpp"
00044 #include "type_discovery.hpp"
00045 #include "MemberFactory.hpp"
00046 
00047 namespace RTT
00048 {
00049     namespace types
00050     {
00061         template<typename T, bool has_ostream = false>
00062         class StructTypeInfo: public TemplateTypeInfo<T, has_ostream>, public MemberFactory
00063         {
00064         public:
00065             StructTypeInfo(std::string name) :
00066                 TemplateTypeInfo<T, has_ostream> (name)
00067             {
00068             }
00069 
00070         bool installTypeInfoObject(TypeInfo* ti) {
00071             // aquire a shared reference to the this object
00072             boost::shared_ptr< StructTypeInfo<T,has_ostream> > mthis = boost::dynamic_pointer_cast<StructTypeInfo<T,has_ostream> >( this->getSharedPtr() );
00073             assert(mthis);
00074             // Allow base to install first
00075             TemplateTypeInfo<T,has_ostream>::installTypeInfoObject(ti);
00076             // Install the factories for primitive types
00077             ti->setMemberFactory( mthis );
00078 
00079             // Don't delete us, we're memory-managed.
00080             return false;
00081         }
00082 
00083             virtual std::vector<std::string> getMemberNames() const {
00084                 // only discover the part names of this struct:
00085                 type_discovery in;
00086                 T t; // boost can't work without a value.
00087                 in.discover( t );
00088                 return in.mnames;
00089             }
00090 
00091             virtual base::DataSourceBase::shared_ptr getMember(base::DataSourceBase::shared_ptr item,
00092                                                              base::DataSourceBase::shared_ptr id) const {
00093                 // user tried to pass the member name by data source, but we can't read out this datasource after getMember() returns.
00094                 // ie, we could only read out id as a string and then call the getMember below.
00095                 // type_discovery requires the name right now and does not allow to delay the name, unless we discover the whole type,
00096                 // keep all datasources and then use getMember using some functor data source.... Not going to do that !
00097                 assert(false && "You're doing something new and exotic. Contact the Orocos-dev mailing list.");
00098                 return base::DataSourceBase::shared_ptr();
00099             }
00100 
00101             virtual base::DataSourceBase::shared_ptr getMember(base::DataSourceBase::shared_ptr item, const std::string& name) const {
00102                 typename internal::AssignableDataSource<T>::shared_ptr adata = boost::dynamic_pointer_cast< internal::AssignableDataSource<T> >( item );
00103                 // Use a copy in case our parent is not assignable:
00104                 if ( !adata ) {
00105                     // is it non-assignable ?
00106                     typename internal::DataSource<T>::shared_ptr data = boost::dynamic_pointer_cast< internal::DataSource<T> >( item );
00107                     if ( data ) {
00108                         // create a copy
00109                         adata = new internal::ValueDataSource<T>( data->get() );
00110                     }
00111                 }
00112                 if (adata) {
00113                     type_discovery in( adata );
00114                     return in.discoverMember( adata->set(), name );
00115                 }
00116                 log(Error) << "Wrong call to type info function " + this->getTypeName() << "'s getMember() can not process "<< item->getTypeName() <<endlog();
00117                 return base::DataSourceBase::shared_ptr();
00118             }
00119 
00120             virtual bool getMember(internal::Reference* ref, base::DataSourceBase::shared_ptr item, const std::string& name) const {
00121                 typename internal::AssignableDataSource<T>::shared_ptr adata = boost::dynamic_pointer_cast< internal::AssignableDataSource<T> >( item );
00122                 // Use a copy in case our parent is not assignable:
00123                 if ( !adata ) {
00124                     // is it non-assignable ?
00125                     typename internal::DataSource<T>::shared_ptr data = boost::dynamic_pointer_cast< internal::DataSource<T> >( item );
00126                     if ( data ) {
00127                         // create a copy -> this is the only place & case where we allocate -> how to fix ?
00128                         adata = new internal::ValueDataSource<T>( data->get() );
00129                     }
00130                 }
00131                 if (adata) {
00132                     type_discovery in( adata );
00133                     return in.referenceMember( ref, adata->set(), name );
00134                 }
00135                 log(Error) << "Wrong call to type info function " + this->getTypeName() << "'s getMember() can not process "<< item->getTypeName() <<endlog();
00136                 return false;
00137             }
00138 
00139         virtual bool resize(base::DataSourceBase::shared_ptr arg, int size) const
00140             {
00141                 return false;
00142             }
00143 
00144 
00150             virtual bool composeTypeImpl(const PropertyBag& source,  typename internal::AssignableDataSource<T>::reference_t result) const {
00151                 // The default implementation decomposes result and refreshes it with source.
00152                 TypeInfoRepository::shared_ptr tir = Types();
00153                 internal::ReferenceDataSource<T> rds(result);
00154                 rds.ref(); // prevent dealloc.
00155                 PropertyBag decomp;
00156                 // only try refreshProperties if decomp's type is equal to source type.
00157                 // update vs refresh: since it is intentional that the decomposition leads to references to parts of result,
00158                 // only refreshProperties() is meaningful (ie we have a one-to-one mapping). In case of sequences, this would
00159                 // of course not match, so this is struct specific.
00160                 return typeDecomposition( &rds, decomp, false) && ( tir->type(decomp.getType()) == tir->type(source.getType()) ) && refreshProperties(decomp, source);
00161             }
00162 
00163 
00164         };
00165     }
00166 }
00167 
00168 #endif