/usr/include/wibble/amorph.h is in libwibble-dev 1.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 | /** -*- C++ -*-
@file wibble/amorph.h
@author Peter Rockai <me@mornfall.net>
*/
#include <iostream> // for noise
#include <wibble/mixin.h>
#include <wibble/cast.h>
#include <wibble/maybe.h>
#include <wibble/sfinae.h>
#include <wibble/test.h>
#ifndef WIBBLE_AMORPH_H
#define WIBBLE_AMORPH_H
namespace wibble {
template< typename _T >
struct ReturnType {
typedef _T T;
};
template<>
struct ReturnType< void > {
typedef Unit T;
};
template< typename F, typename R >
struct SanitizeReturn {
inline typename ReturnType< R >::T call(
typename F::argument_type a )
{
return f( a );
};
};
template< typename F >
struct SanitizeReturn< F, void > {
inline Unit call( typename F::argument_type a )
{
f( a );
return Unit();
}
};
template< int A >
struct IsZero {
static const bool value = false;
};
template<>
struct IsZero< 0 > {
static const bool value = true;
};
template< typename T >
struct IsPolymorphic {
struct A : T {
virtual ~A();
};
struct B : T {};
static const bool value = IsZero< sizeof( A ) - sizeof( B ) >::value;
};
template< typename F >
struct SanitizeResultType {
SanitizeResultType( F _f ) : f( _f ) {}
typedef typename ReturnType< typename F::result_type >::T result_type;
result_type operator()( typename F::argument_type a ) {
return SanitizeReturn< F, typename F::result_type >().call( a );
}
F f;
};
#ifndef SWIG_I
struct Baseless {};
struct VirtualBase {
virtual ~VirtualBase() {}
};
/**
@brief An interface implemented by all morph classes
*/
template< typename Interface >
struct MorphInterface : public Interface {
virtual VirtualBase *virtualBase() { return 0; }
virtual MorphInterface *constructCopy( void *where = 0, unsigned int available = 0 ) const = 0;
virtual void destroy( unsigned int available = 0 ) = 0;
virtual ~MorphInterface() {}
virtual bool leq( const MorphInterface * ) const = 0;
};
/** @brief custom allocator for morph classes */
struct MorphAllocator {
void *operator new( size_t bytes, void *where, unsigned available ) {
if ( bytes > available || where == 0 ) {
where = ::operator new( bytes );
return where;
}
return where;
}
void *operator new( size_t bytes ) {
return ::operator new( bytes );
}
};
template< typename W, typename Interface >
struct MorphBase : MorphInterface< Interface > {
MorphBase( const W &w ) : m_wrapped( w ) {}
template< typename _W >
typename EnableIf< IsPolymorphic< _W >, VirtualBase *>::T virtualBase() {
return dynamic_cast< VirtualBase * >( &m_wrapped );
}
template< typename _W >
typename EnableIf< TNot< IsPolymorphic< _W > >, VirtualBase *>::T
virtualBase() {
return 0;
}
virtual VirtualBase *virtualBase() {
return virtualBase< W >();
}
W &wrapped() {
return m_wrapped;
}
protected:
W m_wrapped;
};
template< typename Self, typename W, typename Interface >
struct Morph : MorphBase< W, Interface >,
mixin::Comparable< Morph< Self, W, Interface > >,
MorphAllocator
{
typedef W Wrapped;
Morph( const Wrapped &w ) : MorphBase< W, Interface >( w ) {}
const Self &self() const { return *static_cast< const Self * >( this ); }
bool operator<=( const Morph &o ) const {
return wrapped().operator<=( o.wrapped() );
}
// evaluation of correctness of definition should be done
virtual bool leq( const MorphInterface< Interface > *_o ) const {
const Morph *o = dynamic_cast< const Morph * >( _o );
if ( !o ) {
if ( typeid( Morph ).before( typeid( _o ) ) )
return true;
else
return false;
}
return wrapped() <= o->wrapped();
}
virtual MorphInterface< Interface > *constructCopy(
void *where, unsigned int available ) const
{
return new( where, available ) Self( self() );
}
virtual void destroy( unsigned int available ) {
if ( sizeof( Morph ) <= available ) {
this->~Morph();
} else {
delete this;
}
}
const Wrapped &wrapped() const {
return this->m_wrapped;
}
Wrapped &wrapped() {
return this->m_wrapped;
}
virtual ~Morph() {}
};
#endif
/**
@brief Amorph base class
This class is an Amorph class. An Amorph can hold one of many
Morhps of the corresponding kind. That means, Amorph is an envelope
that can contain an instance of a Morph. The Amorph envelope can
provide methods that all the Morphs that it can hold provide as
well. These methods will then act polymoprhically in the Amorph
instance, depending on what Morph is inside.
You use the Amorph and Morph classes as values, that is, they have
value semantics. You usually do not need (nor want) to use pointers
to access them. Of course it may be useful sometimes, but is not
the normal mode of operation.
Amorph objects are equal if they hold same type of Morph and the
Morphs themselves are also equal. Different types of Morphs are
never equal.
When implementing your own Amorph class, you will want it to
contain the methods that are shared by all it's Morphs. These will
usually look like
methodFoo() { return this->impl()->methodFoo(); }
If you need to dispatch on the type of the Morph inside the Amorph
envelope, you can use
if ( amorph.is< MyMorph >() ) {
MyMorph morph = amorph;
}
or if you write adaptable unary function objects (see stl manual)
handling the specific morph types, you can write:
amorph.ifType( functor );
This will call functor on the morph type if the functor's
argument_type matches the type of contained Morph. The returned
type is Maybe< functor::result_type >. If the type matches, the
returned value is Just (returned value), otherwise Nothing.
See wibble::Maybe documentation for details.
This also lends itself to template specialisation approach, where
you have a template that handles all Morphs but you need to
specialize it for certain Morphs. Eventually, an example of this
usage will appear in amorph.cpp over time.
Often, using Amorph types will save you a template parameter you
cannot afford. It also supports value-based programming, which
means you need to worry about pointers a lot less.
For a complex example of Amorph class set implementation, see
range.h.
Implementation details: the current Amorph template takes an
integral Padding argument. The MorphImpl class contains an
overloaded operator new, that allows it to be constructed
off-heap. The Padding argument is used as a number of words to
reserve inside the Amorph object itself. If the Morph that will be
enveloped in the Amorph fits in this space, it will be allocated
there, otherwise on heap. The Padding size defaults to 0 and
therefore all Morphs are by default heap-allocated. Reserving a
reasonable amount of padding should improve performance a fair bit
in some applications (and is worthless in others).
*/
#ifndef WIBBLE_AMORPH_PADDING
#define WIBBLE_AMORPH_PADDING 0
#endif
template<int Padding1> class AmorphPadder
{
int m_padding[ Padding1 ];
};
template<> class AmorphPadder<0>
{
};
template< typename Self, typename _Interface, int Padding = WIBBLE_AMORPH_PADDING >
struct Amorph {
typedef _Interface Interface;
// typedef MorphInterface< Interface > Morp;
template <typename T> struct Convert {
typedef T type;
};
/* Amorph( const Interface &b ) {
setInterfacePointer( &b );
} */
Amorph( const MorphInterface< Interface > &b ) {
setMorphInterfacePointer( &b );
}
Amorph( const Amorph &a ) {
setMorphInterfacePointer( a.morphInterface() );
// setInterfacePointer( a.implementation() );
}
Amorph() : m_impl( 0 ) {}
const Self &self() const {
return *static_cast< const Self * >( this );
}
Self &self() {
return *static_cast<Self *>( this );
}
bool leq( const Self &i ) const {
if ( morphInterface() )
if ( i.morphInterface() )
return morphInterface()->leq( i.morphInterface() );
else
return false; // it's false that non-0 <= 0
else
return !i.morphInterface(); // 0 <= 0 holds, but 0 <=
// non-0 doesn't
}
bool operator<=( const Self &i ) const { return leq( i ); }
void setInterfacePointer( const Interface *i ) {
if ( !i ) {
m_impl = 0;
return;
}
/* assert( dynamic_cast< const MorphInterface * >( i ) );
assert( dynamic_cast< const Interface * >(
dynamic_cast< const MorphInterface * >( i ) ) ); */
m_impl = dynamic_cast< const MorphInterface< Interface > * >( i )->constructCopy(
&m_padding, sizeof( m_padding ) );
// assert( dynamic_cast< const Interface * >( m_impl ) );
}
void setMorphInterfacePointer( const MorphInterface< Interface > *i ) {
if ( !i ) {
m_impl = 0;
return;
}
m_impl = i->constructCopy( &m_padding, sizeof( m_padding ) );
}
Amorph &operator=( const Amorph &i ) {
setInterfacePointer( i.implementation() );
return *this;
}
~Amorph() {
if ( morphInterface() )
morphInterface()->destroy( sizeof( m_padding ) );
}
template< typename F >
Maybe< typename F::result_type > ifType( F func ) {
typedef typename F::argument_type T;
typedef Maybe< typename F::result_type > rt;
T *ptr = impl<T>();
if (ptr) {
return rt::Just( func(*ptr) );
}
return rt::Nothing();
}
const Interface *implementation() const {
// return dynamic_cast< const Interface * >( m_impl );
return static_cast< const Interface * >( m_impl );
}
Interface *implementation() {
// return dynamic_cast< Interface * >( m_impl );
return static_cast< Interface * >( m_impl );
}
MorphInterface< Interface > *morphInterface() const {
return m_impl;
// return dynamic_cast< MorphInterface< * >( m_impl );
}
const Interface &wrapped() const {
return *implementation();
}
Interface &wrapped() {
return *implementation();
}
template< typename T >
bool is() const {
return impl< T >();
}
bool isVoid() const { return !m_impl; }
template< typename T >
T *impl() const {
T *p = dynamic_cast< T * >( m_impl );
if ( !p ) {
MorphBase< T, Interface > *m = dynamic_cast< MorphBase< T, Interface > * >( m_impl );
if ( m ) p = &(m->wrapped());
}
if ( !p ) {
p = dynamic_cast< T * >( morphInterface()->virtualBase() );
}
return p;
}
private:
unsigned int reservedSize() { return sizeof( m_padding ) + sizeof( m_impl ); }
AmorphPadder<Padding> m_padding;
MorphInterface< Interface > *m_impl;
// Interface *m_impl;
};
#ifndef SWIG_I
template< typename T, typename X >
typename X::template Convert<T>::type &downcast( const X &a )
{
return *a.template impl< T >();
}
#endif
}
#endif
|