Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #include "TypeInfo.hpp"
00040 #include "TypeConstructor.hpp"
00041 #include "../internal/DataSourceTypeInfo.hpp"
00042 #include "TypeTransporter.hpp"
00043
00044 #include "rtt-config.h"
00045
00046 #include "../Logger.hpp"
00047 #include "../base/AttributeBase.hpp"
00048
00049 namespace RTT
00050 {
00051 using namespace std;
00052 using namespace detail;
00053 using namespace internal;
00054
00055 TypeInfo::~TypeInfo()
00056 {
00057
00058 for (Transporters::iterator i = transporters.begin(); i != transporters.end(); ++i)
00059 delete *i;
00060
00061
00062 for (Constructors::iterator i= constructors.begin(); i != constructors.end(); ++i)
00063 delete (*i);
00064 }
00065
00066 std::vector<std::string> TypeInfo::getTypeNames() const
00067 {
00068 return mtypenames;
00069 }
00070
00071 void TypeInfo::addAlias(const std::string& alias) {
00072
00073 if ( !alias.empty() && find(mtypenames.begin(), mtypenames.end(), alias) == mtypenames.end() )
00074 mtypenames.push_back(alias);
00075 }
00076
00077 bool TypeInfo::isType(const std::string& name) {
00078 return (find(mtypenames.begin(), mtypenames.end(), name) != mtypenames.end() );
00079 }
00080
00081 base::AttributeBase* TypeInfo::buildVariable(std::string name, int hint) const
00082 {
00083 return mdsf ? mdsf->buildVariable(name, hint) : 0;
00084 }
00085
00086 DataSourceBase::shared_ptr TypeInfo::construct(const std::vector<DataSourceBase::shared_ptr>& args) const
00087 {
00088
00089 DataSourceBase::shared_ptr ds;
00090
00091 if ( args.empty() ) {
00092 AttributeBase* ab = this->buildVariable("constructor");
00093 ds = ab->getDataSource();
00094 delete ab;
00095 return ds;
00096 }
00097
00098
00099 if ( args.size() == 1 && args.front()->getTypeInfo() == this )
00100 return args.front();
00101
00102 Constructors::const_iterator i= constructors.begin();
00103 while (i != constructors.end() ) {
00104 ds = (*i)->build( args );
00105 if ( ds )
00106 return ds;
00107 ++i;
00108 }
00109
00110 return ds;
00111 }
00112
00113 void TypeInfo::addConstructor(TypeConstructor* tb) {
00114 constructors.push_back(tb);
00115 }
00116
00117 DataSourceBase::shared_ptr TypeInfo::convert(DataSourceBase::shared_ptr arg) const
00118 {
00119 DataSourceBase::shared_ptr ds;
00120 Constructors::const_iterator i= constructors.begin();
00121 if ( arg->getTypeInfo() == this )
00122 return arg;
00123
00124 while (i != constructors.end() ) {
00125 ds = (*i)->convert( arg );
00126 if ( ds ) {
00127 return ds;
00128 }
00129 ++i;
00130 }
00131
00132 return arg;
00133 }
00134
00135 bool TypeInfo::addProtocol(int protocol_id, TypeTransporter* tt)
00136 {
00137 if (transporters.size() < static_cast<size_t>(protocol_id + 1))
00138 transporters.resize(protocol_id + 1);
00139 if ( transporters[protocol_id] ) {
00140 log(Debug) << "A protocol with id "<<protocol_id<<" was already added for type "<< getTypeName()<<endlog();
00141 delete tt;
00142 return false;
00143 }
00144 transporters[protocol_id] = tt;
00145 return true;
00146 }
00147
00148 TypeTransporter* TypeInfo::getProtocol(int protocol_id) const
00149 {
00150
00151
00152
00153
00154 if ( protocol_id + 1 > int(transporters.size()) || transporters[protocol_id] == 0) {
00155 if ( DataSourceTypeInfo<UnknownType>::getTypeInfo() != this )
00156 return DataSourceTypeInfo<UnknownType>::getTypeInfo()->getProtocol( protocol_id );
00157 else {
00158 log(Warning) << "The protocol with id "<<protocol_id<<" did not register a fall-back handler for unknown types!"<<endlog();
00159 log(Warning) << " triggered by: "<< getTypeName() << " which does not have a transport."<<endlog();
00160 return 0;
00161 }
00162 }
00163 return transporters[protocol_id];
00164 }
00165
00166 bool TypeInfo::hasProtocol(int protocol_id) const
00167 {
00168
00169
00170
00171
00172 if ( protocol_id + 1 > int(transporters.size()) || transporters[protocol_id] == 0) {
00173 return false;
00174 }
00175 return true;
00176 }
00177
00178 std::vector<int> TypeInfo::getTransportNames() const
00179 {
00180 std::vector<int> ret;
00181 for (size_t i=0; i<transporters.size(); ++i)
00182 {
00183
00184
00185
00186 if (0 != transporters[i])
00187 {
00188 ret.push_back(i);
00189 }
00190 }
00191 return ret;
00192 }
00193
00194 }