Topic: Variants |& Inheritance
Author: marc@offline.be (Marc Duponcheel)
Date: 18 May 93 00:51:24 PST Raw View
In article <1993May17.163414.7144@uunet.uu.net!rcm> rmartin@uunet.uu.net!rcm (Robert Martin) writes:
> maxtal@physics.su.OZ.AU (John Max Skaller) writes:
>
> >In article <C6tswt.68t@world.std.com> tob@world.std.com (Tom O Breton) writes:
FIRST: I think both inheritance and variants are ok.
=========================================================================
But let's first look at the expression (unary -e ? binary e+f ?) problem
mentioned several times ...
=========================================================================
My friend Jan Joos who is a very much underestimated (mostly by himself)
C++ guru implements Expressions and Values as follows.
(It IS a simplification because both Logical and Algebraic Expressions are
designed in the real thing)
class ExprImpl // abstract implementation (the 'variant')
{
public:
// ...
virtual Val Eval() const = 0;
// ...
};
class Expr
{
// ...binary operators
friend Expr operator + (const Expr& l, const Expr& r);
// ...
protected:
SmartPtr<ExprImpl> sp; // deletes itself when no references left
Expr(const SmartPtr<ExprImpl>& sp);
public:
// ...unary operators
Expr operator - () const;
// ...
Val Eval() const
{
return sp->Eval();
}
};
// ...
class Add : public ExprImpl
{
SmartPtr<ExprImpl> spl;
SmartPtr<ExprImpl> spr;
public:
Val Eval() const;
};
// ...
class Neg : public ExprImpl
{
SmartPtr<ExprImpl> sp;
public:
Val Eval() const;
};
// ...
// ...
Expr operator + (const Expr& l, const Expr& r)
{
return Expr(new Add(l.sp, r.sp));
}
// ...
Expr Expr::operator - () const
{
return Expr(new Neg(sp));
}
// ...
=========================================================================
!!! SO !!! all is done through derivation !!!
=========================================================================
The technique is used at work in a big project to write a custom
interpreter using yacc and lex.
Jan wrote the basics in 1 weekend !
=========================================================================
Let's now look at the Val class ...
=========================================================================
Here, Jan needed an enum.
In fact the values travel from memory to file to database so Jan designed
something that could be:
(These are the common values found in database implementations.)
empty
integer
float
date
time
datetime
string
money
With all operators implemented (errors included if they dont make sense)
These are the common values found in database implementations.
The code is one giant piece of cases in cases :-(
=========================================================================
!!! SO !!! here he could have wanted variants !!!
=========================================================================
-- marc.
##########################################################################
preferred address = marc@offline.be
e-mail marc@offline.be marc@offline.UUCP offline!marc ub4b!offline!marc
fido 2:292/603.26 Marc.Duponcheel@p26.f603.n292.z2.FidoNet.Org
bix mduponcheel mduponcheel@BIX.com
##########################################################################