1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2010 University of Bonn. All rights reserved.
|
---|
5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * hello.cpp
|
---|
10 | *
|
---|
11 | * Created on: Sep 20, 2011
|
---|
12 | * Author: heber
|
---|
13 | */
|
---|
14 |
|
---|
15 | // include config.h
|
---|
16 | #ifdef HAVE_CONFIG_H
|
---|
17 | #include <config.h>
|
---|
18 | #endif
|
---|
19 |
|
---|
20 | #include <boost/python.hpp>
|
---|
21 |
|
---|
22 | #include "CodePatterns/MemDebug.hpp"
|
---|
23 |
|
---|
24 | #include <iostream>
|
---|
25 | #include <map>
|
---|
26 | #include <string>
|
---|
27 |
|
---|
28 | #include "CodePatterns/Registry.hpp"
|
---|
29 | #include "CodePatterns/Singleton.hpp"
|
---|
30 |
|
---|
31 | #include "CodePatterns/Singleton_impl.hpp"
|
---|
32 | #include "CodePatterns/Registry_impl.hpp"
|
---|
33 |
|
---|
34 | class Atrait;
|
---|
35 |
|
---|
36 | class Otrait
|
---|
37 | {
|
---|
38 | public:
|
---|
39 | Otrait(const std::string &_text) : text(_text) {}
|
---|
40 | ~Otrait() {}
|
---|
41 | const std::string & getName() const { return text; }
|
---|
42 | private:
|
---|
43 | std::string text;
|
---|
44 | };
|
---|
45 |
|
---|
46 | class Atrait : public Otrait
|
---|
47 | {
|
---|
48 | public:
|
---|
49 | Atrait() : Otrait(std::string("Atrait"))
|
---|
50 | {
|
---|
51 | options.insert( std::make_pair ( "AOtrait", new Otrait("AOtrait") ) );
|
---|
52 | }
|
---|
53 | Atrait(const Atrait &trait) : Otrait(trait)
|
---|
54 | {
|
---|
55 | for (Omap::const_iterator iter = trait.options.begin(); iter != trait.options.end(); ++iter)
|
---|
56 | options.insert( std::make_pair ( "copied AOtrait", new Otrait(*(iter->second)) ) );
|
---|
57 | }
|
---|
58 | ~Atrait()
|
---|
59 | {
|
---|
60 | for (Omap::iterator iter = options.begin(); iter != options.end(); ++iter)
|
---|
61 | delete iter->second;
|
---|
62 | options.clear();
|
---|
63 | }
|
---|
64 | private:
|
---|
65 | typedef std::map<std::string, Otrait * > Omap;
|
---|
66 |
|
---|
67 | Omap options;
|
---|
68 | };
|
---|
69 |
|
---|
70 | class Action
|
---|
71 | {
|
---|
72 | public:
|
---|
73 | Action(const Atrait &_trait) : trait(_trait) {}
|
---|
74 | ~Action() {}
|
---|
75 |
|
---|
76 | const char * greet() { return "hello, this is MoleCuilder."; }
|
---|
77 |
|
---|
78 | const std::string & getName() const { return trait.getName(); }
|
---|
79 |
|
---|
80 | const Atrait trait;
|
---|
81 | private:
|
---|
82 | };
|
---|
83 |
|
---|
84 |
|
---|
85 | /** Action Registry.
|
---|
86 | *
|
---|
87 | * The Action registry is a storage for any Action instance to retrieved by name.
|
---|
88 | * It is a singleton and can be called from anywhere.
|
---|
89 | *
|
---|
90 | */
|
---|
91 | class ActionRegistry : public Singleton<ActionRegistry>, public Registry<Action>
|
---|
92 | {
|
---|
93 | friend class Singleton<ActionRegistry>;
|
---|
94 | //friend class Registry<Action>;
|
---|
95 |
|
---|
96 | public:
|
---|
97 | Action* getActionByName(const std::string name);
|
---|
98 | void fillRegistry();
|
---|
99 |
|
---|
100 | private:
|
---|
101 | ActionRegistry();
|
---|
102 | ~ActionRegistry();
|
---|
103 | };
|
---|
104 |
|
---|
105 | /** Constructor for class ActionRegistry.
|
---|
106 | */
|
---|
107 | ActionRegistry::ActionRegistry()
|
---|
108 | {
|
---|
109 | std::cout << "ActionRegistry::ActionRegistry() called, instance is " << this << "." << std::endl;
|
---|
110 | fillRegistry();
|
---|
111 | }
|
---|
112 |
|
---|
113 | void ActionRegistry::fillRegistry()
|
---|
114 | {
|
---|
115 | Action *presentAction = NULL;
|
---|
116 | {
|
---|
117 | Atrait atrait;
|
---|
118 | presentAction = new Action(atrait);
|
---|
119 | registerInstance(presentAction);
|
---|
120 | }
|
---|
121 | std::cout << "presentAction instance is " << presentAction << "." << std::endl;
|
---|
122 | }
|
---|
123 |
|
---|
124 |
|
---|
125 | /** Destructor for class ActionRegistry.
|
---|
126 | */
|
---|
127 | ActionRegistry::~ActionRegistry()
|
---|
128 | {
|
---|
129 | std::cout << "ActionRegistry::~ActionRegistry() called, instance is " << this << "." << std::endl;
|
---|
130 | cleanup();
|
---|
131 | }
|
---|
132 |
|
---|
133 | /** Just passes on call to Registry<Action>::getByName().
|
---|
134 | * \param name name of Action
|
---|
135 | * \return pointer to Action
|
---|
136 | */
|
---|
137 | Action* ActionRegistry::getActionByName(const std::string name)
|
---|
138 | {
|
---|
139 | return getByName(name);
|
---|
140 | }
|
---|
141 |
|
---|
142 |
|
---|
143 | char const* greet()
|
---|
144 | {
|
---|
145 | return ActionRegistry::getInstance().getActionByName("Atrait")->greet();
|
---|
146 | }
|
---|
147 |
|
---|
148 | BOOST_PYTHON_MODULE(Pythontest)
|
---|
149 | {
|
---|
150 | boost::python::def("greet", greet);
|
---|
151 | }
|
---|