OpenWalnut  1.5.0dev
WPropertyList.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WPROPERTYLIST_H
26 #define WPROPERTYLIST_H
27 
28 #include <memory>
29 #include <string>
30 
31 
32 #include "WPropertyGroupBase.h"
33 #include "WPropertyTypes.h"
34 
35 /**
36  * This is a dynamic property list. With its help, users can dynamically add items.
37  *
38  * \tparam T This is a property type. The list will then contain several of these properties.
39  */
40 template< typename T >
42 {
43 public:
44  /**
45  * The type of property to store in this list.
46  */
47  typedef T ValueType;
48 
49  /**
50  * Abbreviation for this template with the current value type.
51  */
53 
54  /**
55  * Convenience typedef for a std::shared_ptr< WPropertyList >.
56  */
57  typedef std::shared_ptr< WPropertyList< ValueType > > SPtr;
58 
59  /**
60  * Convenience typedef for a std::shared_ptr< const WPropertyList >.
61  */
62  typedef std::shared_ptr< const WPropertyList< ValueType > > ConstSPtr;
63 
64  /**
65  * Create an empty named property.
66  *
67  * \param name the name of the property
68  * \param description the description of the property
69  */
70  WPropertyList( std::string name, std::string description ):
71  WPropertyGroupBase( name, description )
72  {
73  // nothing to do here. The list is initially empty
74  }
75 
76  /**
77  * Copy constructor. Creates a deep copy of this property. As boost::signals2 and condition variables are non-copyable, new instances get
78  * created. The subscriptions to a signal are LOST as well as all listeners to a condition.
79  *
80  * \param from the instance to copy.
81  */
82  explicit WPropertyList( const WPropertyListType& from ):
83  WPropertyGroupBase( from )
84  {
85  // this created a NEW update condition and NEW property instances (clones)
86  }
87 
88  /**
89  * Destructor.
90  */
91  virtual ~WPropertyList()
92  {
93  // storing structure is destroyed automatically in WPropertyGroupBase.
94  }
95 
96  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
97  // The WPropertyList specific stuff
98  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
99 
100 
101 
102 
103 
104 
105  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
106  // The WPropertyBase specific stuff
107  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
108 
109  /**
110  * This method clones a property and returns the clone. It does a deep copy and, in contrast to a copy constructor, creates property with the
111  * correct type without explicitly requiring the user to specify it. It creates a NEW change condition and change signal. This means, alls
112  * subscribed signal handlers are NOT copied.
113  *
114  * \note this simply ensures the copy constructor of the runtime type is issued.
115  *
116  * \return the deep clone of this property.
117  */
119  {
120  // just use the copy constructor
121  return WPropertyListType::SPtr( new WPropertyListType( *this ) );
122  }
123 
124  /**
125  * Gets the real WPropertyVariable type of this instance.
126  *
127  * \return the real type.
128  */
129  virtual PROPERTY_TYPE getType() const
130  {
131  return PV_LIST;
132  }
133 
134  /**
135  * This methods allows properties to be set by a string value. This is especially useful when a property is only available as string and the
136  * real type of the property is unknown. This is a shortcut for casting the property and then setting the lexically casted value.
137  *
138  * \param value the new value to set.
139  *
140  * \return true if value could be set.
141  */
142  virtual bool setAsString( std::string value );
143 
144  /**
145  * Returns the current value as a string. This is useful for debugging or project files. It is not implemented as << operator, since the <<
146  * should also print min/max constraints and so on. This simply is the value.
147  *
148  * \return the value as a string.
149  */
150  virtual std::string getAsString()
151  {
152  // lock, unlocked if l looses focus
154  return "";
155  }
156 
157  /**
158  * Sets the value from the specified property to this one. This is especially useful to copy a value without explicitly casting/knowing the
159  * dynamic type of the property.
160  *
161  * \param value the new value.
162  *
163  * \return true if the value has been accepted.
164  */
165  virtual bool set( std::shared_ptr< WPropertyBase > value )
166  {
167  // is this the same type as we are?
168  typename WPropertyListType::SPtr v = std::dynamic_pointer_cast< WPropertyListType >( value );
169  if( !v )
170  {
171  // it is not a WPropertyList with the same type
172  return false;
173  }
174  }
175 
176 protected:
177 private:
178 };
179 
180 template< typename T >
181 bool WPropertyList< T >::setAsString( std::string /*value*/ )
182 {
183  return false;
184 }
185 
186 #endif // WPROPERTYLIST_H
187 
std::shared_ptr< WPropertyBase > SPtr
Convenience typedef for a std::shared_ptr< WPropertyBase >
Definition: WPropertyBase.h:53
This is the base class and interface for property groups.
PropertySharedContainerType m_properties
The set of proerties.
This is a dynamic property list.
Definition: WPropertyList.h:42
WPropertyList< ValueType > WPropertyListType
Abbreviation for this template with the current value type.
Definition: WPropertyList.h:52
T ValueType
The type of property to store in this list.
Definition: WPropertyList.h:47
std::shared_ptr< const WPropertyList< ValueType > > ConstSPtr
Convenience typedef for a std::shared_ptr< const WPropertyList >.
Definition: WPropertyList.h:62
virtual PROPERTY_TYPE getType() const
Gets the real WPropertyVariable type of this instance.
virtual bool set(std::shared_ptr< WPropertyBase > value)
Sets the value from the specified property to this one.
virtual WPropertyBase::SPtr clone()
This method clones a property and returns the clone.
WPropertyList(const WPropertyListType &from)
Copy constructor.
Definition: WPropertyList.h:82
virtual ~WPropertyList()
Destructor.
Definition: WPropertyList.h:91
virtual std::string getAsString()
Returns the current value as a string.
virtual bool setAsString(std::string value)
This methods allows properties to be set by a string value.
WPropertyList(std::string name, std::string description)
Create an empty named property.
Definition: WPropertyList.h:70
std::shared_ptr< WPropertyList< ValueType > > SPtr
Convenience typedef for a std::shared_ptr< WPropertyList >.
Definition: WPropertyList.h:57
std::shared_ptr< WSharedObjectTicketRead< T > > ReadTicket
Type for read tickets.
Definition: WSharedObject.h:65
ReadTicket getReadTicket() const
Returns a ticket to get read access to the contained data.