Skip to content

Commit

Permalink
minor formatting fixes and more assertions added
Browse files Browse the repository at this point in the history
  • Loading branch information
apolukhin committed Apr 10, 2021
1 parent 69d2f14 commit 8db591c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
10 changes: 9 additions & 1 deletion include/boost/anys/basic_any.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace boost {

namespace anys {

template<std::size_t OptimizeForSize, std::size_t OptimizeForAlignment>
template <std::size_t OptimizeForSize, std::size_t OptimizeForAlignment>
class basic_any
{
BOOST_STATIC_ASSERT_MSG(OptimizeForSize > 0 && OptimizeForAlignment > 0, "Size and Align shall be positive values");
Expand All @@ -63,6 +63,7 @@ namespace anys {
switch (op)
{
case Destroy:
BOOST_ASSERT(!left.empty());
reinterpret_cast<ValueType*>(&left.content.small_value)->~ValueType();
break;
case Move: {
Expand Down Expand Up @@ -92,9 +93,12 @@ namespace anys {
left.man = right->man;
break;
case AnyCast:
BOOST_ASSERT(info);
BOOST_ASSERT(!left.empty());
return boost::typeindex::type_id<ValueType>() == *info ?
reinterpret_cast<typename remove_cv<ValueType>::type *>(&left.content.small_value) : 0;
case UnsafeCast:
BOOST_ASSERT(!left.empty());
return reinterpret_cast<typename remove_cv<ValueType>::type *>(&left.content.small_value);
case Typeinfo:
return const_cast<void*>(static_cast<const void*>(&boost::typeindex::type_id<ValueType>().type_info()));
Expand All @@ -109,6 +113,7 @@ namespace anys {
switch (op)
{
case Destroy:
BOOST_ASSERT(!left.empty());
delete static_cast<ValueType*>(left.content.large_value);
break;
case Move:
Expand All @@ -130,9 +135,12 @@ namespace anys {
left.man = right->man;
break;
case AnyCast:
BOOST_ASSERT(info);
BOOST_ASSERT(!left.empty());
return boost::typeindex::type_id<ValueType>() == *info ?
static_cast<typename remove_cv<ValueType>::type *>(left.content.large_value) : 0;
case UnsafeCast:
BOOST_ASSERT(!left.empty());
return reinterpret_cast<typename remove_cv<ValueType>::type *>(left.content.large_value);
case Typeinfo:
return const_cast<void*>(static_cast<const void*>(&boost::typeindex::type_id<ValueType>().type_info()));
Expand Down
19 changes: 10 additions & 9 deletions test/basic_test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,18 @@ struct huge_structure {


template <typename Any>
struct basic_tests { // test definitions
struct basic_tests // test definitions
{
struct copy_counter
{

public:

copy_counter() {}
copy_counter(const copy_counter&) { ++count; }
copy_counter& operator=(const copy_counter&) { ++count; return *this; }
static int get_count() { return count; }

private:

static int count;

};

static void test_default_ctor()
Expand Down Expand Up @@ -277,17 +274,20 @@ struct basic_tests { // test definitions
}

template<typename T>
class class_with_address_op {
class class_with_address_op
{
public:
class_with_address_op(const T* p)
: ptr(p)
{}

const T** operator &() {
const T** operator &()
{
return &ptr;
}

const T* get() const {
const T* get() const
{
return ptr;
}

Expand Down Expand Up @@ -327,7 +327,8 @@ struct basic_tests { // test definitions
check_true(!!boost::any_cast<huge_structure>(&test_val), "any_cast");
}

static int run_tests() {
static int run_tests()
{
typedef test<const char *, void (*)()> test_case;
const test_case test_cases[] =
{
Expand Down

0 comments on commit 8db591c

Please sign in to comment.