forked from Kitware/kwiver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
any.h
351 lines (309 loc) · 9.16 KB
/
any.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*ckwg +29
* Copyright 2016-2017 by Kitware, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither name of Kitware, Inc. nor the names of any contributors may be used
* to endorse or promote products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef KWIVER_VITAL_ANY_H
#define KWIVER_VITAL_ANY_H
#include <vital/vital_config.h>
#include <vital/util/demangle.h>
#include <algorithm>
#include <typeinfo>
namespace kwiver {
namespace vital {
// ----------------------------------------------------------------
/**
* @brief Class that contains *any* data type.
*
* This class represents a single data item of indeterminate type.
*/
class any
{
public:
/**
* @brief Create empty object.
*
*/
any() noexcept
: m_content( 0 )
{ }
/**
* @brief Create new object containing typed value.
*
* This CTOR creates a new object that holds the specified type and
* value.
*
* @param value Data item (value and type) to be held in new object.
*/
template < typename T >
any( const T& value )
: m_content( new internal_typed< T >( value ))
{ }
/**
* @brief Create new object form existing object.
*
* This copy CTOR creates a new object that contains the data value
* and type of another any object.
*
* @param other Object to copy type and value from.
*/
any( any const& other )
: m_content( other.m_content ? other.m_content->clone() : 0 )
{ }
~any() noexcept
{
delete m_content;
}
// ------------------------------------------------------------------
/**
* @brief Swap value and type.
*
* This method swaps the specified value and type with this item.
*
* @param rhs Item to swap into this.
*
* @return Modified current (this) object.
*/
any& swap(any& rhs) noexcept
{
std::swap(m_content, rhs.m_content);
return *this;
}
// ------------------------------------------------------------------
/**
* @brief Assignment operator.
*
* This operator assigns the specified type and value to this object.
*
* @param rhs New value to assign to this object.
*
* @return Reference to this object.
*/
template < typename T >
any& operator=( T const& rhs )
{
any( rhs ).swap( *this );
return *this;
}
// ------------------------------------------------------------------
/**
* @brief Assignment operator.
*
* This operator assigns the specified any object to this object.
*
* @param rhs New value to assign to this object.
*
* @return Reference to this object.
*/
any& operator=( any rhs )
{
any( rhs ).swap( *this );
return *this;
}
// ------------------------------------------------------------------
/**
* @brief Determine if this object has a value.
*
* This method returns \b true if this object has not been assigned
* a value.
*
* @return \b true if no value in object, \b false if there is a
* value.
*/
bool empty() const noexcept
{
return ! m_content;
}
// ------------------------------------------------------------------
/**
* @brief Remove value from object.
*
* This method removes the current type and value from this
* object. The empty() method will return /b true after this call.
*
*/
void clear() noexcept
{
any().swap( *this );
}
// ------------------------------------------------------------------
/**
* @brief Get typeid for current value.
*
* This method returns the std::type_info for the item contained in
* this object. If this object is empty(), then the type info for \b
* void is returned.
*
* You can get the type name string from the following, but the name
* string may not be all that helpful.
*
\code
kwiver::vital::any any_double(3.14159);
std::cout << "Type name: " << any_double.type().name() << std::endl;
\endcode
*
* @return The type info for the datum in this object is returned.
*/
std::type_info const& type() const noexcept
{
return m_content ? m_content->type() : typeid(void);
}
/// Return demangled name of type contained in this object.
/**
* This method returns the demangled name of type contained in this
* object.
*
* @return Demangled type name string.
*/
std::string type_name() const noexcept
{
return demangle( this->type().name() );
}
private:
// ------------------------------------------------------------------
// Base class for representing content
class internal
{
public:
virtual ~internal() { }
virtual std::type_info const& type() const noexcept = 0;
virtual internal* clone() const = 0;
};
// ------------------------------------------------------------------
// type specific content
template < typename T > class internal_typed : public internal
{
public:
internal_typed( T const& value ) : m_any_data( value ) { }
virtual std::type_info const& type() const noexcept
{
return typeid(T);
}
virtual internal* clone() const
{
return new internal_typed( m_any_data );
}
T m_any_data;
// -- NOT IMPLEMENTED --
internal_typed& operator=( const internal_typed& ) = delete;
};
private:
template < typename T >
friend T* any_cast( any * aval ) noexcept;
template < typename T >
friend T any_cast(any const& aval);
internal* m_content;
};
// ==================================================================
class bad_any_cast : public std::bad_cast
{
public:
/// Create bad cast exception;
/**
* This is the CTOR for the bnad any cast exception. A message is
* created from the supplied mangled type names.
*
*
* @param from_type Mangled type name.
* @param to_type Mangled type name.
*/
bad_any_cast( std::string const& from_type,
std::string const& to_type )
{
// Construct helpful message
m_message = "vital::bad_any_cast: failed conversion using kwiver::vital::any_cast from type \""
+ demangle( from_type ) + "\" to type \"" + demangle( to_type ) + "\"";
}
virtual ~bad_any_cast() noexcept {}
virtual const char * what() const noexcept
{
return m_message.c_str();
}
private:
std::string m_message;
};
// ==================================================================
// Casting functions
//
/// Get value from a container.
/**
* This method returns a typed value from the any container. If the
* conversion can not be completed, then an exception is thrown.
*
* @param aval Object that has the value.
*
* @return Value from object as specified type.
*/
template < typename T >
inline T*
any_cast( any* operand ) noexcept
{
if ( operand && ( operand->type() == typeid( T ) ) )
{
return &static_cast< any::internal_typed< T >* > ( operand->m_content )->m_any_data;
}
return 0;
}
// ------------------------------------------------------------------
/// Get value from a container.
/**
* This method returns a typed value from the any container. If the
* conversion can not be completed, then an exception is thrown.
*
* @param aval Object that has the value.
*
* @return Value from object as specified type.
*/
template < typename T >
inline const T*
any_cast( any const* operand ) noexcept
{
return any_cast< T > ( const_cast< any* > ( operand ) );
}
// ------------------------------------------------------------------
/// Get value from a container.
/**
* This method returns a typed value from the any container. If the
* conversion can not be completed, then an exception is thrown.
*
* @param aval Object that has the value.
*
* @return Value from object as specified type.
*/
template < typename T >
inline T
any_cast( any const& aval )
{
// Is the type requested compatible with the type represented.
if ( typeid( T ) == aval.m_content->type() )
{
return ( ( any::internal_typed< T >* )aval.m_content )->m_any_data;
}
throw bad_any_cast( aval.m_content->type().name(), typeid( T ).name() );
}
} } // end namespace
#endif /* KWIVER_VITAL_ANY_H */