Skip to content

Commit

Permalink
initial fixes and tests for the unique_any (refs #24)
Browse files Browse the repository at this point in the history
  • Loading branch information
apolukhin committed May 6, 2023
1 parent 89d4565 commit 55160ab
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 61 deletions.
112 changes: 51 additions & 61 deletions include/boost/any/unique_any.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,16 @@
#include <memory>

#ifdef BOOST_NO_CXX11_HDR_INITIALIZER_LIST
#include <initializer_list>
#error Header <boost/any/unique_any.hpp> requires C++11 compatible standard library with std::initializer_list
#endif

#include <utility>
#include <type_traits>

#include <boost/any/bad_any_cast.hpp>

#include <boost/core/addressof.hpp>
#include <boost/type_traits/decay.hpp>
#include <boost/core/enable_if.hpp>
#include <boost/type_index.hpp>

namespace boost { namespace anys {

Expand All @@ -52,37 +53,34 @@ constexpr in_place_type_t<T> in_place_type{};

class unique_any {
public:
constexpr unique_any() noexcept = default;

BOOST_CONSTEXPR unique_any() BOOST_NOEXCEPT = default;

unique_any(unique_any&& other) BOOST_NOEXCEPT = default;
unique_any(unique_any&& other) noexcept = default;

// Perfect forwarding of T
template<typename T>
unique_any(T&& value
, typename boost::disable_if<boost::is_same<unique_any&, T> >::type* = 0 // disable if value has type `unique_any&`
, typename boost::disable_if<boost::is_const<T> >::type* = 0) // disable if value has type `const T&&`
: content(new holder< typename boost::decay<T>::type >(std::forward<T>(value)))
, typename boost::disable_if<std::is_same<unique_any&, T> >::type* = 0 // disable if value has type `unique_any&`
, typename boost::disable_if<std::is_const<T> >::type* = 0) // disable if value has type `const T&&`
: content(new holder< typename std::decay<T>::type >(std::forward<T>(value)))
{
}

template<class T, class... Args>
explicit unique_any(in_place_type_t<T>, Args&&... args)
: content(new holder<typename boost::decay<T>::type>(std::forward<Args>(args)...))
: content(new holder<typename std::decay<T>::type>(std::forward<Args>(args)...))
{
}

#ifdef BOOST_NO_CXX11_HDR_INITIALIZER_LIST
template <class T, class U, class... Args>
explicit unique_any(in_place_type_t<T>, std::initializer_list<U> il, Args&&... args)
: content(new holder<typename boost::decay<T>::type>(il, std::forward<Args>(args)...))
: content(new holder<typename std::decay<T>::type>(il, std::forward<Args>(args)...))
{
}
#endif

~unique_any() BOOST_NOEXCEPT = default;
~unique_any() noexcept = default;

unique_any & operator=(unique_any&& rhs) BOOST_NOEXCEPT = default;
unique_any & operator=(unique_any&& rhs) noexcept = default;

template <class T>
unique_any & operator=(T&& rhs)
Expand All @@ -92,134 +90,128 @@ class unique_any {
}

template<class T, class... Args>
typename boost::decay<T>::type& emplace(Args&&... args) {
content = std::unique_ptr<placeholder>(
new holder<typename boost::decay<T>::type>(std::forward<Args>(args)...)
);
typename std::decay<T>::type& emplace(Args&&... args) {
auto* raw_ptr = new holder<typename std::decay<T>::type>(std::forward<Args>(args)...);
content = std::unique_ptr<placeholder>(raw_ptr);
return raw_ptr->held;
}

#ifdef BOOST_NO_CXX11_HDR_INITIALIZER_LIST
template<class T, class U, class... Args>
typename boost::decay<T>::type& emplace(initializer_list<U> il, Args&&... args) {
content = std::unique_ptr<placeholder>(
new holder<typename boost::decay<T>::type>(il, std::forward<Args>(args)...)
);
typename std::decay<T>::type& emplace(std::initializer_list<U> il, Args&&... args) {
auto* raw_ptr = holder<typename std::decay<T>::type>(il, std::forward<Args>(args)...);
content = std::unique_ptr<placeholder>(raw_ptr);
return raw_ptr->held;
}
#endif

void reset() BOOST_NOEXCEPT
void reset() noexcept
{
content.reset();
}

unique_any& swap(unique_any& rhs) BOOST_NOEXCEPT
void swap(unique_any& rhs) noexcept
{
content.swap(rhs.content);
return *this;
}


bool has_value() const BOOST_NOEXCEPT
bool has_value() const noexcept
{
return !content;
return !!content;
}

const boost::typeindex::type_info& type() const BOOST_NOEXCEPT
const boost::typeindex::type_info& type() const noexcept
{
return content ? content->type() : boost::typeindex::type_id<void>().type_info();
}

private: // types
class BOOST_SYMBOL_VISIBLE placeholder
{
public:
virtual ~placeholder()
{
}

virtual const boost::typeindex::type_info& type() const BOOST_NOEXCEPT = 0;
virtual const boost::typeindex::type_info& type() const noexcept = 0;

};

template<typename T>
class holder BOOST_FINAL: public placeholder
class holder final: public placeholder
{
public: // structors

public:
template <class... Args>
holder(Args&&... args)
: held(std::forward<Args>(args)...)
{
}

#ifdef BOOST_NO_CXX11_HDR_INITIALIZER_LIST
template <class U, class... Args>
holder(std::initializer_list<U> il, Args&&... args)
: held(il, std::forward<Args>(args)...)
{
}
#endif

const boost::typeindex::type_info& type() const BOOST_NOEXCEPT BOOST_OVERRIDE
const boost::typeindex::type_info& type() const noexcept override
{
return boost::typeindex::type_id<T>().type_info();
}

public: // representation

public:
T held;
};


private: // representation

template<typename T>
friend T * any_cast(unique_any *) BOOST_NOEXCEPT;
friend T * any_cast(unique_any *) noexcept;

template<typename T>
friend T * unsafe_any_cast(unique_any *) BOOST_NOEXCEPT;
friend T * unsafe_any_cast(unique_any *) noexcept;

std::unique_ptr<placeholder> content;
};

inline void swap(unique_any & lhs, unique_any & rhs) BOOST_NOEXCEPT
inline void swap(unique_any & lhs, unique_any & rhs) noexcept
{
lhs.swap(rhs);
}

template<typename T>
T * any_cast(unique_any * operand) BOOST_NOEXCEPT
T * any_cast(unique_any * operand) noexcept
{
return operand && operand->type() == boost::typeindex::type_id<T>()
? boost::addressof(
static_cast<unique_any::holder<BOOST_DEDUCED_TYPENAME remove_cv<T>::type>&>(*operand->content).held
? std::addressof(
static_cast<unique_any::holder<typename remove_cv<T>::type>&>(*operand->content).held
)
: 0;
: nullptr;
}

template<typename T>
inline const T * any_cast(const unique_any * operand) BOOST_NOEXCEPT
inline const T * any_cast(const unique_any * operand) noexcept
{
return any_cast<T>(const_cast<unique_any *>(operand));
}

template<typename T>
T any_cast(unique_any & operand)
{
typedef BOOST_DEDUCED_TYPENAME remove_reference<T>::type nonref;
typedef typename remove_reference<T>::type nonref;


nonref * result = any_cast<nonref>(boost::addressof(operand));
nonref * result = any_cast<nonref>(std::addressof(operand));
if(!result)
boost::throw_exception(bad_any_cast());

// Attempt to avoid construction of a temporary object in cases when
// `T` is not a reference. Example:
// `static_cast<std::string>(*result);`
// which is equal to `std::string(*result);`
typedef BOOST_DEDUCED_TYPENAME boost::conditional<
boost::is_reference<T>::value,
typedef typename std::conditional<
std::is_reference<T>::value,
T,
BOOST_DEDUCED_TYPENAME boost::add_reference<T>::type
T&
>::type ref_type;

#ifdef BOOST_MSVC
Expand All @@ -235,22 +227,20 @@ T any_cast(unique_any & operand)
template<typename T>
inline T any_cast(const unique_any & operand)
{
typedef BOOST_DEDUCED_TYPENAME remove_reference<T>::type nonref;
typedef typename remove_reference<T>::type nonref;
return any_cast<const nonref &>(const_cast<unique_any &>(operand));
}

#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
template<typename T>
inline T any_cast(unique_any&& operand)
{
BOOST_STATIC_ASSERT_MSG(
boost::is_rvalue_reference<T&&>::value /*true if T is rvalue or just a value*/
|| boost::is_const< typename boost::remove_reference<T>::type >::value,
std::is_rvalue_reference<T&&>::value /*true if T is rvalue or just a value*/
|| std::is_const< typename std::remove_reference<T>::type >::value,
"boost::any_cast shall not be used for getting nonconst references to temporary objects"
);
return any_cast<T>(operand);
}
#endif


// Note: The "unsafe" versions of any_cast are not part of the
Expand All @@ -259,15 +249,15 @@ inline T any_cast(unique_any&& operand)
// use typeid() comparison, e.g., when our types may travel across
// different shared libraries.
template<typename T>
inline T * unsafe_any_cast(unique_any * operand) BOOST_NOEXCEPT
inline T * unsafe_any_cast(unique_any * operand) noexcept
{
return boost::addressof(
return std::addressof(
static_cast<unique_any::holder<T>&>(*operand->content)->held
);
}

template<typename T>
inline const T * unsafe_any_cast(const unique_any * operand) BOOST_NOEXCEPT
inline const T * unsafe_any_cast(const unique_any * operand) noexcept
{
return unsafe_any_cast<T>(const_cast<unique_any *>(operand));
}
Expand Down
2 changes: 2 additions & 0 deletions test/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
# For more information, see http://www.boost.org/libs/any
#

build-project unique_any ;

import testing ;

test-suite any :
Expand Down
22 changes: 22 additions & 0 deletions test/unique_any/Jamfile.v2
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright Vladimir Prus 2005. 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)
#
# For more information, see http://www.boost.org/libs/any
#
import ../../config/checks/config : requires ;

import testing ;

project
: source-location .
: requirements
[ requires cxx11 ]
;

test-suite unique_any :
[ run base.cpp ]
;


0 comments on commit 55160ab

Please sign in to comment.