/usr/include/range/v3/utility/any.hpp is in librange-v3-dev 0.3.5-1.
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 | /// \file
// Range v3 library
//
// Copyright Eric Niebler 2015-present
//
// Use, modification and distribution is subject to the
// Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// Project home: https://github.com/ericniebler/range-v3
//
#ifndef RANGES_V3_UTILITY_ANY_HPP
#define RANGES_V3_UTILITY_ANY_HPP
#include <memory>
#include <typeinfo>
#include <type_traits>
#include <meta/meta.hpp>
#include <range/v3/range_fwd.hpp>
#include <range/v3/utility/concepts.hpp>
namespace ranges
{
inline namespace v3
{
struct bad_any_cast
: std::bad_cast
{
virtual const char* what() const noexcept override
{
return "bad any_cast";
}
};
struct any;
template<typename T>
meta::if_c<std::is_reference<T>() || Copyable<T>(), T>
any_cast(any &);
template<typename T>
meta::if_c<std::is_reference<T>() || Copyable<T>(), T>
any_cast(any const &);
template<typename T>
meta::if_c<std::is_reference<T>() || Copyable<T>(), T>
any_cast(any &&);
template<typename T>
T * any_cast(any *) noexcept;
template<typename T>
T const * any_cast(any const *) noexcept;
struct any
{
private:
template<typename T>
friend meta::if_c<std::is_reference<T>() || Copyable<T>(), T>
any_cast(any &);
template<typename T>
friend meta::if_c<std::is_reference<T>() || Copyable<T>(), T>
any_cast(any const &);
template<typename T>
friend meta::if_c<std::is_reference<T>() || Copyable<T>(), T>
any_cast(any &&);
template<typename T>
friend T * any_cast(any *) noexcept;
template<typename T>
friend T const * any_cast(any const *) noexcept;
struct interface
{
virtual ~interface()
{}
virtual interface *clone() const = 0;
virtual std::type_info const & type() const noexcept = 0;
};
template<class T>
struct impl final : interface
{
private:
T obj;
public:
impl() = default;
impl(T o)
: obj(std::move(o))
{}
T &get() { return obj; }
T const &get() const { return obj; }
impl *clone() const override
{
return new impl{obj};
}
std::type_info const & type() const noexcept override
{
return typeid(T);
}
};
std::unique_ptr<interface> ptr_;
public:
any() noexcept = default;
template<typename TRef, typename T = detail::decay_t<TRef>,
CONCEPT_REQUIRES_(Copyable<T>() && !Same<T, any>())>
any(TRef &&t)
: ptr_(new impl<T>(static_cast<TRef&&>(t)))
{}
any(any &&) noexcept = default;
any(any const &that)
: ptr_{that.ptr_ ? that.ptr_->clone() : nullptr}
{}
any &operator=(any &&) noexcept = default;
any &operator=(any const &that)
{
ptr_.reset(that.ptr_ ? that.ptr_->clone() : nullptr);
return *this;
}
template<typename TRef, typename T = detail::decay_t<TRef>,
CONCEPT_REQUIRES_(Copyable<T>() && !Same<T, any>())>
any &operator=(TRef &&t)
{
any{static_cast<TRef&&>(t)}.swap(*this);
return *this;
}
void clear() noexcept
{
ptr_.reset();
}
bool empty() const noexcept
{
return !ptr_;
}
std::type_info const & type() const noexcept
{
return ptr_ ? ptr_->type() : typeid(void);
}
void swap(any &that) noexcept
{
ptr_.swap(that.ptr_);
}
friend void swap(any &x, any &y) noexcept
{
x.swap(y);
}
};
/// \throw bad_any_cast
template<typename T>
meta::if_c<std::is_reference<T>() || Copyable<T>(), T>
any_cast(any &x)
{
if(x.type() != typeid(detail::decay_t<T>))
throw bad_any_cast{};
return static_cast<any::impl<detail::decay_t<T>>*>(x.ptr_.get())->get();
}
/// \overload
template<typename T>
meta::if_c<std::is_reference<T>() || Copyable<T>(), T>
any_cast(any const &x)
{
if(x.type() != typeid(detail::decay_t<T>))
throw bad_any_cast{};
return static_cast<any::impl<detail::decay_t<T>> const *>(x.ptr_.get())->get();
}
/// \overload
template<typename T>
meta::if_c<std::is_reference<T>() || Copyable<T>(), T>
any_cast(any &&x)
{
if(x.type() != typeid(detail::decay_t<T>))
throw bad_any_cast{};
return static_cast<any::impl<detail::decay_t<T>>*>(x.ptr_.get())->get();
}
/// \overload
template<typename T>
T * any_cast(any *p) noexcept
{
if(p && p->ptr_)
if(any::impl<T> *q = dynamic_cast<any::impl<T>*>(p->ptr_.get()))
return &q->get();
return nullptr;
}
/// \overload
template<typename T>
T const * any_cast(any const *p) noexcept
{
if(p && p->ptr_)
if(any::impl<T> const *q = dynamic_cast<any::impl<T> const *>(p->ptr_.get()))
return &q->get();
return nullptr;
}
}
}
#endif
|