STIR  6.2.0
Static Public Member Functions | Protected Types | Static Protected Member Functions | List of all members
stir::RegisteredObject< Root > Class Template Reference

Helper class to provide registry mechanisms to a Base classSuppose you have a hierarchy of classes with (nearly) all public functionality provided by virtual functions of the Base (here called Root) class. The aim is then to be able to select at run-time which of the nodes will be used. More...

#include "stir/RegisteredObject.h"

Inheritance diagram for stir::RegisteredObject< Root >:
Inheritance graph
[legend]

Static Public Member Functions

static Root * read_registered_object (std::istream *in, const std::string &registered_name)
 Construct a new object (of a type derived from Root, its actual type determined by the registered_name parameter) by parsing the istream. More...
 
static Root * ask_type_and_parameters ()
 ask the user for the type, and then calls read_registered_object(0, type) More...
 
static void list_registered_names (std::ostream &stream)
 List all possible registered names to the stream. More...
 

Protected Types

typedef Root *(* RootFactory) (std::istream *)
 The type of a root factory is a function, taking an istream* as argument, and returning a Root*.
 
typedef FactoryRegistry< std::string, RootFactory, interfile_lessRegistryType
 The type of the registry.
 

Static Protected Member Functions

static RegistryTyperegistry ()
 Static function returning the registry. More...
 

Additional Inherited Members

- Public Member Functions inherited from stir::RegisteredObjectBase
virtual std::string get_registered_name () const =0
 Returns the name of the type of the object. More...
 
- Public Member Functions inherited from stir::ParsingObject
 ParsingObject (const ParsingObject &)
 
ParsingObjectoperator= (const ParsingObject &)
 
void ask_parameters ()
 
virtual std::string parameter_info ()
 
bool parse (std::istream &f)
 
bool parse (const char *const filename)
 
- Protected Member Functions inherited from stir::ParsingObject
virtual void set_defaults ()
 Set defaults before parsing.
 
virtual void initialise_keymap ()
 Initialise all keywords.
 
virtual bool post_processing ()
 This will be called at the end of the parsing. More...
 
virtual void set_key_values ()
 This will be called before parsing or parameter_info is called. More...
 
- Protected Attributes inherited from stir::ParsingObject
KeyParser parser
 

Detailed Description

template<typename Root>
class stir::RegisteredObject< Root >

Helper class to provide registry mechanisms to a Base class

Suppose you have a hierarchy of classes with (nearly) all public functionality provided by virtual functions of the Base (here called Root) class. The aim is then to be able to select at run-time which of the nodes will be used.

To do this, one needs to enter all node classes in a registry. This registry contains a key and a "Root factory" for every node-class. The factory for the node-class returns (a pointer to) a new node-class object, which of course is also a Root object.

In STIR, FactoryRegistry provides the type for the registry.

In many cases, the factory constructs the new object from a stream. The current class provides the basic mechanisms for this, i.e. a registry, and a function that looks up the relevant factory in the registry and uses it to construct the object from a stream. In addition, there is an interactive function for asking the type and its parameters. This makes only sense if the object construction can be interactive as well (see ask_type_and_parameters()).

We currently assume that the construction of the object is done by using ParsingObject. Nearly all of the necessary functionality can be provided to the hierarchy by using RegisteredParsingObject in the hierarchy. The hierarchy looks normally as follows:

ParsingObject
RegisteredObjectBase
RegisteredObject<Root>
Root
...
Parent
RegisteredParsingObject<Derived,Root,Parent>
Derived

When there is no intermediate class in hierarchy, this is simplified to:

ParsingObject
RegisteredObjectBase
RegisteredObject<Root>
Root
RegisteredParsingObject<Derived,Root,Root>
Derived
See also
RegisteredParsingObject
Todo:
Currently there is a hard-wired value of "None" for the default key (with a 0 factory). This is inappropriate in some cases.
Limitation:

In the previous (including STIR 4.x) version of this hierarchy, ParsingObject wasn't at the root of everything. However, the current hierarchy is simpler to use, and you can still override relevant members such that ParsingObject is effectively not used.

Member Function Documentation

◆ read_registered_object()

template<class Root >
Root * stir::RegisteredObject< Root >::read_registered_object ( std::istream *  in,
const std::string &  registered_name 
)
inlinestatic

Construct a new object (of a type derived from Root, its actual type determined by the registered_name parameter) by parsing the istream.

This works by finding the 'root factory' object in a registry that corresponds to registered_name, and calling the factory on this istream*.

◆ ask_type_and_parameters()

template<class Root >
Root * stir::RegisteredObject< Root >::ask_type_and_parameters ( )
inlinestatic

ask the user for the type, and then calls read_registered_object(0, type)

Warning
Relies on read_registered_object to be interactive when its first argument is 0.

Sadly, this function cannot be called ask_parameters() because of conflicts with ParsingObject::ask_parameters() in the derived classes.

◆ list_registered_names()

template<class Root >
void stir::RegisteredObject< Root >::list_registered_names ( std::ostream &  stream)
inlinestatic

List all possible registered names to the stream.

Names are separated with newlines.

◆ registry()

template<class Root >
RegisteredObject< Root >::RegistryType & stir::RegisteredObject< Root >::registry ( )
inlinestaticprotected

Static function returning the registry.

Warning
This function is non inline when using Visual C++ 6.0 because of a compiler limitation. This means that when using this compiler, RegisteredObject will need explicit instantiations for all derived classes.

The documentation for this class was generated from the following files: