All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
PropertyMgr.cpp
Go to the documentation of this file.
1 // $Id: PropertyMgr.cpp,v 1.23 2008/04/03 17:27:01 marcocle Exp $
2 // ============================================================================
3 // CVS tag $Name: $, version $Revision: 1.23 $
4 // ============================================================================
5 // Include files
6 // ============================================================================
7 // STD& STL
8 // ============================================================================
9 #include <iostream>
10 #include <sstream>
11 #include <string>
12 #include <functional>
13 #include <stdexcept>
14 #include <algorithm>
15 // ============================================================================
16 // GaudiKernel
17 // ============================================================================
19 // ============================================================================
20 namespace
21 {
22  // ==========================================================================
24  struct Nocase : public std::binary_function<std::string,std::string,bool>
25  {
26  inline bool operator() ( const std::string& v1 ,
27  const std::string& v2 ) const
28  {
29  std::string::const_iterator i1 = v1.begin() ;
30  std::string::const_iterator i2 = v2.begin() ;
31  for ( ; v1.end() != i1 && v2.end() != i2 ; ++i1 , ++i2 )
32  { if ( toupper(*i1) != toupper(*i2) ) { return false ; } }
33  return v1.size() == v2.size() ;
34  }
35  };
36  // ==========================================================================
38  struct PropByName : public std::unary_function<const Property*,bool>
39  {
40  PropByName ( const std::string& name ) : m_name ( name ) {} ;
41  inline bool operator() ( const Property* p ) const
42  { return ( 0 == p ) ? false : m_cmp ( p->name() , m_name ) ; }
43  private:
44  std::string m_name ;
45  Nocase m_cmp ;
46  } ;
47  // ==========================================================================
48 }
49 // ====================================================================
50 // constructor from the interface
51 // ====================================================================
53  : m_properties ()
54  , m_remoteProperties ()
55  , m_todelete ()
56  , m_pOuter ( iface )
57 {
58  addRef(); // initial reference count set to 1
59 }
60 // ====================================================================
61 // copy constructor
62 // ====================================================================
64  : IInterface(right),
65  IProperty(right),
67  base_class(right)
68  , m_properties ( right.m_properties )
69  , m_remoteProperties ( right.m_remoteProperties )
70  , m_todelete ( right.m_todelete )
71  , m_pOuter ( right.m_pOuter )
72 {
73  addRef(); // initial reference count set to 1
74  std::transform
75  ( m_todelete.begin() , m_todelete.end () ,
76  m_todelete.begin() , std::mem_fun(&Property::clone));
77 }
78 // ====================================================================
79 // destructor
80 // ====================================================================
82 {
84  for ( Properties::iterator ip = m_todelete.begin() ;
85  m_todelete.end() != ip ; ++ip ) { delete *ip ; }
86 }
87 // ====================================================================
88 // assignment operator
89 // ====================================================================
91 {
92  if ( &right == this ) { return *this ; }
93  //
94  for ( Properties::iterator ip = m_todelete.begin() ;
95  m_todelete.end() != ip ; ++ip ) { delete *ip ; }
96  //
97  m_properties = right.m_properties ;
99  m_todelete = right.m_todelete ;
100  m_pOuter = right.m_pOuter ;
101  //
102  std::transform
103  ( m_todelete.begin() , m_todelete.end () ,
104  m_todelete.begin() , std::mem_fun(&Property::clone));
105  //
106  return *this ;
107 }
108 // ====================================================================
109 // Declare a remote property
110 // ====================================================================
112 ( const std::string& name ,
113  IProperty* rsvc ,
114  const std::string& rname )
115 {
116  if ( 0 == rsvc ) { return 0 ; }
117  const std::string& nam = rname.empty() ? name : rname ;
118  Property* p = property ( nam , rsvc->getProperties() ) ;
119  m_remoteProperties.push_back ( RemProperty ( name , std::make_pair ( rsvc , nam ) ) ) ;
120  return p ;
121 }
122 // ====================================================================
123 // get the property by name form the proposed list
124 // ====================================================================
126 ( const std::string& name ,
127  const std::vector<Property*>& props ) const
128 {
129  Properties::const_iterator it =
130  std::find_if ( props.begin() , props.end() , PropByName( name ) ) ;
131  if ( props.end() != it ) { return *it ; } // RETURN
132  return 0 ; // RETURN
133 }
134 // ====================================================================
135 // retrieve the property by name
136 // ====================================================================
138 ( const std::string& name ) const
139 {
140  // local property ?
141  Property* lp = property ( name , m_properties ) ;
142  if ( 0 != lp ) { return lp ; } // RETURN
143  // look for remote property
144  Nocase cmp ;
145  for ( RemoteProperties::const_iterator it = m_remoteProperties.begin() ;
146  m_remoteProperties.end() != it ; ++it )
147  {
148  if ( !cmp(it->first,name) ) { continue ; } // CONTINUE
149  const IProperty* p = it->second.first ;
150  if ( 0 == p ) { continue ; } // CONITNUE
151  return property ( it->second.second , p->getProperties() ) ; // RETURN
152  }
153  return 0 ; // RETURN
154 }
155 // ====================================================================
156 /* set a property from another property
157  * Implementation of IProperty::setProperty
158  */
159 // =====================================================================
161 {
162  Property* pp = property( p.name() ) ;
163  if ( 0 == pp ) { return StatusCode::FAILURE ; } // RETURN
164  //
165  try
166  { if ( !pp->assign(p) ) { return StatusCode::FAILURE ; } } // RETURN
167  catch(...) { return StatusCode::FAILURE ; } // RETURN
168  //
169  return StatusCode::SUCCESS; // Property value set
170 }
171 // ====================================================================
172 /* set a property from the stream
173  * Implementation of IProperty::setProperty
174  */
175 // =====================================================================
177 PropertyMgr::setProperty( const std::string& i )
178 {
179  std::string name ;
180  std::string value ;
181  StatusCode sc = Gaudi::Parsers::parse( name , value , i ) ;
182  if ( sc.isFailure() ) { return sc ; }
183  return setProperty ( name , value ) ;
184 }
185 // =====================================================================
186 /* set a property from the string
187  * Implementation of IProperty::setProperty
188  */
189 // =====================================================================
191 PropertyMgr::setProperty( const std::string& n, const std::string& v )
192 {
193  Property* p = property( n ) ;
194  if ( 0 == p ) { return StatusCode::FAILURE ; } // RETURN
195  bool result = p->fromString( v ) != 0 ;
196  return result ? StatusCode::SUCCESS : StatusCode::FAILURE ;
197 }
198 // =====================================================================
199 /* Retrieve the value of a property
200  * Implementation of IProperty::getProperty
201  */
202 // =====================================================================
204 {
205  try
206  {
207  const Property* pp = property( p->name() ) ;
208  if ( 0 == pp ) { return StatusCode::FAILURE ; } // RETURN
209  if ( !pp->load( *p ) ) { return StatusCode::FAILURE ; } // RETURN
210  }
211  catch ( ... ) { return StatusCode::FAILURE; } // RETURN
212  return StatusCode::SUCCESS ; // RETURN
213 }
214 // =====================================================================
215 /* Get the property by name
216  * Implementation of IProperty::getProperty
217  */
218 // =====================================================================
219 const Property& PropertyMgr::getProperty( const std::string& name) const
220 {
221  const Property* p = property( name ) ;
222  if ( 0 != p ) { return *p ; } // RETURN
223  //
224  throw std::out_of_range( "Property "+name+" not found." ); // Not found
225 }
226 // =====================================================================
227 /* Get the property by name
228  * Implementation of IProperty::getProperty
229  */
230 // =====================================================================
232 ( const std::string& n ,
233  std::string& v ) const
234 {
235  // get the property
236  const Property* p = property( n ) ;
237  if ( 0 == p ) { return StatusCode::FAILURE ; }
238  // convert the value into the string
239  v = p->toString() ;
240  //
241  return StatusCode::SUCCESS ;
242 }
243 // =====================================================================
244 // Implementation of IProperty::getProperties
245 // =====================================================================
246 const std::vector<Property*>&
248 // =====================================================================
249 // Implementation of IInterface::queryInterface
250 // =====================================================================
252  // try local interfaces
253  StatusCode sc= base_class::queryInterface(iid, pinterface);
254  if (sc.isSuccess()) return sc;
255  // fall back on the owner
256  return (0 != m_pOuter)? m_pOuter->queryInterface(iid, pinterface)
257  : sc; // FAILURE
258 }
259 // =====================================================================
260 // The END
261 // =====================================================================
StatusCode setProperty(const Property &p)
set the property form another property
IInterface * m_pOuter
Interface hub reference (ApplicationMgr)
Definition: PropertyMgr.h:169
virtual bool assign(const Property &source)=0
import the property value form the source
virtual const std::vector< Property * > & getProperties() const =0
Get list of properties.
virtual std::string toString() const =0
value -> string
const std::string & name() const
property name
Definition: Property.h:47
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:75
Properties m_todelete
Properties to be deleted.
Definition: PropertyMgr.h:165
StatusCode parse(GaudiUtils::HashMap< K, V > &result, const std::string &input)
Basic parser for the types of HashMap used in DODBasicMapper.
virtual bool load(Property &dest) const =0
export the property value to the destination
Property manager helper class.
Definition: PropertyMgr.h:38
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:85
PropertyMgr(IInterface *iface=0)
constructor from the interface
Definition: PropertyMgr.cpp:52
StatusCode queryInterface(const InterfaceID &iid, void **pinterface)
Set the void** to the pointer to the requested interface of the instance.
Gaudi::InterfaceId< IInterface, 0, 0 > iid
Interface ID.
Definition: IInterface.h:164
Base class used to implement the interfaces.
Definition: implements.h:133
Interface ID class.
Definition: IInterface.h:55
Properties m_properties
Collection of all declared properties.
Definition: PropertyMgr.h:161
RemoteProperties m_remoteProperties
Collection of all declared remote properties.
Definition: PropertyMgr.h:163
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:30
Definition of the basic interface.
Definition: IInterface.h:160
virtual ~PropertyMgr()
virtual destructor
Definition: PropertyMgr.cpp:81
std::pair< std::string, std::pair< IProperty *, std::string > > RemProperty
Definition: PropertyMgr.h:155
PropertyMgr & operator=(const PropertyMgr &)
Definition: PropertyMgr.cpp:90
StatusCode getProperty(Property *p) const
get the property
const std::vector< Property * > & getProperties() const
get all properties
Property base class allowing Property* collections to be "homogeneous".
Definition: Property.h:43
virtual unsigned long addRef()=0
Increment the reference count of Interface instance.
Property * declareRemoteProperty(const std::string &name, IProperty *rsvc, const std::string &rname="")
Declare a remote property.
The IProperty is the basic interface for all components which have properties that can be set or get...
Definition: IProperty.h:22
list i
Definition: ana.py:128
virtual Property * clone() const =0
clone: "virtual constructor"
void toupper(std::string &s)
Property * property(const std::string &name) const
virtual StatusCode queryInterface(const InterfaceID &ti, void **pp)=0
Set the void** to the pointer to the requested interface of the instance.
virtual StatusCode fromString(const std::string &value)=0
string -> value