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
00040 #include "AttributeRepository.hpp"
00041 #include "mystd.hpp"
00042 #include <functional>
00043 #include <boost/bind.hpp>
00044
00045 namespace RTT
00046 {
00047 using namespace std;
00048 using namespace boost;
00049
00050 AttributeRepository::AttributeRepository()
00051 :bag(0)
00052 {
00053 }
00054
00055 AttributeRepository::~AttributeRepository()
00056 {
00057 delete bag;
00058
00059
00060 }
00061
00062 AttributeRepository* AttributeRepository::copy( std::map<const DataSourceBase*, DataSourceBase*>& repl, bool inst ) const
00063 {
00064 AttributeRepository* ar = new AttributeRepository();
00065 for ( map_t::const_iterator i = values.begin(); i != values.end(); ++i ) {
00066 ar->setValue((*i)->copy( repl, inst ) );
00067 }
00068 return ar;
00069 }
00070
00071
00072 void AttributeRepository::clear()
00073 {
00074 for ( map_t::iterator i = values.begin(); i != values.end(); ++i )
00075 delete *i;
00076 values.clear();
00077 delete bag;
00078 bag = 0;
00079 }
00080
00081 bool AttributeRepository::setValue( AttributeBase* value )
00082 {
00083 map_t::iterator i = find( values.begin(), values.end(), value );
00084 if ( i != values.end() )
00085 return false;
00086 values.push_back( value );
00087 return true;
00088 }
00089
00090 bool AttributeRepository::addProperty( PropertyBase* pb ) {
00091 if ( bag && bag->find( pb->getName() ) )
00092 return false;
00093 if ( bag == 0 )
00094 bag = new PropertyBag();
00095 bag->add( pb );
00096 return true;
00097 }
00098
00099 bool AttributeRepository::removeValue( const std::string& name )
00100 {
00101 map_t::iterator i = find_if( values.begin(), values.end(), bind(equal_to<std::string>(),name, bind(&AttributeBase::getName, _1)) );
00102 if ( i != values.end() ) {
00103 delete (*i);
00104 values.erase( i );
00105 return true;
00106 }
00107 return false;
00108 }
00109
00110 AttributeBase* AttributeRepository::getValue( const std::string& name ) const
00111 {
00112 map_t::const_iterator i = find_if( values.begin(), values.end(), bind(equal_to<std::string>(),name, bind(&AttributeBase::getName, _1)) );
00113 if ( i == values.end() ) return 0;
00114 else return *i;
00115 }
00116
00117 bool AttributeRepository::hasAttribute( const std::string& name ) const
00118 {
00119 map_t::const_iterator i = find_if( values.begin(), values.end(), bind(equal_to<std::string>(),name, bind(&AttributeBase::getName, _1)) );
00120 return i != values.end();
00121 }
00122
00123 bool AttributeRepository::hasProperty( const std::string& name ) const
00124 {
00125 return (bag && bag->find(name) != 0);
00126 }
00127
00128 bool AttributeRepository::removeProperty( PropertyBase* p )
00129 {
00130 if ( bag && bag->find( p->getName() ) ) {
00131 bag->remove(p);
00132 removeValue( p->getName() );
00133 return true;
00134 }
00135 return false;
00136 }
00137
00138 std::vector<std::string> AttributeRepository::names() const { return this->getAttributes(); }
00139
00140 std::vector<std::string> AttributeRepository::getAttributes() const
00141 {
00142 std::vector<std::string> ret;
00143 std::transform( values.begin(), values.end(), ret.begin(),back_inserter(ret), bind(&AttributeBase::getName, _1) );
00144 return ret;
00145 }
00146
00147 PropertyBag* AttributeRepository::properties() const
00148 {
00149 if ( bag == 0 )
00150 bag = new PropertyBag();
00151 return bag;
00152 }
00153 }