~ubuntu-branches/debian/sid/gsmartcontrol/sid

« back to all changes in this revision

Viewing changes to src/rmn/resource_data_any.h

  • Committer: Package Import Robot
  • Author(s): Giuseppe Iuculano
  • Date: 2013-05-31 11:41:52 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20130531114152-5ljhkuswwpt4kdwo
Tags: 0.8.7-1
* [314881d] Updated debian/watch
* [18ebada] Imported Upstream version 0.8.7
* [c2a1f1b] debian/rules: Provide build-arch and build-indep
* [d3036a4] Enabled Hardening Options
* [2edfb87] Refreshed patches and removed patches apllied upstream
* [ac3b953] Bump to standard versions 3.9.4
* [292c276] Remove quilt from depends

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/**************************************************************************
2
2
 Copyright:
3
 
      (C) 2008 - 2011  Alexander Shaduri <ashaduri 'at' gmail.com>
 
3
      (C) 2008 - 2012  Alexander Shaduri <ashaduri 'at' gmail.com>
4
4
 
5
5
 License:
6
6
 
20
20
    misrepresented as being the original software.
21
21
 3. This notice may not be removed or altered from any source distribution.
22
22
***************************************************************************/
 
23
/// \file
 
24
/// \author Alexander Shaduri
 
25
/// \ingroup rmn
 
26
/// \weakgroup rmn
 
27
/// @{
23
28
 
24
29
#ifndef RMN_RESOURCE_DATA_ANY_H
25
30
#define RMN_RESOURCE_DATA_ANY_H
35
40
#include "resource_exception.h"
36
41
 
37
42
 
38
 
// any-type data for resource_node
 
43
/**
 
44
\file
 
45
Any-type data for resource_node
 
46
*/
39
47
 
40
48
 
41
49
namespace rmn {
42
50
 
43
51
 
44
52
 
45
 
 
46
 
// helper class for <<.
 
53
/// Helper class for operator\<\< with std::ostream.
47
54
template<class T>
48
55
struct ResourceDataAnyDumper {
49
56
        ResourceDataAnyDumper(T* o) : obj(o) { }
50
57
 
 
58
        /// Dump resource data into \c os
51
59
        void dump(std::ostream& os) const
52
60
        {
53
61
                os << obj->data_.to_stream();
54
62
        }
55
63
 
56
 
