| 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 |  | 
|---|
| 41 | RandomNumberDistributionFactory::TypeList RandomNumberGeneratorFactory::distribution = (RandomNumberDistributionFactory::TypeList) 0; | 
|---|
| 42 | RandomNumberEngineFactory::TypeList RandomNumberGeneratorFactory::engine = (RandomNumberEngineFactory::TypeList) 0; | 
|---|
| 43 | RandomNumberGeneratorFactory::EngineDistributionTable RandomNumberGeneratorFactory::GeneratorPrototypeTable; | 
|---|
| 44 |  | 
|---|
| 45 | RandomNumberGeneratorFactory::RandomNumberGeneratorFactory() | 
|---|
| 46 | { | 
|---|
| 47 | FillPrototypeTable(); | 
|---|
| 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()) { | 
|---|
| 57 | EmptyPrototypeTable< std::map<RandomNumberDistributionFactory::TypeList,RandomNumberGenerator*> > (iter->second); | 
|---|
| 58 | GeneratorPrototypeTable.erase(iter); | 
|---|
| 59 | } | 
|---|
| 60 | GeneratorPrototypeTable.clear(); | 
|---|
| 61 | } | 
|---|
| 62 |  | 
|---|
| 63 | void 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 |  | 
|---|
| 102 | RandomNumberGenerator& RandomNumberGeneratorFactory::makeRandomNumberGenerator() const | 
|---|
| 103 | { | 
|---|
| 104 | // Instantiate and return (implicitly creates a copy of the stored prototype) | 
|---|
| 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 ]); | 
|---|
| 111 | } | 
|---|
| 112 |  | 
|---|
| 113 | RandomNumberGenerator& RandomNumberGeneratorFactory::makeRandomNumberGenerator( | 
|---|
| 114 | std::string engine_type, | 
|---|
| 115 | std::string distribution_type | 
|---|
| 116 | ) const | 
|---|
| 117 | { | 
|---|
| 118 | RandomNumberEngineFactory::TypeList eng_type; | 
|---|
| 119 | RandomNumberDistributionFactory::TypeList dis_type; | 
|---|
| 120 |  | 
|---|
| 121 | // Instantiate and return (implicitly creates a copy of the stored prototype) | 
|---|
| 122 | if (!engine_type.empty()) { | 
|---|
| 123 | eng_type = RandomNumberEngineFactory::getInstance().getEnum(engine_type); | 
|---|
| 124 | } else | 
|---|
| 125 | eng_type = RandomNumberEngineFactory::getInstance().getCurrentTypeEnum(); | 
|---|
| 126 | if (!distribution_type.empty()) { | 
|---|
| 127 | dis_type = RandomNumberDistributionFactory::getInstance().getEnum(distribution_type); | 
|---|
| 128 | } else | 
|---|
| 129 | dis_type = RandomNumberDistributionFactory::getInstance().getCurrentTypeEnum(); | 
|---|
| 130 | return (*GeneratorPrototypeTable[ eng_type ][ dis_type ]); | 
|---|
| 131 |  | 
|---|
| 132 | return (*GeneratorPrototypeTable[ eng_type ][ dis_type ]); | 
|---|
| 133 | } | 
|---|
| 134 |  | 
|---|
| 135 | void RandomNumberGeneratorFactory::setEngine(std::string engine_type) | 
|---|
| 136 | { | 
|---|
| 137 | RandomNumberEngineFactory::getInstance().setCurrentType(engine_type); | 
|---|
| 138 | } | 
|---|
| 139 |  | 
|---|
| 140 | const std::string &RandomNumberGeneratorFactory::getEngineName() const | 
|---|
| 141 | { | 
|---|
| 142 | return RandomNumberEngineFactory::getInstance().getCurrentTypeName(); | 
|---|
| 143 | } | 
|---|
| 144 |  | 
|---|
| 145 | void RandomNumberGeneratorFactory::setDistribution(std::string distribution_type) | 
|---|
| 146 | { | 
|---|
| 147 | RandomNumberDistributionFactory::getInstance().setCurrentType(distribution_type); | 
|---|
| 148 | } | 
|---|
| 149 |  | 
|---|
| 150 | const std::string &RandomNumberGeneratorFactory::getDistributionName() const | 
|---|
| 151 | { | 
|---|
| 152 | return RandomNumberDistributionFactory::getInstance().getCurrentTypeName(); | 
|---|
| 153 | } | 
|---|
| 154 |  | 
|---|
| 155 | CONSTRUCT_SINGLETON(RandomNumberGeneratorFactory) | 
|---|
| 156 |  | 
|---|
| 157 | #include "RandomNumberGeneratorFactory.undef" | 
|---|
| 158 |  | 
|---|