WIP reworked to replace boost::variant by a custom solution

Not a big simplification, but at least the actual codepath is shorter,
while it's not so general as boost::variant (and not threadsafe!)
This commit is contained in:
Fischlurch 2008-05-17 04:34:46 +02:00
parent 85b0029166
commit 88fc2f6099
3 changed files with 588 additions and 26 deletions

View file

@ -136,7 +136,7 @@ namespace lumiera
>
{
public:
typedef InstantiateChained<TYPES,_X_> Next;
typedef InstantiateChained<TYPES,_X_,BASE> Next;
typedef _X_<TY,Next> Unit;
};

View file

@ -9,35 +9,235 @@
// 1/08 - check 64bit longs
// 4/08 - comparison operators on shared_ptr<Asset>
// 4/08 - conversions on the value_type used for boost::any
// 5/08 - how to guard a downcasting access, so it is compiled in only if the involved types are convertible
#include <nobug.h>
#include <iostream>
#include <typeinfo>
#include <boost/any.hpp>
#include <boost/type_traits/is_convertible.hpp>
#include <boost/type_traits/is_polymorphic.hpp>
#include <boost/type_traits/is_base_of.hpp>
#include <boost/type_traits/remove_pointer.hpp>
#include <boost/type_traits/remove_reference.hpp>
#include <boost/utility/enable_if.hpp>
using std::string;
using std::cout;
using std::ostream;
using boost::remove_pointer;
using boost::remove_reference;
using boost::is_convertible;
using boost::is_polymorphic;
using boost::is_base_of;
using boost::enable_if;
template <typename SRC, typename TAR>
struct can_cast : boost::false_type {};
template <typename SRC, typename TAR>
struct can_cast<SRC*,TAR*> { enum { value = is_base_of<SRC,TAR>::value };};
template <typename SRC, typename TAR>
struct can_cast<SRC*&,TAR*> { enum { value = is_base_of<SRC,TAR>::value };};
template <typename SRC, typename TAR>
struct can_cast<SRC&,TAR&> { enum { value = is_base_of<SRC,TAR>::value };};
template <typename T>
struct has_RTTI
{
typedef typename remove_pointer<
typename remove_reference<T>::type>::type TPlain;
enum { value = is_polymorphic<TPlain>::value };
};
template <typename SRC, typename TAR>
struct use_dynamic_downcast
{
enum { value = can_cast<SRC,TAR>::value
&& has_RTTI<SRC>::value
&& has_RTTI<TAR>::value
};
};
template <typename SRC, typename TAR>
struct use_static_downcast
{
enum { value = can_cast<SRC,TAR>::value
&& ( !has_RTTI<SRC>::value
|| !has_RTTI<TAR>::value
)
};
};
template <typename SRC, typename TAR>
struct use_conversion
{
enum { value = is_convertible<SRC,TAR>::value
&& !( use_static_downcast<SRC,TAR>::value
||use_dynamic_downcast<SRC,TAR>::value
)
};
};
template<typename X>
struct EmptyVal
{
static X create()
{
cout << " NULL() " << __PRETTY_FUNCTION__ <<"\n";
return X();
}
};
template<typename X>
struct EmptyVal<X*&>
{
static X*& create()
{
cout << " NULL & " << __PRETTY_FUNCTION__ <<"\n";
static X* null(0);
return null;
}
};
template<typename RET>
struct NullAccessor
{
typedef RET Ret;
static RET access (...) { return ifEmpty(); }
static RET ifEmpty () { return EmptyVal<RET>::create(); }
};
template<typename TAR>
struct AccessCasted : NullAccessor<TAR>
{
using NullAccessor<TAR>::access;
template<typename ELM>
static typename enable_if< use_dynamic_downcast<ELM&,TAR>, TAR>::type
access (ELM& elem)
{
cout << " dynamic " << __PRETTY_FUNCTION__ <<"\n";
return dynamic_cast<TAR> (elem);
}
template<typename ELM>
static typename enable_if< use_static_downcast<ELM&,TAR>, TAR>::type
access (ELM& elem)
{
cout << " static " << __PRETTY_FUNCTION__ <<"\n";
return static_cast<TAR> (elem);
}
template<typename ELM>
static typename enable_if< use_conversion<ELM&,TAR>, TAR>::type
access (ELM& elem)
{
cout << " convert " << __PRETTY_FUNCTION__ <<"\n";
return elem;
}
};
using boost::any;
using boost::any_cast;
struct B {};
struct D : B {};
int main (int argc, char* argv[])
struct E : D
{
virtual ~E() {};
};
struct F : E {};
ostream& operator<< (ostream& s, const B& b) { return s << "B{} adr="<<&b; }
ostream& operator<< (ostream& s, const D& d) { return s << "D{} adr="<<&d; }
ostream& operator<< (ostream& s, const E& e) { return s << "E{} adr="<<&e; }
ostream& operator<< (ostream& s, const F& f) { return s << "F{} adr="<<&f; }
int
main (int argc, char* argv[])
{
NOBUG_INIT;
D d;
D* pD =&d;
B* pB =pD;
D& rD = *pD;
B& rB = *pB;
D*& rpD = pD;
B*& rpB = pB;
F f;
E& rE = f;
E* pE = &f;
D* pDE = pE;
any aD (pD);
any_cast<B*> (aD);
cout << "is_base_of<B,D> = " << is_base_of<B ,D >::value << "\n";
cout << "is_base_of<B*,D*> = " << is_base_of<B*,D*>::value << "\n";
cout << "is_base_of<B&,D&> = " << is_base_of<B&,D&>::value << "\n";
cout << "can_cast<B,D> = " << can_cast<B,D>::value << "\n";
cout << "can_cast<B*,D*> = " << can_cast<B*,D*>::value << "\n";
cout << "can_cast<B&,D&> = " << can_cast<B&,D&>::value << "\n";
cout << "can_cast<B&,D*> = " << can_cast<B&,D*>::value << "\n";
cout << "can_cast<B*,D&> = " << can_cast<B*,D&>::value << "\n";
cout << "can_cast<B*&,D*&> = " << can_cast<B*&,D*&>::value << "\n";
cout << "can_cast<D*&,D*&> = " << can_cast<D*&,D*&>::value << "\n";
cout << "can_cast<D*,E*> = " << can_cast<D*,E*>::value << "\n";
cout << "can_cast<E*,F*> = " << can_cast<E*,F*>::value << "\n";
cout << "has_RTTI<D*> = " << has_RTTI<D*>::value << "\n";
cout << "has_RTTI<E*> = " << has_RTTI<E*>::value << "\n";
cout << "has_RTTI<F*> = " << has_RTTI<F*>::value << "\n";
cout << "use_dynamic_downcast<B*,D*> = " << use_dynamic_downcast<B*,D*>::value << "\n";
cout << "use_static_downcast<B*,D*> = " << use_static_downcast<B*,D*>::value << "\n";
cout << "use_conversion<D*,B*> = " << use_conversion<D*,B*>::value << "\n";
cout << "use_static_downcast<D*&,D*&> = " << use_static_downcast<D*&,D*&>::value << "\n";
cout << "use_static_downcast<D*,E*> = " << use_static_downcast<D*,E*>::value << "\n";
cout << "use_dynamic_downcast<D*&,E*> = " << use_dynamic_downcast<D*&,E*>::value << "\n";
cout << "Access(D as D&) --->" << AccessCasted<D&>::access(d) << "\n";
cout << "Access(D& as D&) --->" << AccessCasted<D&>::access(rD) << "\n";
cout << "Access(B& as D&) --->" << AccessCasted<D&>::access(rB) << "\n";
cout << "Access(D* as D*) --->" << AccessCasted<D*>::access(pD) << "\n";
cout << "Access(B* as D*) --->" << AccessCasted<D*>::access(pB) << "\n";
cout << "Access(D*& as D*&) --->" << AccessCasted<D*&>::access(rpD) << "\n";
cout << "Access(B*& as D*&) --->" << AccessCasted<D*&>::access(rpB) << "\n";
cout << "Access(D as B&) --->" << AccessCasted<B&>::access(d) << "\n";
cout << "Access(D& as B&) --->" << AccessCasted<B&>::access(rD) << "\n";
cout << "Access(B& as B&) --->" << AccessCasted<D&>::access(rB) << "\n";
cout << "Access(D* as B*) --->" << AccessCasted<B*>::access(pD) << "\n";
cout << "Access(B* as B*) --->" << AccessCasted<B*>::access(pB) << "\n";
cout << "Access(D*& as B*&) --->" << AccessCasted<B*&>::access(rpD) << "\n";
cout << "Access(B*& as B*&) --->" << AccessCasted<B*&>::access(rpB) << "\n";
cout << "Access(D as E&) --->" << AccessCasted<E&>::access(d) << "\n";
cout << "Access(E& as F&) --->" << AccessCasted<F&>::access(rE) << "\n";
cout << "Access(D(E)* as E*) --->" << AccessCasted<E*>::access(pDE) << "\n";
cout << "Access(D(E)* as F*) --->" << AccessCasted<F*>::access(pDE) << "\n";
cout << "Access(E* as F*) --->" << AccessCasted<F*>::access(pE) << "\n";
cout << "\ngulp\n";

View file

@ -32,7 +32,15 @@
#include "proc/mobject/placement.hpp"
#include "proc/mobject/explicitplacement.hpp" //////////TODO
#include <boost/variant.hpp>
#include "common/typelistutil.hpp"
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/remove_pointer.hpp>
#include <boost/type_traits/is_convertible.hpp>
#include <boost/type_traits/is_polymorphic.hpp>
#include <boost/type_traits/is_base_of.hpp>
//#include <boost/variant.hpp>
#include <iostream>
using std::string;
using std::cout;
@ -65,8 +73,359 @@ namespace mobject
- vieleicht einen allgemeinen Argument-Adapter nutzen?
- Zielobjekt oder Wrapper<Zielobjekt> als Argumenttyp?
*/
struct Nothing {};
using boost::enable_if;
using boost::is_convertible;
using lumiera::typelist::Types;
using lumiera::typelist::Node;
using lumiera::typelist::NullType;
// using lumiera::typelist::count;
// using lumiera::typelist::maxSize;
using lumiera::typelist::InstantiateChained;
template
< class TYPES // List of Types
, template<class,class,uint> class _X_ // your-template-goes-here
, class BASE = NullType // Base class at end of chain
, uint i = 0 // incremented on each instantiaton
>
class InstantiateWithIndex;
template< template<class,class,uint> class _X_
, class BASE
, uint i
>
class InstantiateWithIndex<NullType, _X_, BASE, i>
: public BASE
{
public:
typedef BASE Unit;
typedef NullType Next;
enum{ COUNT = i };
};
template
< class TY, typename TYPES
, template<class,class,uint> class _X_
, class BASE
, uint i
>
class InstantiateWithIndex<Node<TY, TYPES>, _X_, BASE, i>
: public _X_< TY
, InstantiateWithIndex<TYPES, _X_, BASE, i+1 >
, i
>
{
public:
typedef InstantiateWithIndex<TYPES,_X_,BASE,i+1> Next;
typedef _X_<TY,Next,i> Unit;
enum{ COUNT = Next::COUNT };
};
template<class TYPES>
struct count;
template<>
struct count<NullType>
{
enum{ value = 0 };
};
template<class TY, class TYPES>
struct count<Node<TY,TYPES> >
{
enum{ value = 1 + count<TYPES>::value };
};
template<class TYPES>
struct maxSize;
template<>
struct maxSize<NullType>
{
enum{ value = 0 };
};
template<class TY, class TYPES>
struct maxSize<Node<TY,TYPES> >
{
enum{ nextval = maxSize<TYPES>::value
, thisval = sizeof(TY)
, value = nextval > thisval? nextval:thisval
};
};
template<class T>
struct Holder
{
T content;
Holder (T const& src) : T(src) {}
template<typename TAR>
TAR get () { return static_cast<TAR> (content); }
};
typedef Types < Placement<MObject>*
, P<asset::Asset>*
> ::List
WrapperTypes;
template<typename X>
struct EmptyVal
{
static X create()
{
return X();
}
};
template<typename X>
struct EmptyVal<X*&>
{
static X*& create()
{
static X* null(0);
return null;
}
};
const uint TYPECNT = count<WrapperTypes>::value;
const size_t SIZE = maxSize<WrapperTypes>::value;
struct Buffer
{
char buffer_[SIZE];
uint which;
Buffer() : which(TYPECNT) {}
void*
put (void)
{
deleteCurrent();
return 0;
}
void
deleteCurrent (); // depends on the Deleter, see below
};
template<typename T, class BASE, uint idx>
struct Storage : BASE
{
T&
put (T const& toStore)
{
BASE::deleteCurrent(); // remove old content, if any
T& storedObj = *new(BASE::buffer_) T (toStore);
this->which = idx; // remember the actual type selected
return storedObj;
}
using BASE::put;
};
template<class FUNCTOR>
struct CaseSelect
{
typedef typename FUNCTOR::Ret Ret;
typedef Ret (*Func)(Buffer&);
Func table_[TYPECNT];
CaseSelect ()
{
for (uint i=0; i<TYPECNT; ++i)
table_[i] = 0;
}
template<typename T>
static Ret
trampoline (Buffer& storage)
{
T& content = reinterpret_cast<T&> (storage.buffer_);
return FUNCTOR::access (content);
}
template<typename T>
void
create_thunk (uint idx)
{
Func thunk = &trampoline<T>;
table_[idx] = thunk;
}
Ret
invoke (Buffer& storage)
{
if (TYPECNT <= storage.which)
return FUNCTOR::ifEmpty ();
else
return (*table_[storage.which]) (storage);
}
};
template< class T, class BASE, uint i >
struct CasePrepare
: BASE
{
CasePrepare () : BASE()
{
// BASE::table_[i] = &(BASE::template trampoline<T>);
BASE::template create_thunk<T>(i);
}
};
template<class FUNCTOR>
typename FUNCTOR::Ret
access (Buffer& buf)
{
typedef InstantiateWithIndex< WrapperTypes
, CasePrepare
, CaseSelect<FUNCTOR>
>
Accessor;
static Accessor select_case;
return select_case.invoke(buf);
}
struct Deleter
{
typedef void Ret;
template<typename T>
static void access (T& elem) { elem.~T(); }
static void ifEmpty () { }
};
void
Buffer::deleteCurrent ()
{
access<Deleter>(*this); // remove old content, if any
which = TYPECNT; // mark as empty
}
using boost::remove_pointer;
using boost::remove_reference;
using boost::is_convertible;
using boost::is_polymorphic;
using boost::is_base_of;
using boost::enable_if;
template <typename SRC, typename TAR>
struct can_cast : boost::false_type {};
template <typename SRC, typename TAR>
struct can_cast<SRC*,TAR*> { enum { value = is_base_of<SRC,TAR>::value };};
template <typename SRC, typename TAR>
struct can_cast<SRC*&,TAR*> { enum { value = is_base_of<SRC,TAR>::value };};
template <typename SRC, typename TAR>
struct can_cast<SRC&,TAR&> { enum { value = is_base_of<SRC,TAR>::value };};
template <typename T>
struct has_RTTI
{
typedef typename remove_pointer<
typename remove_reference<T>::type>::type TPlain;
enum { value = is_polymorphic<TPlain>::value };
};
template <typename SRC, typename TAR>
struct use_dynamic_downcast
{
enum { value = can_cast<SRC,TAR>::value
&& has_RTTI<SRC>::value
&& has_RTTI<TAR>::value
};
};
template <typename SRC, typename TAR>
struct use_static_downcast
{
enum { value = can_cast<SRC,TAR>::value
&& ( !has_RTTI<SRC>::value
|| !has_RTTI<TAR>::value
)
};
};
template <typename SRC, typename TAR>
struct use_conversion
{
enum { value = is_convertible<SRC,TAR>::value
&& !( use_static_downcast<SRC,TAR>::value
||use_dynamic_downcast<SRC,TAR>::value
)
};
};
template<typename RET>
struct NullAccessor
{
typedef RET Ret;
static RET access (...) { return ifEmpty(); }
static RET ifEmpty () { return EmptyVal<RET>::create(); }
};
template<typename TAR>
struct AccessCasted : NullAccessor<TAR>
{
using NullAccessor<TAR>::access;
template<typename ELM>
static typename enable_if< use_dynamic_downcast<ELM&,TAR>, TAR>::type
access (ELM& elem)
{
return dynamic_cast<TAR> (elem);
}
template<typename ELM>
static typename enable_if< use_static_downcast<ELM&,TAR>, TAR>::type
access (ELM& elem)
{
return static_cast<TAR> (elem);
}
template<typename ELM>
static typename enable_if< use_conversion<ELM&,TAR>, TAR>::type
access (ELM& elem)
{
return elem;
}
};
/**
* helper to treat various sorts of smart-ptrs uniformly.
* Implemented as a variant-type value object, it is preconfigured
@ -80,32 +439,33 @@ namespace mobject
class WrapperPtr // NONCOPYABLE!!
{
template<typename X>
struct accessor;
template<template<class> class WRA, class TAR>
struct accessor< WRA<TAR> >
: public boost::static_visitor<WRA<TAR>*>
{
template<typename X> WRA<TAR>* operator() (X&) { return 0; }
template<typename BASE> WRA<TAR>* operator() (WRA<BASE>& stored) { return static_cast<WRA<TAR>*> (stored); }
};
private:
typedef InstantiateWithIndex< WrapperTypes
, Storage
, Buffer
>
VariantHolder;
/** storage: buffer holding either and "empty" marker,
* or one of the configured pointer to wrapper types */
boost::variant<Nothing, Placement<MObject>*, P<asset::Asset>*> holder_;
VariantHolder holder_;
public:
void reset () { holder_ = Nothing(); }
void
reset ()
{
access<Deleter> (holder_);
holder_.which = TYPECNT;
}
template<typename WRA>
WrapperPtr&
operator= (WRA* src) ///< store a ptr to the given wrapper, after casting to base
{
if (src) holder_ = src;
else holder_ = Nothing();
if (src) holder_.put (src);
else reset();
return *this;
}
@ -113,8 +473,8 @@ namespace mobject
WRA*
get () ///< retrieve ptr and try to downcast to type WRA
{
accessor<WRA> acc;
WRA* res = boost::apply_visitor(acc, this->holder_);
typedef AccessCasted<WRA*> AccessWraP;
WRA* res = access<AccessWraP>(this->holder_);
return res;
}
};
@ -244,7 +604,8 @@ namespace mobject
void treat (Clip& c)
{
Placement<Clip>& pC = getPlacement<Clip>();
cout << "media is: "<< str(pC->getMedia()) <<"\n";
cout << "media is: "<< str(pC->getMedia()) <<"\n"
<< "Placement(adr) " << &pC <<"\n";
}
void treat (AbstractMO&)
{
@ -291,6 +652,7 @@ namespace mobject
Placement<Clip> clip = asset::Media::create("test-1", asset::VIDEO)->createClip();
cout << "Placement(adr) " << &clip <<"\n";
apply (tool, clip);
cout << "Placement(adr) " << &dumm <<"\n";
apply (tool, dummy);