14 #ifndef WIBBLE_AMORPH_H 15 #define WIBBLE_AMORPH_H 19 template<
typename _T >
29 template<
typename F,
typename R >
32 typename F::argument_type a )
39 template<
typename F >
41 inline Unit call(
typename F::argument_type a )
50 static const bool value =
false;
55 static const bool value =
true;
58 template<
typename T >
64 static const bool value =
IsZero<
sizeof(
A ) -
sizeof(
B ) >::value;
67 template<
typename F >
90 template<
typename Interface >
93 virtual MorphInterface *constructCopy(
void *where = 0,
unsigned int available = 0 )
const = 0;
94 virtual void destroy(
unsigned int available = 0 ) = 0;
101 void *
operator new(
size_t bytes,
void *where,
unsigned available ) {
102 if ( bytes > available || where == 0 ) {
103 where = ::operator
new( bytes );
108 void *
operator new(
size_t bytes ) {
109 return ::operator
new( bytes );
113 template<
typename W,
typename Interface >
117 template<
typename _W >
119 return dynamic_cast< VirtualBase *
>( &m_wrapped );
122 template<
typename _W >
129 return virtualBase< W >();
140 template<
typename Self,
typename W,
typename Interface >
149 const Self &
self()
const {
return *
static_cast< const Self *
>( this ); }
152 return wrapped().operator<=( o.
wrapped() );
157 const Morph *o =
dynamic_cast< const Morph *
>( _o );
159 if (
typeid(
Morph ).before(
typeid( _o ) ) )
164 return wrapped() <= o->
wrapped();
168 void *where,
unsigned int available )
const 170 return new( where, available ) Self(
self() );
173 virtual void destroy(
unsigned int available ) {
174 if (
sizeof(
Morph ) <= available ) {
182 return this->m_wrapped;
186 return this->m_wrapped;
260 #ifndef WIBBLE_AMORPH_PADDING 261 #define WIBBLE_AMORPH_PADDING 0 265 int m_padding[ Padding1 ];
271 template<
typename Self,
typename _Interface,
int Padding = WIBBLE_AMORPH_PADDING >
285 setMorphInterfacePointer( &b );
295 const Self &
self()
const {
296 return *
static_cast< const Self *
>( this );
300 return *
static_cast<Self *
>( this );
303 bool leq(
const Self &i )
const {
304 if ( morphInterface() )
305 if ( i.morphInterface() )
306 return morphInterface()->leq( i.morphInterface() );
310 return !i.morphInterface();
327 &m_padding,
sizeof( m_padding ) );
337 m_impl = i->
constructCopy( &m_padding,
sizeof( m_padding ) );
346 if ( morphInterface() )
347 morphInterface()->destroy(
sizeof( m_padding ) );
350 template<
typename F >
352 typedef typename F::argument_type
T;
356 return rt::Just( func(*ptr) );
358 return rt::Nothing();
363 return static_cast< const Interface *
>( m_impl );
368 return static_cast< Interface *
>( m_impl );
377 return *implementation();
381 return *implementation();
384 template<
typename T >
391 template<
typename T >
393 T *p =
dynamic_cast< T *
>( m_impl );
399 p =
dynamic_cast< T *
>( morphInterface()->virtualBase() );
405 unsigned int reservedSize() {
return sizeof( m_padding ) +
sizeof( m_impl ); }
412 template<
typename T,
typename X >
413 typename X::template Convert<T>::type &
downcast(
const X &a )
415 return *a.template impl< T >();
virtual VirtualBase * virtualBase()
Definition: amorph.h:92
W & wrapped()
Definition: amorph.h:132
Unit T
Definition: amorph.h:26
virtual MorphInterface * constructCopy(void *where=0, unsigned int available=0) const =0
Amorph & operator=(const Amorph &i)
Definition: amorph.h:340
SanitizeResultType(F _f)
Definition: amorph.h:69
T type
Definition: amorph.h:277
MorphBase(const W &w)
Definition: amorph.h:115
X::template Convert< T >::type & downcast(const X &a)
Definition: amorph.h:413
const Interface * implementation() const
Definition: amorph.h:361
_Interface Interface
Definition: amorph.h:273
Amorph()
Definition: amorph.h:293
void setInterfacePointer(const Interface *i)
Definition: amorph.h:316
Morph(const Wrapped &w)
Definition: amorph.h:147
bool is() const
Definition: amorph.h:385
custom allocator for morph classes
Definition: amorph.h:100
~Amorph()
Definition: amorph.h:345
ReturnType< typename F::result_type >::T result_type
Definition: amorph.h:70
virtual VirtualBase * virtualBase()
Definition: amorph.h:128
virtual MorphInterface< Interface > * constructCopy(void *where, unsigned int available) const
Definition: amorph.h:167
An interface implemented by all morph classes.
Definition: amorph.h:91
bool isVoid() const
Definition: amorph.h:389
_T T
Definition: amorph.h:21
bool operator<=(const Morph &o) const
Definition: amorph.h:151
EnableIf< TNot< IsPolymorphic< _W > >, VirtualBase * >::T virtualBase()
Definition: amorph.h:124
virtual void destroy(unsigned int available)
Definition: amorph.h:173
EnableIf< IsPolymorphic< _W >, VirtualBase * >::T virtualBase()
Definition: amorph.h:118
Amorph(const Amorph &a)
Definition: amorph.h:288
Wrapped & wrapped()
Definition: amorph.h:185
result_type operator()(typename F::argument_type a)
Definition: amorph.h:72
Unit call(typename F::argument_type a)
Definition: amorph.h:41
virtual ~MorphInterface()
Definition: amorph.h:95
ReturnType< R >::T call(typename F::argument_type a)
Definition: amorph.h:31
virtual ~Morph()
Definition: amorph.h:189
Interface & wrapped()
Definition: amorph.h:380
W m_wrapped
Definition: amorph.h:137
Amorph(const MorphInterface< Interface > &b)
Definition: amorph.h:284
F f
Definition: amorph.h:77
const Interface & wrapped() const
Definition: amorph.h:376
Interface * implementation()
Definition: amorph.h:366
MorphInterface< Interface > * morphInterface() const
Definition: amorph.h:371
const Wrapped & wrapped() const
Definition: amorph.h:181
virtual ~VirtualBase()
Definition: amorph.h:84
W Wrapped
Definition: amorph.h:145
Maybe< typename F::result_type > ifType(F func)
Definition: amorph.h:351
virtual bool leq(const MorphInterface< Interface > *_o) const
Definition: amorph.h:156
T * impl() const
Definition: amorph.h:392
bool leq(const Self &i) const
Definition: amorph.h:303
void setMorphInterfacePointer(const MorphInterface< Interface > *i)
Definition: amorph.h:332
bool operator<=(const Self &i) const
Definition: amorph.h:314