namespace original
{
// Klasse IntStack wie in Listing 1
class DoubleStack
// gleicher Aufbau wie IntStack
class PositiveIntStack: public IntStack
{
typedef IntStack Base;
public:
void push(const int& i)
{
if (i >= 0)
Base::push(i);
}
}; class IncrementingPositiveIntStack: public PositiveIntStack
{
typedef PositiveIntStack Base;
public:
void push(const int& i)
{
if (i >= 0)
Base::push(i+1);
}
};
}
// namespace aspects wie in Listing 4
namespace composed
{
// a simple domain specific language (DSL)
// for specifying aspects (implemented as
// a bit mask)
enum AspectDSL
{
none = 0,
tracing = 1,
checking= 2
};
// a simple generator for
// composing aspects and classes
template <int aspect,class Base,class Class>
struct Compose
{
// parse DSL specification
enum
{
subclass = aspects::Inherits<Class,Base>::RET,
addChecking = aspect & checking && subclass,
addTracing = aspect & tracing && subclass
};
// add aspects if applicable
typedef typename
aspects::IF <addChecking,aspects::Checking<Class>,Class>::RET Phase1;
typedef typename
aspects::IF <addTracing,aspects::Tracing<Phase1>,Phase1>::RET Phase2;
typedef Phase2 RET;
};
typedef Compose<tracing + checking,original::IntStack,original::IntStack>::RET IntStack;
typedef Compose<checking + tracing,original::IntStack,original::PositiveIntStack>::RET PositiveIntStack;
typedef Compose<tracing + checking,original::IntStack,original::IncrementingPositiveIntStack>::RET IncrementingPositiveIntStack;
typedef Compose<tracing + checking,original::IntStack,original::DoubleStack>::RET DoubleStack;
}
Dieser Text ist der Zeitschriften-Ausgabe 09/2001 von iX entnommen.
iOS, Android, Windows Phone 7 und HTML5 - das neue Sonderheft von heise Developer führt Einsteiger und Profis in die Programmierung mobiler Geräte ein.