Greens-code
A modular quantum transport code
Classes | Public Types | Public Member Functions | Static Public Member Functions | List of all members
loadable_Object Class Reference

An abstract base class supporting xml_file_reading of input parameters. More...

#include <input_file.hpp>

Inheritance diagram for loadable_Object:
Inheritance graph
[legend]

Public Types

typedef STD_TR1::shared_ptr< loadable_Objectref
 
typedef STD_TR1::shared_ptr< const loadable_Objectconst_ref
 
typedef std::pair< std::string, ref > ref_pair
 
typedef STD_TR1::weak_ptr< loadable_Objectlink
 
typedef STD_TR1::weak_ptr< const loadable_Objectconst_link
 

Public Member Functions

void write_xml (std::ostream &) const
 Output the xml code that would construct the object to stream.
 
 loadable_Object ()
 Empty construction, objects are initialized by init routine.
 
const char * class_Name () const
 Calls virtual const char* name(), which must be redefined for derived classes.
 
bool get_param (const std::string &s, double &v) const
 get a double, integer or string parameter by name (s), return wether parameter exists
 
bool get_param (const std::string &s, int &v) const
 
bool get_param (const std::string &s, std::string &v) const
 
double get_double_param (const std::string &s) const
 get a double, integer or string parameter, throw if it does not exist
 
int get_int_param (const std::string &s) const
 
std::string get_string_param (const std::string &s) const
 
void set_param (const std::string &s, double v)
 set a double, integer or string parameter
 
void set_param (const std::string &s, int v)
 
void set_param (const std::string &s, std::string v)
 
bool get_bool (const std::string &s, const bool def) const
 return bool parameter, return def if string not found
 
void acquire_params (const const_ref &)
 import all parameters from another object
 
void insert_object (const std::string &s, ref o)
 insert an object as parameter
 
void link_object (const std::string &s, ref o)
 insert a link to an object as parameter, the refered object must be defined elsewhere
 
bool get_component (const std::string &s, const_ref &) const
 get a contained object by string s, return wether it exists
 
bool get_component (const std::string &s, ref &)
 
const_ref get_component (const std::string &s) const
 get a contained object by string s, throw if it does not exist
 
ref get_component (const std::string &s)
 
template<typename T >
STD_TR1::shared_ptr< T > get_dynamic_component (const std::string &s)
 get a contained object with a dynamic cast More...
 
template<typename T >
STD_TR1::shared_ptr< const T > get_dynamic_component (const std::string &s) const
 
ref get_ref ()
 Get a shared_ptr to this object. More...
 
const_ref get_ref () const
 
template<typename T >
STD_TR1::shared_ptr< T > get_dynamic_ref ()
 Get a dynamic reference to this object. More...
 
template<typename T >
STD_TR1::shared_ptr< const T > get_dynamic_ref () const
 
void initialize (ref me)
 called by the xml_parser to initialize the object More...
 
void set_correct_ref (ref me)
 called to set reference to object More...
 
bool is_initialized () const
 returns whether the object has been correctly initialized
 
void require_initialized () const
 throws if is_initialized returns false
 
void force_reinit ()
 call if parameters are changed, object is completely reinitialized, children are not
 
void register_Observer (Observer::ref)
 register/unregister Observer: All registered Observers will have notify() called upon state change (reinit)
 
void unregister_Observer (Observer::ref)
 
size_t nparams () const
 returns total number of parameters
 
ref clone () const
 returns a complete clone of the Object and all parameters (with correct dynamic type)
 
virtual size_t verbosity ()
 

Static Public Member Functions

template<class T >
static STD_TR1::shared_ptr< const T > clone (const STD_TR1::shared_ptr< const T > r)
 
template<class T >
static STD_TR1::shared_ptr< T > clone (STD_TR1::shared_ptr< T > r)
 

Detailed Description

An abstract base class supporting xml_file_reading of input parameters.

Abstract class for an object that can be loaded from an XML file.

The object is identified in the XML file by the string "type", which is compared to the Name of the object class (virtual method name) for this to work, objects need to instantiate the template template class effective_manage_Loadable::registrator<derived_class> where derived_class inherits from loadable_Object

Parameters can be string, integer, double precision, or other loadable objects.

All derived objects should have a default constructor (that takes no arguments) The (dynamically correct) type of object is instantiated, then the parameters of the object (from the xml file) will be set (syntax see xml_input_object.cpp). Finally, the virtual method init is called. Here, parameters should be interpreted as needed. If necessary parameters are missing, initialize should throw E_missing_parameter("Name of missing parameter", "Name of constructable Object")

To ensure that cloned object (see clone below) are, indeed, identical copies, you can override init_from_clone( const_ref original ), ensuring that the objects internal state is identical to its clone. This is unnecessary in most cases, as objects will be constructed using the same parameters. However, especially in the case of randomly created objects, you might need to supply your own init_from_clone.

Member Function Documentation

◆ get_dynamic_component()

template<typename T >
STD_TR1::shared_ptr<T> loadable_Object::get_dynamic_component ( const std::string &  s)

get a contained object with a dynamic cast

To obtain e.g. a reference to a rectangle with param name rect_01, you could use Rectangle::ref r ( o->get_dynamic_component<Rectangle>("rect_01"); If the dynamic cast fails, or the parameter does not exist, the appropriate exception is thrown

◆ get_dynamic_ref()

template<typename T >
STD_TR1::shared_ptr<T> loadable_Object::get_dynamic_ref ( )

Get a dynamic reference to this object.

in extension to get_ref (see comments there on why you should not directly create a smart pointer) get_dynamic_ref allows you to correctly cast into a ref to a derived type, Rectangle::ref r = o->get_dynamic_ref<Rectangle>(); if the dynamic_cast fails, this will throw

◆ get_ref()

ref loadable_Object::get_ref ( )
inline

Get a shared_ptr to this object.

since a loadable_Object is typically created upon parsing the XML file, and referred to by smart pointers, you should not use something like STD_TR1::sharep_ptr p = but rather STD_TR1::sharep_ptr p = o->get_ref(); in such a way, all smart-pointers pointing to the object know of each other, and the object will not be freed twice

◆ initialize()

void loadable_Object::initialize ( ref  me)

called by the xml_parser to initialize the object

This is called by the xml_parser (see xml_input_object.hpp) to initialize the object. It will in turn call the virtual function void init() throw (E_missing_parameter, E_invalid_param_value, E_invalid_dynamic_cast); You will have to override this virtual function to define proper initialization of object data

◆ set_correct_ref()

void loadable_Object::set_correct_ref ( ref  me)

called to set reference to object

This normally happens automatically by calling initialize. Sometimes it might be needed to be called manually, if you need the object to parse parameters before being properly initialized. me should be the smart-pointer handling the object


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