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;
58 template<
typename T >
67 template<
typename F >
90 template<
typename Interface >
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 >
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 ); }
157 const Morph *o =
dynamic_cast< const Morph *
>( _o );
159 if (
typeid(
Morph ).before(
typeid( _o ) ) )
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 ) {
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 >
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 {
305 if ( i.morphInterface() )
310 return !i.morphInterface();
327 &m_padding,
sizeof( m_padding ) );
337 m_impl = i->
constructCopy( &m_padding,
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 );
384 template<
typename T >
391 template<
typename T >
393 T *p =
dynamic_cast< T *
>( m_impl );
405 unsigned int reservedSize() {
return sizeof( m_padding ) +
sizeof( m_impl ); }
406 AmorphPadder<Padding> m_padding;
407 MorphInterface< Interface > *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 >();
X::template Convert< T >::type & downcast(const X &a)
Definition: amorph.h:413
T type
Definition: amorph.h:277
const Interface * implementation() const
Definition: amorph.h:361
Amorph(const Amorph &a)
Definition: amorph.h:288
Interface & wrapped()
Definition: amorph.h:380
MorphInterface< Interface > * morphInterface() const
Definition: amorph.h:371
bool leq(const Self &i) const
Definition: amorph.h:303
Interface * implementation()
Definition: amorph.h:366
Maybe< typename F::result_type > ifType(F func)
Definition: amorph.h:351
Amorph & operator=(const Amorph &i)
Definition: amorph.h:340
T * impl() const
Definition: amorph.h:392
~Amorph()
Definition: amorph.h:345
Amorph()
Definition: amorph.h:293
void setInterfacePointer(const Interface *i)
Definition: amorph.h:316
bool isVoid() const
Definition: amorph.h:389
bool operator<=(const Self &i) const
Definition: amorph.h:314
_Interface Interface
Definition: amorph.h:273
void setMorphInterfacePointer(const MorphInterface< Interface > *i)
Definition: amorph.h:332
Amorph(const MorphInterface< Interface > &b)
Definition: amorph.h:284
const Interface & wrapped() const
Definition: amorph.h:376
bool is() const
Definition: amorph.h:385
static const bool value
Definition: amorph.h:64
static const bool value
Definition: amorph.h:50
custom allocator for morph classes
Definition: amorph.h:100
virtual VirtualBase * virtualBase()
Definition: amorph.h:128
W m_wrapped
Definition: amorph.h:137
EnableIf< TNot< IsPolymorphic< _W > >, VirtualBase * >::T virtualBase()
Definition: amorph.h:124
MorphBase(const W &w)
Definition: amorph.h:115
EnableIf< IsPolymorphic< _W >, VirtualBase * >::T virtualBase()
Definition: amorph.h:118
W & wrapped()
Definition: amorph.h:132
An interface implemented by all morph classes.
Definition: amorph.h:91
virtual VirtualBase * virtualBase()
Definition: amorph.h:92
virtual ~MorphInterface()
Definition: amorph.h:95
virtual bool leq(const MorphInterface *) const =0
virtual MorphInterface * constructCopy(void *where=0, unsigned int available=0) const =0
virtual void destroy(unsigned int available=0)=0
W Wrapped
Definition: amorph.h:145
Wrapped & wrapped()
Definition: amorph.h:185
bool operator<=(const Morph &o) const
Definition: amorph.h:151
virtual ~Morph()
Definition: amorph.h:189
Morph(const Wrapped &w)
Definition: amorph.h:147
virtual MorphInterface< Interface > * constructCopy(void *where, unsigned int available) const
Definition: amorph.h:167
virtual bool leq(const MorphInterface< Interface > *_o) const
Definition: amorph.h:156
virtual void destroy(unsigned int available)
Definition: amorph.h:173
const Wrapped & wrapped() const
Definition: amorph.h:181
Unit T
Definition: amorph.h:26
_T T
Definition: amorph.h:21
result_type operator()(typename F::argument_type a)
Definition: amorph.h:72
SanitizeResultType(F _f)
Definition: amorph.h:69
F f
Definition: amorph.h:77
ReturnType< typename F::result_type >::T result_type
Definition: amorph.h:70
Unit call(typename F::argument_type a)
Definition: amorph.h:41
ReturnType< R >::T call(typename F::argument_type a)
Definition: amorph.h:31
virtual ~VirtualBase()
Definition: amorph.h:84