/usr/include/libint2/any.h is in libint2-dev 2.1.0~beta2-2.
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 | // taken from http://codereview.stackexchange.com/questions/20058/c11-any-class
// and modified slightly.
// this code is in public domain
#ifndef _libint2_include_libint2_any_h_
#define _libint2_include_libint2_any_h_
#include <type_traits>
#include <utility>
#include <typeinfo>
#include <string>
#include <cassert>
namespace libint2 {
template<class T>
using StorageType = typename std::decay<T>::type;
/// emulates boost::any
struct Any
{
bool is_null() const { return !ptr; }
bool not_null() const { return ptr; }
template<typename U> Any(U&& value)
: ptr(new Derived<StorageType<U>>(std::forward<U>(value)))
{
}
template<class U> bool is() const
{
typedef StorageType<U> T;
auto derived = dynamic_cast<Derived<T>*> (ptr);
return derived;
}
/// \note if NDEBUG is not defined, will throw if U is not the stored type
template<class U>
StorageType<U>& as()
{
typedef StorageType<U> T;
#if not defined(NDEBUG)
auto derived = dynamic_cast<Derived<T>*> (ptr);
if (!derived)
throw std::bad_cast();
#else // NDEBUG
auto derived = static_cast<Derived<T>*> (ptr);
#endif
return derived->value;
}
/// \note if NDEBUG is not defined, will throw if U is not the stored type
template<class U>
const StorageType<U>& as() const
{
typedef StorageType<U> T;
#if not defined(NDEBUG)
auto derived = dynamic_cast<Derived<T>*> (ptr);
if (!derived)
throw std::bad_cast();
#else // NDEBUG
auto derived = static_cast<Derived<T>*> (ptr);
#endif
return derived->value;
}
template<class U>
operator U()
{
return as<StorageType<U>>();
}
Any()
: ptr(nullptr)
{
}
Any(Any& that)
: ptr(that.clone())
{
}
Any(Any&& that)
: ptr(that.ptr)
{
that.ptr = nullptr;
}
Any(const Any& that)
: ptr(that.clone())
{
}
Any(const Any&& that)
: ptr(that.clone())
{
}
Any& operator=(const Any& a)
{
if (ptr == a.ptr)
return *this;
auto old_ptr = ptr;
ptr = a.clone();
if (old_ptr)
delete old_ptr;
return *this;
}
Any& operator=(Any&& a)
{
if (ptr == a.ptr)
return *this;
std::swap(ptr, a.ptr);
return *this;
}
~Any()
{
if (ptr)
delete ptr;
}
private:
struct Base
{
virtual ~Base() {}
virtual Base* clone() const = 0;
};
template<typename T>
struct Derived : Base
{
template<typename U> Derived(U&& value) : value(std::forward<U>(value)) { }
T value;
Base* clone() const { return new Derived<T>(value); }
};
Base* clone() const
{
if (ptr)
return ptr->clone();
else
return nullptr;
}
Base* ptr;
};
} // namespace libint2
#endif // header guard
|