        T* obj;
 
64
        T* obj;  ///< ResourceDataAny object
57
65
};
58
66
 
59
67
 
60
68
 
 
69
/// Output the data into \c os
61
70
template <class T>
62
71
inline std::ostream& operator<<(std::ostream& os, const ResourceDataAnyDumper<T>& dumper)
63
72
{
68
77
 
69
78
 
70
79
 
71
 
 
 
80
/// Resource data which can hold variables of any type.
72
81
template<class LockingPolicy>
73
82
class ResourceDataAny {
74
83
 
75
 
        typedef ResourceDataAny<LockingPolicy> self_type;
76
 
 
 
84
        typedef ResourceDataAny<LockingPolicy> self_type;  ///< Self type
77
85
 
78
86
        public:
79
87
 
 
88
                /// Constructor
80
89
                ResourceDataAny()
81
90
#if defined RMN_TYPE_TRACKING && RMN_TYPE_TRACKING
82
91
                        : type_(T_EMPTY)
84
93
                { }
85
94
 
86
95
 
87
 
 
 
96
                /// Copy data from \c src node
88
97
                template<class T>
89
 
                bool copy_data_from(const T& src)  // src node
 
98
                bool copy_data_from(const T& src)
90
99
                {
91
100
                        if (!src)
92
101
                                return false;
99
108
                }
100
109
 
101
110
 
 
111
                /// Check whether data is empty
102
112
                bool data_is_empty() const
103
113
                {
104
114
                        return data_.empty();
105
115
                }
106
116
 
107
117
 
 
118
                /// Clear the data, making it empty
108
119
                void clear_data()
109
120
                {
110
121
                        data_.clear();
114
125
                }
115
126
 
116
127
 
117
 
 
 
128
                /// Set data of any type
118
129
                template<typename T>
119
130
                inline bool set_data(T data)
120
131
                {
126
137
                }
127
138
 
128
139
 
129
 
                // const char* -> std::string specialization
 
140
                /// const char* -\> std::string specialization
130
141
                inline bool set_data(const char* data)
131
142
                {
132
143
                        data_ = std::string(data);
137
148
                }
138
149
 
139
150
 
140
 
                // this function works only if either RTTI or type tracking is enabled
 
151
                /// \fn bool data_is_type() const
 
152
                /// Check whether data is of type \c T.
 
153
                /// This function is available only if either RTTI or type tracking is enabled.
141
154
#if !(defined DISABLE_RTTI && DISABLE_RTTI)
142
155
                template<typename T>
143
156
                inline bool data_is_type() const
156
169
 
157
170
 
158
171
#if defined RMN_TYPE_TRACKING && RMN_TYPE_TRACKING
 
172
                /// Check whether data is of type \c type.
 
173
                /// This function is available only if type tracking is enabled.
159
174
                inline bool data_is_type(node_data_type type) const
160
175
                {
161
176
                        return type == type_;
162
177
                }
163
178
 
 
179
                /// Get data type.
 
180
                /// This function is available only if type tracking is enabled.
164
181
                inline node_data_type get_type() const
165
182
                {
166
183
                        return type_;
168
185
#endif
169
186
 
170
187
 
 
188
                /// Get data of type \c T.
 
189
                /// \return false if casting failed, or if it's empty or invalid type.
171
190
                template<typename T>
172
 
                inline bool get_data(T& put_it_here) const  // returns false if cast failed
 
191
                inline bool get_data(T& put_it_here) const
173
192
                {
174
193
#if defined RMN_TYPE_TRACKING && RMN_TYPE_TRACKING
175
194
                        if (node_data_type_by_real<T>::type != type_)
179
198
                }
180
199
 
181
200
 
182
 
                // This function throws if:
183
 
                //      * data empty (rmn::empty_data_retrieval);
184
 
                //      * type mismatch (rmn::type_mismatch).
 
201
                /// Return a copy of data of type \c T.
 
202
                /// \throw rmn::empty_data_retrieval Data is empty
 
203
                /// \throw rmn::type_mismatch Type mismatch
185
204
                template<typename T>
186
205
                T get_data() const
187
206
                {
203
222
 
204
223
 
205
224
 
206
 
                // More loose conversion - can convert between C++ built-in types and std::string.
207
 
                // Uses any_convert<>.
 
225
                /// Similar to get_data(), but with looser conversion - can convert between
 
226
                /// C++ built-in types and std::string. Uses hz::any_convert<>.
 
227
                /// \return false if casting failed, or empty or invalid type.
208
228
                template<typename T>
209
 
                inline bool convert_data(T& put_it_here) const  // returns false if cast failed
 
229
                inline bool convert_data(T& put_it_here) const
210
230
                {
211
 
                        return data_.convert(put_it_here);  // returns false if empty or invalid type
 
231
                        return data_.convert(put_it_here);
212
232
                }
213
233
 
214
234
 
215
 
                // This function throws if:
216
 
                //      * data empty (rmn::empty_data_retrieval);
217
 
                //      * type conversion error (rmn::type_convert_error).
 
235
                /// Similar to get_data(), but with looser conversion - can convert between
 
236
                /// C++ built-in types and std::string. Uses hz::any_convert<>.
 
237
                /// \throw rmn::empty_data_retrieval Data is empty
 
238
                /// \throw rmn::type_mismatch Type mismatch
218
239
                template<typename T>
219
240
                T convert_data() const
220
241
                {
242
263
 
243
264
 
244
265
 
245
 
 
246
266
                template<class T>
247
267
                friend struct ResourceDataAnyDumper;
248
268
 
249
269
 
 
270
                /// Return a helper object that can be dumped into ostream.
250
271
                inline ResourceDataAnyDumper<const self_type> dump_data_to_stream() const
251
272
                {
252
273
                        return ResourceDataAnyDumper<const self_type>(this);
256
277
 
257
278
        private:
258
279
 
259
 
                hz::any_type data_;
 
280
                hz::any_type data_;  ///< The data
260
281
 
261
282
#if defined RMN_TYPE_TRACKING && RMN_TYPE_TRACKING
262
 
                node_data_type type_;
 
283
                node_data_type type_;  ///< Type of the data
263
284
#endif
264
285
 
265
286
};
278
299
 
279
300
 
280
301
#endif
 
302
 
 
303
/// @}