rewrite + add patch to all stl containers

This commit is contained in:
Zongyuan Zuo 2018-02-26 19:51:21 +08:00 committed by GitHub
parent 6b8e9824a6
commit 54297cd329
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 20 deletions

View File

@ -41,14 +41,14 @@ struct ANTLR4CPP_PUBLIC Any
} }
template<typename U> template<typename U>
Any(U&& value) : _ptr(new Derived<StorageType<U>, Cloneable<StorageType<U>>::value>(std::forward<U>(value))) { Any(U&& value) : _ptr(new Derived<StorageType<U>>(std::forward<U>(value))) {
} }
template<class U> template<class U>
bool is() const { bool is() const {
typedef StorageType<U> T; typedef StorageType<U> T;
auto derived = dynamic_cast<Derived<T, Cloneable<T>::value> *>(_ptr); auto derived = dynamic_cast<Derived<T> *>(_ptr);
return derived != nullptr; return derived != nullptr;
} }
@ -57,7 +57,7 @@ struct ANTLR4CPP_PUBLIC Any
StorageType<U>& as() { StorageType<U>& as() {
typedef StorageType<U> T; typedef StorageType<U> T;
auto derived = dynamic_cast<Derived<T, Cloneable<T>::value>*>(_ptr); auto derived = dynamic_cast<Derived<T>*>(_ptr);
if (!derived) if (!derived)
throw std::bad_cast(); throw std::bad_cast();
@ -99,21 +99,18 @@ struct ANTLR4CPP_PUBLIC Any
} }
private: private:
template<typename T> template<class T, class V = void>
struct Cloneable : std::is_copy_constructible<T> {}; struct Cloneable : std::is_copy_constructible<T> {};
template<typename T, typename A> template<class T, class A, template<class = T, class = A> class C>
struct Cloneable<std::vector<T, A>> : std::is_copy_constructible<T> {}; struct Cloneable<C<T, A>, typename std::enable_if<std::is_copy_constructible<C<T, A>>::value && !std::is_nothrow_copy_constructible<C<T, A>>::value>::type> : std::is_copy_constructible<T> {};
struct Base { struct Base {
virtual ~Base() {}; virtual ~Base() {};
virtual Base* clone() const = 0; virtual Base* clone() const = 0;
}; };
template<typename T, bool Cloneable>
struct Derived;
template<typename T> template<typename T>
struct Derived<T, true> : Base struct Derived<T> : Base
{ {
template<typename U> Derived(U&& value_) : value(std::forward<U>(value_)) { template<typename U> Derived(U&& value_) : value(std::forward<U>(value_)) {
} }
@ -121,20 +118,17 @@ private:
T value; T value;
Base* clone() const { Base* clone() const {
return new Derived<T, Cloneable<T>::value>(value); return clone<>();
} }
}; private:
template<int N = 0>
template<typename T> auto clone() const -> typename std::enable_if<N == N && Cloneable<T>::value, Base*>::type {
struct Derived<T, false> : Base return new Derived<T>(value);
{
template<typename U> Derived(U&& value_) : value(std::forward<U>(value_)) {
} }
T value; template<int N = 0>
auto clone() const -> typename std::enable_if<N == N && !Cloneable<T>::value, Base*>::type {
Base* clone() const {
return nullptr; return nullptr;
} }