From 2466c23751f131a07432df964658c2de90315e7b Mon Sep 17 00:00:00 2001 From: Gert van Valkenhoef Date: Wed, 9 Nov 2011 17:56:55 +0000 Subject: [PATCH] Reduce boilerplate for BlackBox Types and add some javadoc. --- fold.cc | 24 +++--------------- fold.h | 18 ++++++------- store.cc | 54 +++++++++------------------------------ store.h | 69 +++++++++++--------------------------------------- textmodel.cc | 24 +++--------------- textmodel.h | 18 ++++++------- typeregister.h | 64 +++++++++++++++++++++++++++++++++++++++++++--- 7 files changed, 111 insertions(+), 160 deletions(-) diff --git a/fold.cc b/fold.cc index 37e0fd6..b88d24b 100644 --- a/fold.cc +++ b/fold.cc @@ -4,20 +4,12 @@ namespace odc { const std::string View::TYPENAME("Views.View^"); -const TypeProxy View::PROXY; +const TypeProxy View::PROXY; View::View(INTEGER id) : Store(id) {} -const std::string &View::getType() { - return TYPENAME; -} - -const std::string *View::getSuper() { - return &Store::getType(); -} - const std::string &View::getTypeName() const { - return getType(); + return TYPENAME; } void View::internalize(Reader &reader) { @@ -27,20 +19,12 @@ void View::internalize(Reader &reader) { } const std::string Fold::TYPENAME("StdFolds.Fold^"); -const TypeProxy Fold::PROXY; +const TypeProxy Fold::PROXY; Fold::Fold(INTEGER id) : View(id) {} -const std::string &Fold::getType() { - return TYPENAME; -} - -const std::string *Fold::getSuper() { - return &View::getType(); -} - const std::string &Fold::getTypeName() const { - return getType(); + return TYPENAME; } void Fold::internalize(Reader &reader) { diff --git a/fold.h b/fold.h index 589b76b..79ee735 100644 --- a/fold.h +++ b/fold.h @@ -9,31 +9,29 @@ namespace odc { class View : public Store { private: - static const std::string TYPENAME; - static const TypeProxy PROXY; + static const TypeProxy PROXY; public: - View(INTEGER id); - static const std::string &getType(); - static const std::string *getSuper(); + static const std::string TYPENAME; virtual const std::string &getTypeName() const; + + View(INTEGER id); virtual void internalize(Reader &reader); }; class Fold : public View { private: - static const std::string TYPENAME; - static const TypeProxy PROXY; + static const TypeProxy PROXY; Store *d_hidden; SHORTCHAR *d_label; bool d_collapsed; public: - Fold(INTEGER id); - static const std::string &getType(); - static const std::string *getSuper(); + static const std::string TYPENAME; virtual const std::string &getTypeName() const; + + Fold(INTEGER id); virtual void internalize(Reader &reader); virtual std::string toString(); diff --git a/store.cc b/store.cc index 4bbabf8..6f44e85 100644 --- a/store.cc +++ b/store.cc @@ -26,7 +26,7 @@ std::string TypePath::toString() const { } const std::string Store::TYPENAME("Stores.Store^"); -const TypeProxy Store::PROXY; +const TopTypeProxy Store::PROXY; Store::Store(INTEGER id): d_id(id) {} @@ -34,20 +34,14 @@ INTEGER Store::getId() { return d_id; } -const std::string &Store::getType() { - return Store::TYPENAME; -} - -const std::string *Store::getSuper() { - return 0; -} - const std::string &Store::getTypeName() const { - return Store::getType(); + return Store::TYPENAME; } -void Store::getTypePath(TypePath *out) const { - return calcTypePath(out, getTypeName()); +TypePath Store::getTypePath() const { + TypePath out; + calcTypePath(&out, getTypeName()); + return out; } void Store::calcTypePath(TypePath *path, const std::string &name) const { @@ -69,20 +63,12 @@ std::string Store::toString() { void Store::accept(Visitor &visitor) const {} const std::string Elem::TYPENAME("Stores.Elem^"); -const TypeProxy Elem::PROXY; +const TypeProxy Elem::PROXY; Elem::Elem(INTEGER id) : Store(id) {} -const std::string &Elem::getType() { - return TYPENAME; -} - -const std::string *Elem::getSuper() { - return &Store::getType(); -} - const std::string &Elem::getTypeName() const { - return getType(); + return TYPENAME; } void Elem::internalize(Reader &reader) { @@ -92,20 +78,12 @@ void Elem::internalize(Reader &reader) { } const std::string Model::TYPENAME("Models.Model^"); -const TypeProxy Model::PROXY; +const TypeProxy Model::PROXY; Model::Model(INTEGER id) : Elem(id) {} -const std::string &Model::getType() { - return TYPENAME; -} - -const std::string *Model::getSuper() { - return &Elem::getType(); -} - const std::string &Model::getTypeName() const { - return getType(); + return TYPENAME; } void Model::internalize(Reader &reader) { @@ -115,20 +93,12 @@ void Model::internalize(Reader &reader) { } const std::string ContainerModel::TYPENAME("Containers.Model^"); -const TypeProxy ContainerModel::PROXY; +const TypeProxy ContainerModel::PROXY; ContainerModel::ContainerModel(INTEGER id) : Model(id) {} -const std::string &ContainerModel::getType() { - return TYPENAME; -} - -const std::string *ContainerModel::getSuper() { - return &Model::getType(); -} - const std::string &ContainerModel::getTypeName() const { - return getType(); + return TYPENAME; } void ContainerModel::internalize(Reader &reader) { diff --git a/store.h b/store.h index 4e7360e..a77febe 100644 --- a/store.h +++ b/store.h @@ -26,12 +26,12 @@ namespace odc { */ class Store { private: - static const std::string TYPENAME; - static const TypeProxy PROXY; + static const TopTypeProxy PROXY; INTEGER d_id; public: + static const std::string TYPENAME; static const SHORTCHAR NEWBASE = 0xF0; // (* new base type (level = 0), i.e. not yet in dict *) static const SHORTCHAR NEWEXT = 0xF1; // (* new extension type (level = 1), i.e. not yet in dict *) static const SHORTCHAR OLDTYPE = 0xF2; // (* old type, i.e. already in dict *) @@ -45,16 +45,6 @@ namespace odc { INTEGER getId(); - /** - * Get the TypeName of this object. - * @see TypeRegister - */ - static const std::string &getType(); - /** - * Get the TypeName of the supertype of this object. Return 0 pointer if no supertype. - * @see TypeRegister - */ - static const std::string *getSuper(); /** * Get the TypeName for this object. */ @@ -63,7 +53,7 @@ namespace odc { * Get the TypePath to this object's type. * @see TypePath */ - void getTypePath(TypePath *path) const; + TypePath getTypePath() const; /** * PROCEDURE (s: Store) Domain (): Domain @@ -97,7 +87,7 @@ namespace odc { * source.Domain() = NIL guaranteed * source is not yet initialized guaranteed */ - virtual void internalize(Reader &reader); + virtual void internalize(Reader &reader); // PROCEDURE (s: Store) Internalize- (VAR rd: Reader), NEW, EXTENSIBLE; // VAR thisVersion: INTEGER; // BEGIN @@ -138,71 +128,42 @@ namespace odc { virtual void accept(Visitor &visitor) const; private: - void calcTypePath(TypePath * out, const std::string &name) const; + void calcTypePath(TypePath *out, const std::string &name) const; }; class Elem : public Store { private: - static const std::string TYPENAME; - static const TypeProxy PROXY; + static const TypeProxy PROXY; public: - Elem(INTEGER id); - - /** - * Get the TypeName of this object. - * @see TypeRegister - */ - static const std::string &getType(); - /** - * Get the TypeName of the supertype of this object. Return 0 pointer if no supertype. - * @see TypeRegister - */ - static const std::string *getSuper(); - /** - * Get the TypeName for this object. - */ + static const std::string TYPENAME; virtual const std::string &getTypeName() const; + Elem(INTEGER id); virtual void internalize(Reader &reader); }; class Model : public Elem { private: - static const std::string TYPENAME; - static const TypeProxy PROXY; + static const TypeProxy PROXY; public: - Model(INTEGER id); - - /** - * Get the TypeName of this object. - * @see TypeRegister - */ - static const std::string &getType(); - /** - * Get the TypeName of the supertype of this object. Return 0 pointer if no supertype. - * @see TypeRegister - */ - static const std::string *getSuper(); - /** - * Get the TypeName for this object. - */ + static const std::string TYPENAME; virtual const std::string &getTypeName() const; + Model(INTEGER id); virtual void internalize(Reader &reader); }; class ContainerModel : public Model { private: - static const std::string TYPENAME; - static const TypeProxy PROXY; + static const TypeProxy PROXY; public: - ContainerModel(INTEGER id); - static const std::string &getType(); - static const std::string *getSuper(); + static const std::string TYPENAME; virtual const std::string &getTypeName() const; + + ContainerModel(INTEGER id); virtual void internalize(Reader &reader); }; } diff --git a/textmodel.cc b/textmodel.cc index 56bedba..c02620c 100644 --- a/textmodel.cc +++ b/textmodel.cc @@ -10,20 +10,12 @@ namespace odc { const std::string TextModel::TYPENAME("TextModels.Model^"); -const TypeProxy TextModel::PROXY; +const TypeProxy TextModel::PROXY; TextModel::TextModel(INTEGER id) : ContainerModel(id) {} -const std::string &TextModel::getType() { - return TYPENAME; -} - -const std::string *TextModel::getSuper() { - return &ContainerModel::getType(); -} - const std::string &TextModel::getTypeName() const { - return getType(); + return TYPENAME; } void TextModel::internalize(Reader &reader) { @@ -33,20 +25,12 @@ void TextModel::internalize(Reader &reader) { } const std::string StdTextModel::TYPENAME("TextModels.StdModel^"); -const TypeProxy StdTextModel::PROXY; +const TypeProxy StdTextModel::PROXY; StdTextModel::StdTextModel(INTEGER id) : TextModel(id), d_pieces() {} -const std::string &StdTextModel::getType() { - return TYPENAME; -} - -const std::string *StdTextModel::getSuper() { - return &TextModel::getType(); -} - const std::string &StdTextModel::getTypeName() const { - return getType(); + return TYPENAME; } /* diff --git a/textmodel.h b/textmodel.h index 07a8f64..abfe13d 100644 --- a/textmodel.h +++ b/textmodel.h @@ -9,14 +9,13 @@ namespace odc { class TextModel : public ContainerModel { private: - static const std::string TYPENAME; - static const TypeProxy PROXY; + static const TypeProxy PROXY; public: - TextModel(INTEGER id); - static const std::string &getType(); - static const std::string *getSuper(); + static const std::string TYPENAME; virtual const std::string &getTypeName() const; + + TextModel(INTEGER id); virtual void internalize(Reader &reader); }; @@ -64,15 +63,14 @@ namespace odc { class StdTextModel : public TextModel { private: - static const std::string TYPENAME; - static const TypeProxy PROXY; + static const TypeProxy PROXY; std::vector d_pieces; public: - StdTextModel(INTEGER id); - static const std::string &getType(); - static const std::string *getSuper(); + static const std::string TYPENAME; virtual const std::string &getTypeName() const; + + StdTextModel(INTEGER id); virtual void internalize(Reader &reader); virtual std::string toString(); diff --git a/typeregister.h b/typeregister.h index d647b5b..52a5428 100644 --- a/typeregister.h +++ b/typeregister.h @@ -9,6 +9,11 @@ namespace odc { class Store; class TypeProxyBase; // forward declaration + /** + * Register of Oberon/BlackBox types. + * Each type has a registered name, which refers to the full BlackBox type name (including any modules). + * It also (optionally) has a supertype (also referred to by the full BlackBox type name). + */ class TypeRegister { static TypeRegister *s_instance; @@ -19,28 +24,79 @@ namespace odc { TypeRegister &operator=(const TypeRegister &other); // NI public: + /** + * Get an instance of the type register (singleton pattern). + */ static TypeRegister &getInstance(); + /** + * Register a new type. + */ void add(const std::string &name, TypeProxyBase *proxy); + + /** + * Get a type's proxy. + */ const TypeProxyBase *get(const std::string &name); }; + /** + * Proxy to represent a BlackBox type. Has a name and optional supertype name. + * Can instantiate new instances of the type. + */ class TypeProxyBase { public: + /** + * Create a new TypeProxy and register it with the TypeRegister. + */ TypeProxyBase(const std::string &name); + /** + * @return The full BlackBox type name (including modules). + */ virtual const std::string &getName() const = 0; + /** + * @return The full BlackBox type name of the supertype (if applicable, otherwise a 0-pointer). + */ virtual const std::string *getSuper() const = 0; + /** + * @return A new instance of the type, with the given ID. + */ virtual Store *newInstance(INTEGER id) const = 0; }; - template class TypeProxy : public TypeProxyBase { + /** + * Proxy for a toplevel type (no supertype). + * T: the class to proxy for. + * Requires T to have a static const std::string TYPENAME. + */ + template class TopTypeProxy : public TypeProxyBase { + public: + TopTypeProxy(): TypeProxyBase(T::TYPENAME) {} + virtual const std::string &getName() const { + return T::TYPENAME; + } + virtual const std::string *getSuper() const { + return 0; + } + virtual Store *newInstance(INTEGER id) const { + return new T(id); + } + }; + + /** + * Proxy for a derived type (with supertype). + * T: the class to proxy for. + * S: the supertype. + * Requires T, S to have a static const std::string TYPENAME. + */ + template class TypeProxy : public TypeProxyBase { public: - TypeProxy(): TypeProxyBase(T::getType()) {} + TypeProxy(): TypeProxyBase(T::TYPENAME) {} virtual const std::string &getName() const { - return T::getType(); + return T::TYPENAME; } virtual const std::string *getSuper() const { - return T::getSuper(); + return &S::TYPENAME; } virtual Store *newInstance(INTEGER id) const { return new T(id); -- 2.29.2