[3f9eba] | 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"
|
---|
[c9bc2b7] | 27 | #include "EmptyPrototypeTable.hpp"
|
---|
[3f9eba] | 28 |
|
---|
| 29 | #include <boost/preprocessor/facilities/empty.hpp>
|
---|
| 30 | #include <boost/preprocessor/punctuation/paren.hpp>
|
---|
[c9bc2b7] | 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 |
|
---|
[1d5a871] | 36 | #include "RandomNumberGenerator_Encapsulation.hpp"
|
---|
[3f9eba] | 37 |
|
---|
| 38 | #include "RandomNumberGeneratorFactory.hpp"
|
---|
[1d5a871] | 39 | #include "RandomNumberGeneratorFactory.def"
|
---|
[3f9eba] | 40 |
|
---|
[1d5a871] | 41 | RandomNumberDistributionFactory::TypeList RandomNumberGeneratorFactory::distribution = (RandomNumberDistributionFactory::TypeList) 0;
|
---|
| 42 | RandomNumberEngineFactory::TypeList RandomNumberGeneratorFactory::engine = (RandomNumberEngineFactory::TypeList) 0;
|
---|
[c9bc2b7] | 43 | RandomNumberGeneratorFactory::EngineDistributionTable RandomNumberGeneratorFactory::GeneratorPrototypeTable;
|
---|
[3f9eba] | 44 |
|
---|
| 45 | RandomNumberGeneratorFactory::RandomNumberGeneratorFactory()
|
---|
| 46 | {
|
---|
[c9bc2b7] | 47 | FillPrototypeTable();
|
---|
[3f9eba] | 48 | }
|
---|
| 49 |
|
---|
| 50 | RandomNumberGeneratorFactory::~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()) {
|
---|
[076a77] | 57 | EmptyPrototypeTable< std::map<RandomNumberDistributionFactory::TypeList,Clone<RandomNumberGenerator> *> > (iter->second);
|
---|
[3f9eba] | 58 | GeneratorPrototypeTable.erase(iter);
|
---|
| 59 | }
|
---|
| 60 | GeneratorPrototypeTable.clear();
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[c9bc2b7] | 63 | void RandomNumberGeneratorFactory::FillPrototypeTable()
|
---|
[3f9eba] | 64 | {
|
---|
| 65 | // fill GeneratorPrototypeTable
|
---|
[c9bc2b7] | 66 | #define SequenceElementizer(z,data) (data)
|
---|
[3f9eba] | 67 |
|
---|
[c9bc2b7] | 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))
|
---|
[3f9eba] | 72 |
|
---|
[076a77] | 73 | #define suffixseq ()(<>)
|
---|
[3f9eba] | 74 | #define tableseq (*EnginePrototypeTable)(*DistributionPrototypeTable)
|
---|
[c9bc2b7] | 75 | #define name_spaces_seq (RandomNumberEngineFactory::)(RandomNumberDistributionFactory::)
|
---|
| 76 |
|
---|
| 77 | #define size_tupels BOOST_PP_SEQ_SIZE(tableseq)
|
---|
[3f9eba] | 78 |
|
---|
| 79 | #define TableItemPrinter(z,n,sequence) \
|
---|
[c9bc2b7] | 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) \
|
---|
[3f9eba] | 86 | BOOST_PP_COMMA_IF(n) \
|
---|
| 87 | boost:: \
|
---|
| 88 | BOOST_PP_SEQ_ELEM(n,sequence) \
|
---|
| 89 | BOOST_PP_SEQ_ELEM(n,suffixseq)
|
---|
| 90 |
|
---|
[c9bc2b7] | 91 | #define ParamItemPrinter(z,n,sequence)
|
---|
[3f9eba] | 92 |
|
---|
[076a77] | 93 | #define BOOST_PP_LOCAL_MACRO(n) seqitems_as_enum_key_multidimmap(~, n, size_tupels, distributionengine_seqseq, GeneratorPrototypeTable, TableItemPrinter, new RandomNumberGenerator_Encapsulation , TemplateItemPrinter, ParamItemPrinter)
|
---|
[3f9eba] | 94 | #define BOOST_PP_LOCAL_LIMITS (0, BOOST_PP_SEQ_SIZE(distributionengine_seqseq)-1 )
|
---|
| 95 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
| 96 |
|
---|
[076a77] | 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)
|
---|
[3f9eba] | 98 | #define BOOST_PP_LOCAL_LIMITS (0, BOOST_PP_SEQ_SIZE(distributionengine_seqseq_a)-1 )
|
---|
| 99 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[c9bc2b7] | 102 | RandomNumberGenerator* RandomNumberGeneratorFactory::makeRandomNumberGenerator() const
|
---|
[3f9eba] | 103 | {
|
---|
| 104 | // Instantiate and return (implicitly creates a copy of the stored prototype)
|
---|
[9964ff] | 105 | RandomNumberEngineFactory::TypeList eng_type =
|
---|
| 106 | RandomNumberEngineFactory::getInstance().getCurrentTypeEnum();
|
---|
| 107 | RandomNumberDistributionFactory::TypeList dis_type =
|
---|
| 108 | RandomNumberDistributionFactory::getInstance().getCurrentTypeEnum();
|
---|
| 109 |
|
---|
| 110 | return (GeneratorPrototypeTable[ eng_type ][ dis_type ]->clone());
|
---|
[3f9eba] | 111 | }
|
---|
| 112 |
|
---|
[c9bc2b7] | 113 | RandomNumberGenerator* RandomNumberGeneratorFactory::makeRandomNumberGenerator(
|
---|
| 114 | std::string engine_type,
|
---|
| 115 | std::string distribution_type
|
---|
| 116 | ) const
|
---|
[3f9eba] | 117 | {
|
---|
[1d5a871] | 118 | RandomNumberEngineFactory::TypeList eng_type;
|
---|
| 119 | RandomNumberDistributionFactory::TypeList dis_type;
|
---|
[3f9eba] | 120 |
|
---|
| 121 | // Instantiate and return (implicitly creates a copy of the stored prototype)
|
---|
| 122 | if (!engine_type.empty()) {
|
---|
[c9bc2b7] | 123 | eng_type = RandomNumberEngineFactory::getInstance().getEnum(engine_type);
|
---|
[3f9eba] | 124 | } else
|
---|
[9964ff] | 125 | eng_type = RandomNumberEngineFactory::getInstance().getCurrentTypeEnum();
|
---|
[3f9eba] | 126 | if (!distribution_type.empty()) {
|
---|
[c9bc2b7] | 127 | dis_type = RandomNumberDistributionFactory::getInstance().getEnum(distribution_type);
|
---|
[3f9eba] | 128 | } else
|
---|
[9964ff] | 129 | dis_type = RandomNumberDistributionFactory::getInstance().getCurrentTypeEnum();
|
---|
| 130 | return (GeneratorPrototypeTable[ eng_type ][ dis_type ]->clone());
|
---|
| 131 |
|
---|
[076a77] | 132 | return (GeneratorPrototypeTable[ eng_type ][ dis_type ]->clone());
|
---|
[3f9eba] | 133 | }
|
---|
| 134 |
|
---|
| 135 | void RandomNumberGeneratorFactory::setEngine(std::string engine_type)
|
---|
| 136 | {
|
---|
[9964ff] | 137 | RandomNumberEngineFactory::getInstance().setCurrentType(engine_type);
|
---|
[3f9eba] | 138 | }
|
---|
| 139 |
|
---|
[c9bc2b7] | 140 | const std::string &RandomNumberGeneratorFactory::getEngineName() const
|
---|
[3f9eba] | 141 | {
|
---|
[9964ff] | 142 | return RandomNumberEngineFactory::getInstance().getCurrentTypeName();
|
---|
[3f9eba] | 143 | }
|
---|
| 144 |
|
---|
| 145 | void RandomNumberGeneratorFactory::setDistribution(std::string distribution_type)
|
---|
| 146 | {
|
---|
[9964ff] | 147 | RandomNumberDistributionFactory::getInstance().setCurrentType(distribution_type);
|
---|
[3f9eba] | 148 | }
|
---|
| 149 |
|
---|
[c9bc2b7] | 150 | const std::string &RandomNumberGeneratorFactory::getDistributionName() const
|
---|
[3f9eba] | 151 | {
|
---|
[9964ff] | 152 | return RandomNumberDistributionFactory::getInstance().getCurrentTypeName();
|
---|
[3f9eba] | 153 | }
|
---|
| 154 |
|
---|
| 155 | CONSTRUCT_SINGLETON(RandomNumberGeneratorFactory)
|
---|
| 156 |
|
---|
[1d5a871] | 157 | #include "RandomNumberGeneratorFactory.undef"
|
---|
[3f9eba] | 158 |
|
---|