Skip to main content
Code Review

Return to Question

edited tags
Link
200_success
  • 145.5k
  • 22
  • 190
  • 478
deleted 72 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

A C++11 `Any` ClassAny class

A C++11 Any Class

Overview

We show a C++11 class Any, thatThis is a polymophicpolymorphic wrapper capable of holding any type. (It is loosely based on boost::any)

In particular, this is useful when you want to store a heterogeneous collection. For example, such as vector<Any>.

A C++11 `Any` Class

A C++11 Any Class

Overview

We show a C++11 class Any, that is a polymophic wrapper capable of holding any type. (It is loosely based on boost::any)

In particular this is useful when you want to store a heterogeneous collection. For example vector<Any>.

C++11 Any class

This is a polymorphic wrapper capable of holding any type. (It is loosely based on boost::any)

In particular, this is useful when you want to store a heterogeneous collection, such as vector<Any>.

Post Unlocked by Adam
Notice removed Content dispute by Adam
Post Locked by Jamal
Notice added Content dispute by Jamal
Rollback to Revision 10
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
#include <type_traits>
#include <utility>
#include <typeinfo>
#include <string>
#include <cassert>
using namespace std;
struct Any
{
 template<class T>using DecayStorageType = typename decay<T>decay<typename remove_reference<T>::type>::type;
struct Any
{
 template<typename U>bool Anyis_null(U&& value) : ptr(new Derived<Decay<U>>(forward<U>(value)))const {}
  return Any()!ptr; {}
 Any(Any& that) :bool ptr(that.clonenot_null()) {}
  Any(Any&&const that){ :return ptr(move(that.ptr))ptr; {}
 Any(const Any& that)template<typename :U> ptr(that.cloneAny())U&& {}value)
 Any(const Any&& that) : ptr(that.clonenew Derived<StorageType<U>>(forward<U>(value)) {}
)
 bool is_null() const { return !bool(ptr); }
 bool not_null() const { return bool(ptr); }
 template<class U> bool is() const
 {
 typedef Decay<U>StorageType<U> T;
 auto derived = dynamic_cast<Derived<T>*> (ptr.get());
 return derived;
 }
 template<class U>
 Decay<U>&StorageType<U>& as()
 {
 typedef Decay<U>StorageType<U> T;
 auto derived = dynamic_cast<Derived<T>*> (ptr.get());
 if (!derived)
 throw bad_cast();
 return derived->value;
 }
 template<class U>
 const Decay<U>&operator asU() const
 {
 typedefreturn Decay<U>as<StorageType<U>>();
 T; }
 Any()
 auto derived = dynamic_cast<Derived<T>*>: (ptr.get()nullptr);
 {
 }
 ifAny(Any& that)
  : ptr(!derivedthat.clone())
 {
 }
 throwAny(Any&& bad_castthat)
 : ptr(that.ptr);
 {
 returnthat.ptr derived->value;= nullptr;
 }
 template<classAny(const U>Any& that)
 operator U : ptr(that.clone() const)
 {
 }
 returnAny(const as<Decay<U>>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;
 swap(ptr, a.ptr);
 return *this;
 }
private: ~Any()
 struct{
 Base; if (ptr)
 typedef unique_ptr<Base> PBase; delete ptr;
 }

private:
 struct Base
 {
 virtual ~Base() {}
 virtual PBaseBase* clone() const = 0;
 };
 template<typename T>
 struct Derived : Base
 {
 template<typename U> Derived(U&& value) : value(forward<U>(value)) { }
 T value;
 PBaseBase* clone() const { return PBase(new Derived<T>(value)); }
 };
 PBaseBase* clone() const
 {
 if (ptr)
 return ptr->clone();
 else
 return nullptr;
 }
 PBaseBase* ptr;
};
#include <type_traits>
#include <utility>
#include <typeinfo>
#include <string>
#include <cassert>
using namespace std;
struct Any
{
 template<class T>using Decay = typename decay<T>::type;
 template<typename U> Any(U&& value) : ptr(new Derived<Decay<U>>(forward<U>(value))) {}
  Any() {}
 Any(Any& that) : ptr(that.clone()) {}
  Any(Any&& that) : ptr(move(that.ptr)) {}
 Any(const Any& that) : ptr(that.clone()) {}
 Any(const Any&& that) : ptr(that.clone()) {}

 bool is_null() const { return !bool(ptr); }
 bool not_null() const { return bool(ptr); }
 template<class U> bool is() const
 {
 typedef Decay<U> T;
 auto derived = dynamic_cast<Derived<T>*> (ptr.get());
 return derived;
 }
 template<class U>
 Decay<U>& as()
 {
 typedef Decay<U> T;
 auto derived = dynamic_cast<Derived<T>*> (ptr.get());
 if (!derived)
 throw bad_cast();
 return derived->value;
 }
 template<class U>
 const Decay<U>& as() const
 {
 typedef Decay<U> T;
 auto derived = dynamic_cast<Derived<T>*> (ptr.get());
 if (!derived)
 throw bad_cast();
 return derived->value;
 }
 template<class U>
 operator U() const
 {
 return as<Decay<U>>();
 }
 Any& operator=(const Any& a)
 {
 if (ptr == a.ptr)
 return *this;
 ptr = a.clone();
 return *this;
 }
 Any& operator=(Any&& a)
 {
 if (ptr == a.ptr)
 return *this;
 swap(ptr, a.ptr);
 return *this;
 }
private:
 struct Base;
 typedef unique_ptr<Base> PBase;
 struct Base
 {
 virtual ~Base() {}
 virtual PBase clone() const = 0;
 };
 template<typename T>
 struct Derived : Base
 {
 template<typename U> Derived(U&& value) : value(forward<U>(value)) { }
 T value;
 PBase clone() const { return PBase(new Derived<T>(value)); }
 };
 PBase clone() const
 {
 if (ptr)
 return ptr->clone();
 else
 return nullptr;
 }
 PBase ptr;
};
#include <type_traits>
#include <utility>
#include <typeinfo>
#include <string>
#include <cassert>
using namespace std;
template<class T>using StorageType = typename decay<typename remove_reference<T>::type>::type;
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>>(forward<U>(value)))
 {
 }
 template<class U> bool is() const
 {
 typedef StorageType<U> T;
 auto derived = dynamic_cast<Derived<T>*> (ptr);
 return derived;
 }
 template<class U>
 StorageType<U>& as()
 {
 typedef StorageType<U> T;
 auto derived = dynamic_cast<Derived<T>*> (ptr);
 if (!derived)
 throw bad_cast();
 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;
 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(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;
};
Rollback to Revision 9
Source Link
Loading
Notice removed Content dispute by Community Bot
Post Unlocked by Community Bot
Post Locked by Jamal
Notice added Content dispute by Jamal
Rollback to Revision 8
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Loading
Rollback to Revision 7
Source Link
Loading
Rollback to Revision 6
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Loading
Rollback to Revision 5
Source Link
Loading
Rollback to Revision 2
Source Link
200_success
  • 145.5k
  • 22
  • 190
  • 478
Loading
deleted 17 characters in body
Source Link
Loading
Tweeted twitter.com/#!/StackCodeReview/status/285970488763768832
deleted 8 characters in body
Source Link
Loading
deleted 59 characters in body
Source Link
Loading
added 7 characters in body
Source Link
Loading
Source Link
Loading
lang-cpp

AltStyle によって変換されたページ (->オリジナル) /