/* * Project: MoleCuilder * Description: creates and alters molecular systems * Copyright (C) 2010 University of Bonn. All rights reserved. * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. */ /* * hello.cpp * * Created on: Sep 20, 2011 * Author: heber */ // include config.h #ifdef HAVE_CONFIG_H #include #endif #include #include "CodePatterns/MemDebug.hpp" #include #include #include #include "CodePatterns/Registry.hpp" #include "CodePatterns/Singleton.hpp" #include "CodePatterns/Singleton_impl.hpp" #include "CodePatterns/Registry_impl.hpp" class Atrait; class Otrait { public: Otrait(const std::string &_text) : text(_text) {} ~Otrait() {} const std::string & getName() const { return text; } private: std::string text; }; class Atrait : public Otrait { public: Atrait() : Otrait(std::string("Atrait")) { options.insert( std::make_pair ( "AOtrait", new Otrait("AOtrait") ) ); } Atrait(const Atrait &trait) : Otrait(trait) { for (Omap::const_iterator iter = trait.options.begin(); iter != trait.options.end(); ++iter) options.insert( std::make_pair ( "copied AOtrait", new Otrait(*(iter->second)) ) ); } ~Atrait() { for (Omap::iterator iter = options.begin(); iter != options.end(); ++iter) delete iter->second; options.clear(); } private: typedef std::map Omap; Omap options; }; class Action { public: Action(const Atrait &_trait) : trait(_trait) {} ~Action() {} const char * greet() { return "hello, this is MoleCuilder."; } const std::string & getName() const { return trait.getName(); } const Atrait trait; private: }; /** Action Registry. * * The Action registry is a storage for any Action instance to retrieved by name. * It is a singleton and can be called from anywhere. * */ class ActionRegistry : public Singleton, public Registry { friend class Singleton; //friend class Registry; public: Action* getActionByName(const std::string name); void fillRegistry(); private: ActionRegistry(); ~ActionRegistry(); }; /** Constructor for class ActionRegistry. */ ActionRegistry::ActionRegistry() { std::cout << "ActionRegistry::ActionRegistry() called, instance is " << this << "." << std::endl; fillRegistry(); } void ActionRegistry::fillRegistry() { Action *presentAction = NULL; { Atrait atrait; presentAction = new Action(atrait); registerInstance(presentAction); } std::cout << "presentAction instance is " << presentAction << "." << std::endl; } /** Destructor for class ActionRegistry. */ ActionRegistry::~ActionRegistry() { std::cout << "ActionRegistry::~ActionRegistry() called, instance is " << this << "." << std::endl; cleanup(); } /** Just passes on call to Registry::getByName(). * \param name name of Action * \return pointer to Action */ Action* ActionRegistry::getActionByName(const std::string name) { return getByName(name); } char const* greet() { return ActionRegistry::getInstance().getActionByName("Atrait")->greet(); } BOOST_PYTHON_MODULE(Pythontest) { boost::python::def("greet", greet); }