source: src/Actions/hello.cpp@ 751d7f1

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 751d7f1 was 071243, checked in by Frederik Heber <heber@…>, 13 years ago

Enhanced tiny example to resemble action structure.

  • Pythontest now also works via a true ActionRegistry along with Actions and "Atrait" and "Otrait".
  • this was done to understand runtime (glibc corruption) errors in the full pyMoleCuilder module). However, fault was due to forgotten inclusion of CodePatterns library.
  • In the end, we have an Action that contains the wrapped greet function in a likewise static manner as COMMAND().
  • Property mode set to 100644
File size: 3.2 KB
Line 
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
34class Atrait;
35
36class Otrait
37{
38public:
39 Otrait(const std::string &_text) : text(_text) {}
40 ~Otrait() {}
41 const std::string & getName() const { return text; }
42private:
43 std::string text;
44};
45
46class Atrait : public Otrait
47{
48public:
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 }
64private:
65 typedef std::map<std::string, Otrait * > Omap;
66
67 Omap options;
68};
69
70class Action
71{
72public:
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;
81private:
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 */
91class ActionRegistry : public Singleton<ActionRegistry>, public Registry<Action>
92{
93 friend class Singleton<ActionRegistry>;
94 //friend class Registry<Action>;
95
96public:
97 Action* getActionByName(const std::string name);
98 void fillRegistry();
99
100private:
101 ActionRegistry();
102 ~ActionRegistry();
103};
104
105/** Constructor for class ActionRegistry.
106 */
107ActionRegistry::ActionRegistry()
108{
109 std::cout << "ActionRegistry::ActionRegistry() called, instance is " << this << "." << std::endl;
110 fillRegistry();
111}
112
113void 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 */
127ActionRegistry::~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 */
137Action* ActionRegistry::getActionByName(const std::string name)
138{
139 return getByName(name);
140}
141
142
143char const* greet()
144{
145 return ActionRegistry::getInstance().getActionByName("Atrait")->greet();
146}
147
148BOOST_PYTHON_MODULE(Pythontest)
149{
150 boost::python::def("greet", greet);
151}
Note: See TracBrowser for help on using the repository browser.