Definition in file polymorph.h.
#include <cstring>
#include <string>
#include <typeinfo>
#include <cassert>
#include "../src/stringutils.h"
#include "../src/stringparser.h"
#include "../src/allocatingpointer.h"
#include "../src/tristate.h"
#include "../src/typeless.h"
Include dependency graph for polymorph.h:
This graph shows which files directly or indirectly include this file:
Go to the source code of this file.
Compounds | |
class | Polymorph |
Generic implementation of a polymorphic object; no support for numericvalue(). More... | |
class | Polymorph< string > |
Specialization for string types. More... | |
class | PolymorphBase |
Wrapper to hide the type of an enclosed variable. More... | |
Defines | |
#define | NumericPolymorph(type) |
Specialization for numeric types. | |
Functions | |
template<class T> Polymorph< T > | make_Polymorph (T *ptr) |
Returns a polymorph of the appropriate type for the given pointer argument. | |
template<class T> Polymorph< T > | allocating_Polymorph (const T &val) |
Returns a polymorph of the appropriate type with an internal copy of the given non-pointer argument. | |
NumericPolymorph (int) | |
NumericPolymorph (double) | |
NumericPolymorph (Tristate) |
|
Value: template <> class Polymorph<type> : public PolymorphBase<type> {\ public:\ typedef type value_type;\ typedef Polymorph<value_type> self;\ virtual ~Polymorph() { }\ Polymorph(value_type val) : PolymorphBase<value_type>(val) { };\ Polymorph(value_type* ptr) : PolymorphBase<value_type>(ptr) { };\ virtual Typeless* clone() const { return new self(*this); };\ virtual Typeless* duplicate() const { return new self(*p); };\ virtual bool isnumeric() const { return true; }\ virtual Typeless::exprtype numericvalue() const\ { return Typeless::exprtype(*p); }\ virtual string prettyprint(String::StreamFormat format=String::StreamFormat()) const\ { return (*p==Uninitialized ? std::string("Uninitialized") : stringrep(format)); }\ } This implementation adds numericvalue() support to the given type. It would be cleaner to have numericvalue() as a template function, but then it couldn't be virtual, and also we need to specify that only numeric types can use this specialization. Thus here we explicitly specialize Polymorph for the typical numeric types. If you use other numeric types, just add the appropriate "NumericPolymorph(type);" line before you use them (e.g. just after including this file) and numericvalue() should magically start to work for them. Note that the prettyprint implementation assumes that you will want all values which happen to be equal to Tristate's Uninitialized to print as "Uninitialized" for all numeric types, which is currently useful but may not always be so. Definition at line 223 of file polymorph.h. |
|
Returns a polymorph of the appropriate type for the given pointer argument. This can be used to avoid having to specify the specific template argument to determine the Polymorph type. Definition at line 193 of file polymorph.h. |