source: src/RandomNumbers/RandomNumberGeneratorFactory.cpp@ 6ff0c8

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 6ff0c8 was 076a77, checked in by Frederik Heber <heber@…>, 14 years ago

RandomNumberGeneratorFactory nows uses Clone instead of Creator pattern.

  • requires CodePatterns 1.0.4.
  • RandomNumberGenerator_Encapsulation implements clone() with calls the constructor ...
  • ... who in turn (sadly) needs some evil dynamic_cast to get the complex type from the simple interface, because we need access to true engine and distribution. The instances themselves are obtained properly from the respective factories. However, the constructor is protected now.
  • FIX: friend classes can actually be forward declared.
  • Property mode set to 100644
File size: 5.3 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 * RandomNumberGeneratorFactory.cpp
10 *
11 * Created on: Dec 31, 2010
12 * Author: heber
13 */
14
15// include config.h
16#ifdef HAVE_CONFIG_H
17#include <config.h>
18#endif
19
20#include "CodePatterns/MemDebug.hpp"
21
22#include <utility>
23#include "CodePatterns/Singleton_impl.hpp"
24#include "CodePatterns/Assert.hpp"
25
26#include "TemplatePowerSetGenerator.hpp"
27#include "EmptyPrototypeTable.hpp"
28
29#include <boost/preprocessor/facilities/empty.hpp>
30#include <boost/preprocessor/punctuation/paren.hpp>
31#include <boost/preprocessor/seq/for_each_product.hpp>
32#include <boost/preprocessor/facilities/identity.hpp>
33#include <boost/preprocessor/facilities/expand.hpp>
34#include <boost/preprocessor/seq/seq.hpp>
35
36#include "RandomNumberGenerator_Encapsulation.hpp"
37
38#include "RandomNumberGeneratorFactory.hpp"
39#include "RandomNumberGeneratorFactory.def"
40
41RandomNumberDistributionFactory::TypeList RandomNumberGeneratorFactory::distribution = (RandomNumberDistributionFactory::TypeList) 0;
42RandomNumberEngineFactory::TypeList RandomNumberGeneratorFactory::engine = (RandomNumberEngineFactory::TypeList) 0;
43RandomNumberGeneratorFactory::EngineDistributionTable RandomNumberGeneratorFactory::GeneratorPrototypeTable;
44
45RandomNumberGeneratorFactory::RandomNumberGeneratorFactory()
46{
47 FillPrototypeTable();
48}
49
50RandomNumberGeneratorFactory::~RandomNumberGeneratorFactory()
51{
52 // clear out factories map to allow boost::shared_ptr to do their work (i.e. to release mem)
53 // this is necessary as factories is a object
54 for (EngineDistributionTable::iterator iter = GeneratorPrototypeTable.begin();
55 !GeneratorPrototypeTable.empty();
56 iter = GeneratorPrototypeTable.begin()) {
57 EmptyPrototypeTable< std::map<RandomNumberDistributionFactory::TypeList,Clone<RandomNumberGenerator> *> > (iter->second);
58 GeneratorPrototypeTable.erase(iter);
59 }
60 GeneratorPrototypeTable.clear();
61}
62
63void RandomNumberGeneratorFactory::FillPrototypeTable()
64{
65 // fill GeneratorPrototypeTable
66#define SequenceElementizer(z,data) (data)
67
68#define distributionengine_seqseq \
69 BOOST_PP_SEQ_FOR_EACH_PRODUCT(SequenceElementizer, (engine_seq)(distribution_seq))
70#define distributionengine_seqseq_a \
71 BOOST_PP_SEQ_FOR_EACH_PRODUCT(SequenceElementizer, (engine_seq_a)(distribution_seq))
72
73#define suffixseq ()(<>)
74#define tableseq (*EnginePrototypeTable)(*DistributionPrototypeTable)
75#define name_spaces_seq (RandomNumberEngineFactory::)(RandomNumberDistributionFactory::)
76
77#define size_tupels BOOST_PP_SEQ_SIZE(tableseq)
78
79#define TableItemPrinter(z,n,sequence) \
80 [ \
81 BOOST_PP_SEQ_ELEM(n,name_spaces_seq) \
82 BOOST_PP_SEQ_ELEM(n,sequence) \
83 ]
84
85#define TemplateItemPrinter(z,n,sequence) \
86 BOOST_PP_COMMA_IF(n) \
87 boost:: \
88 BOOST_PP_SEQ_ELEM(n,sequence) \
89 BOOST_PP_SEQ_ELEM(n,suffixseq)
90
91#define ParamItemPrinter(z,n,sequence)
92
93#define BOOST_PP_LOCAL_MACRO(n) seqitems_as_enum_key_multidimmap(~, n, size_tupels, distributionengine_seqseq, GeneratorPrototypeTable, TableItemPrinter, new RandomNumberGenerator_Encapsulation , TemplateItemPrinter, ParamItemPrinter)
94#define BOOST_PP_LOCAL_LIMITS (0, BOOST_PP_SEQ_SIZE(distributionengine_seqseq)-1 )
95#include BOOST_PP_LOCAL_ITERATE()
96
97#define BOOST_PP_LOCAL_MACRO(n) seqitems_as_enum_key_multidimmap(~, n, size_tupels, distributionengine_seqseq_a, GeneratorPrototypeTable, TableItemPrinter, new RandomNumberGenerator_Encapsulation , TemplateItemPrinter, ParamItemPrinter)
98#define BOOST_PP_LOCAL_LIMITS (0, BOOST_PP_SEQ_SIZE(distributionengine_seqseq_a)-1 )
99#include BOOST_PP_LOCAL_ITERATE()
100}
101
102RandomNumberGenerator* RandomNumberGeneratorFactory::makeRandomNumberGenerator() const
103{
104 // Instantiate and return (implicitly creates a copy of the stored prototype)
105 return (GeneratorPrototypeTable[engine][distribution]->clone());
106}
107
108RandomNumberGenerator* RandomNumberGeneratorFactory::makeRandomNumberGenerator(
109 std::string engine_type,
110 std::string distribution_type
111 ) const
112{
113 RandomNumberEngineFactory::TypeList eng_type;
114 RandomNumberDistributionFactory::TypeList dis_type;
115
116 // Instantiate and return (implicitly creates a copy of the stored prototype)
117 if (!engine_type.empty()) {
118 eng_type = RandomNumberEngineFactory::getInstance().getEnum(engine_type);
119 } else
120 eng_type = engine;
121 if (!distribution_type.empty()) {
122 dis_type = RandomNumberDistributionFactory::getInstance().getEnum(distribution_type);
123 } else
124 dis_type = distribution;
125 return (GeneratorPrototypeTable[ eng_type ][ dis_type ]->clone());
126}
127
128void RandomNumberGeneratorFactory::setEngine(std::string engine_type)
129{
130 engine = RandomNumberEngineFactory::getInstance().getEnum(engine_type);
131}
132
133const std::string &RandomNumberGeneratorFactory::getEngineName() const
134{
135 return RandomNumberEngineFactory::getInstance().getName(engine);
136}
137
138void RandomNumberGeneratorFactory::setDistribution(std::string distribution_type)
139{
140 distribution = RandomNumberDistributionFactory::getInstance().getEnum(distribution_type);
141}
142
143const std::string &RandomNumberGeneratorFactory::getDistributionName() const
144{
145 return RandomNumberDistributionFactory::getInstance().getName(distribution);
146}
147
148CONSTRUCT_SINGLETON(RandomNumberGeneratorFactory)
149
150#include "RandomNumberGeneratorFactory.undef"
151
Note: See TracBrowser for help on using the repository browser.