/usr/include/vigra/any.hxx is in libvigraimpex-dev 1.10.0+git20160211.167be93+dfsg-5ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 | /************************************************************************/
/* */
/* Copyright 2014-2015 by Ullrich Koethe */
/* */
/* This file is part of the VIGRA2 computer vision library. */
/* The VIGRA2 Website is */
/* http://ukoethe.github.io/vigra2 */
/* Please direct questions, bug reports, and contributions to */
/* ullrich.koethe@iwr.uni-heidelberg.de or */
/* vigra@informatik.uni-hamburg.de */
/* */
/* Permission is hereby granted, free of charge, to any person */
/* obtaining a copy of this software and associated documentation */
/* files (the "Software"), to deal in the Software without */
/* restriction, including without limitation the rights to use, */
/* copy, modify, merge, publish, distribute, sublicense, and/or */
/* sell copies of the Software, and to permit persons to whom the */
/* Software is furnished to do so, subject to the following */
/* conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the */
/* Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES */
/* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
/* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */
/* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
/* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */
/* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR */
/* OTHER DEALINGS IN THE SOFTWARE. */
/* */
/************************************************************************/
#pragma once
#ifndef VIGRA_ANY_HXX
#define VIGRA_ANY_HXX
#include "config.hxx"
#include "error.hxx"
#include <typeinfo>
#include <type_traits>
namespace vigra {
namespace detail {
struct AnyHandle
{
AnyHandle() {}
virtual ~AnyHandle() {}
virtual const std::type_info & type() const = 0;
virtual AnyHandle * clone() const = 0;
virtual bool equal(AnyHandle const *) const = 0;
private:
AnyHandle(AnyHandle const &);
AnyHandle & operator=(AnyHandle const &);
};
template <class T>
struct TypedAnyHandle
: public AnyHandle
{
T value_;
TypedAnyHandle(T const & t)
: value_(t)
{}
const std::type_info & type() const
{
return typeid(T);
}
AnyHandle * clone() const
{
return new TypedAnyHandle(value_);
}
bool equal(AnyHandle const * h) const
{
TypedAnyHandle const * ptr = dynamic_cast<TypedAnyHandle const *>(h);
return ptr != 0 && value_ == ptr->value_;
}
};
struct ConvertibleAnyHandle
: public AnyHandle
{
template <class T>
struct TypeTag {};
ConvertibleAnyHandle() {}
virtual signed char cast(TypeTag<signed char>) const = 0;
virtual signed short cast(TypeTag<signed short>) const = 0;
virtual signed int cast(TypeTag<signed int>) const = 0;
virtual signed long cast(TypeTag<signed long>) const = 0;
virtual signed long long cast(TypeTag<signed long long>) const = 0;
virtual unsigned char cast(TypeTag<unsigned char>) const = 0;
virtual unsigned short cast(TypeTag<unsigned short>) const = 0;
virtual unsigned int cast(TypeTag<unsigned int>) const = 0;
virtual unsigned long cast(TypeTag<unsigned long>) const = 0;
virtual unsigned long long cast(TypeTag<unsigned long long>) const = 0;
virtual float cast(TypeTag<float>) const = 0;
virtual double cast(TypeTag<double>) const = 0;
virtual long double cast(TypeTag<long double>) const = 0;
};
#define VIGRA_ANY_OF_CONVERTIBLE(TYPE) \
template <> \
struct TypedAnyHandle<TYPE> \
: public ConvertibleAnyHandle \
{ \
TYPE value_; \
\
TypedAnyHandle(TYPE const & t) \
: value_(t) \
{} \
\
const std::type_info & type() const \
{ \
return typeid(value_); \
} \
\
AnyHandle * clone() const \
{ \
return new TypedAnyHandle(value_); \
} \
\
bool equal(AnyHandle const * h) const \
{ \
TypedAnyHandle const * ptr = dynamic_cast<TypedAnyHandle const *>(h); \
return ptr != 0 && value_ == ptr->value_; \
} \
\
virtual signed char cast(TypeTag<signed char>) const \
{ return static_cast<signed char>(value_); } \
virtual signed short cast(TypeTag<signed short>) const \
{ return static_cast<signed short>(value_); } \
virtual signed int cast(TypeTag<signed int>) const \
{ return static_cast<signed int>(value_); } \
virtual signed long cast(TypeTag<signed long>) const \
{ return static_cast<signed long>(value_); } \
virtual signed long long cast(TypeTag<signed long long>) const \
{ return static_cast<signed long long>(value_); } \
\
virtual unsigned char cast(TypeTag<unsigned char>) const \
{ return static_cast<unsigned char>(value_); } \
virtual unsigned short cast(TypeTag<unsigned short>) const \
{ return static_cast<unsigned short>(value_); } \
virtual unsigned int cast(TypeTag<unsigned int>) const \
{ return static_cast<unsigned int>(value_); } \
virtual unsigned long cast(TypeTag<unsigned long>) const \
{ return static_cast<unsigned long>(value_); } \
virtual unsigned long long cast(TypeTag<unsigned long long>) const \
{ return static_cast<unsigned long long>(value_); } \
\
virtual float cast(TypeTag<float>) const \
{ return static_cast<float>(value_); } \
virtual double cast(TypeTag<double>) const \
{ return static_cast<double>(value_); } \
virtual long double cast(TypeTag<long double>) const \
{ return static_cast<long double>(value_); } \
};
VIGRA_ANY_OF_CONVERTIBLE(signed char )
VIGRA_ANY_OF_CONVERTIBLE(signed short )
VIGRA_ANY_OF_CONVERTIBLE(signed int )
VIGRA_ANY_OF_CONVERTIBLE(signed long )
VIGRA_ANY_OF_CONVERTIBLE(signed long long)
VIGRA_ANY_OF_CONVERTIBLE(unsigned char )
VIGRA_ANY_OF_CONVERTIBLE(unsigned short )
VIGRA_ANY_OF_CONVERTIBLE(unsigned int )
VIGRA_ANY_OF_CONVERTIBLE(unsigned long )
VIGRA_ANY_OF_CONVERTIBLE(unsigned long long)
VIGRA_ANY_OF_CONVERTIBLE(float )
VIGRA_ANY_OF_CONVERTIBLE(double )
VIGRA_ANY_OF_CONVERTIBLE(long double)
#undef VIGRA_ANY_OF_CONVERTIBLE
} // namespace detail
/** \brief Typesafe storage of arbitrary values.
Items are always stored by value, but it is of course possible
to store pointers and smart pointers.
<b>Usage:</b>
\code
Any a(10); // store integer '10'
assert(a.is_type<int>());
assert(a.is_convertible<int>());
// retrieve the stored value (throws when types don't match)
assert(a.get<int>() == 10);
// change the value
a = 20;
assert(a.get<int>() == 20);
// values of arithmetic types can be converted into each other
// (this is currently not implemented for other types)
assert(a.is_convewrtible<double>());
assert(a.cast<double>() == 20.0);
// delete the stored value
a.release();
assert(a.empty());
assert(a == false);
// store a shared_ptr
typedef std::shared_ptr<int> Ptr;
Any p(Ptr(new int(5))), q = p;
assert(*(p.get<Ptr>()) == 5);
// the addresses of the elements in p and q are the same
assert(p.get<Ptr>().get() == p.get<Ptr>().get());
\endcode
*/
class Any
{
VIGRA_UNIQUE_PTR<detail::AnyHandle> handle_;
public:
/** Construct empty 'Any' object.
*/
Any()
: handle_((detail::AnyHandle*)0)
{}
/** Construct 'Any' object holding the given value.
*/
template <class T>
Any(T const & t)
: handle_(new detail::TypedAnyHandle<T>(t))
{}
/** Construct 'Any' object holding a copy of other's value.
*/
Any(Any const & other)
: handle_(bool(other) ? other.handle_->clone() : (detail::AnyHandle*)0)
{}
/** Assign the given value to this 'Any' object
(overwrites the old value, regardless of types).
*/
template <class T>
Any & operator=(T const & t)
{
handle_.reset(new detail::TypedAnyHandle<T>(t));
return *this;
}
/** Assign a copy of other's value to this 'Any' object
(overwrites the old value, regardless of types).
*/
Any & operator=(Any const & other)
{
if(this != &other)
handle_.reset(bool(other) ? other.handle_->clone() : (detail::AnyHandle*)0);
return *this;
}
/** Delete the contained object (make this 'Any' object empty).
*/
void release()
{
handle_.release();
}
/** Exchange the value of this object with other's.
*/
void swap(Any & other)
{
#ifdef VIGRA_NO_UNIQUE_PTR // fallback for old compilers
detail::AnyHandle *t = handle_.release(),
*o = other.handle_.release();
handle_.reset(o);
other.handle_.reset(t);
#else
handle_.swap(other.handle_);
#endif
}
/** Exchange the value of objects l and r.
*/
friend void swap(Any & l, Any & r)
{
l.swap(r);
}
/** Check if this object contains the same type and value as other.
Also true if both 'Any' objects are empty.
*/
bool operator==(Any const & other) const
{
return (handle_.get() == 0 && other.handle_.get() == 0) ||
(handle_.get() != 0 && handle_->equal(other.handle_.get()));
}
/** Check if this object differs from other by type or value.
*/
bool operator!=(Any const & other) const
{
return !operator==(other);
}
bool operator==(bool other) const
{
return bool(*this) == other;
}
bool operator!=(bool other) const
{
return bool(*this) != other;
}
/** Convert 'Any' to <tt>false</tt> if this object is empty, <tt>true</tt> otherwise.
*/
operator bool() const
{
return handle_.get() != 0;
}
/** Check if this object is empty (holds no value).
*/
bool empty() const
{
return handle_.get() == 0;
}
/** Check if this object holds a value of the given type.
*/
template <class T>
bool is_type() const
{
return dynamic_cast<detail::TypedAnyHandle<T> const *>(handle_.get()) != 0;
}
/** Check if this object's value is convertible to the given type.
At present, this only succeeds if <tt>T</tt> matches the stored
type exactly or is an arithmetic type convertible from the stored type.
*/
template <class T>
bool is_readable() const
{
return (dynamic_cast<detail::TypedAnyHandle<T> const *>(handle_.get()) != 0) ||
(std::is_arithmetic<T>::value &&
dynamic_cast<detail::ConvertibleAnyHandle const *>(handle_.get()) != 0);
}
/** Read-write access to the contained value. This throws an exception
if the types don't match.
*/
template <class T>
T & get()
{
vigra_precondition(bool(*this), "Any::get(): object empty.");
auto ptr = dynamic_cast<detail::TypedAnyHandle<T> *>(handle_.get());
vigra_precondition(ptr != 0, "Any::get(): object is not an instance of the target type.");
return ptr->value_;
}
/** Read-only access to the contained value. This throws an exception
if the types don't match.
*/
template <class T>
T const & get() const
{
vigra_precondition(bool(*this), "Any::get(): object empty.");
auto ptr = dynamic_cast<detail::TypedAnyHandle<T> const *>(handle_.get());
vigra_precondition(ptr != 0, "Any::get(): object is not an instance of the target type.");
return ptr->value_;
}
/** By-value access to the stored value. This throws an exception
if the stored type doesn't match <tt>T</tt> and <tt>T</tt> is
not an arithmetic type.
*/
template <class T>
T read() const
{
vigra_precondition(bool(*this), "Any::read(): object empty.");
auto ptr1 = dynamic_cast<detail::TypedAnyHandle<T> const *>(handle_.get());
if(ptr1 != 0)
return ptr1->value_;
auto ptr2 = dynamic_cast<detail::ConvertibleAnyHandle const *>(handle_.get());
vigra_precondition(ptr2 != 0, "Any::read(): object is not covertible to the target type.");
return ptr2->cast(detail::ConvertibleAnyHandle::TypeTag<T>());
}
};
} // namespace vigra
#endif // VIGRA_ANY_HXX
|