[3f9eba] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
[0aa122] | 4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
[94d5ac6] | 5 | *
|
---|
| 6 | *
|
---|
| 7 | * This file is part of MoleCuilder.
|
---|
| 8 | *
|
---|
| 9 | * MoleCuilder is free software: you can redistribute it and/or modify
|
---|
| 10 | * it under the terms of the GNU General Public License as published by
|
---|
| 11 | * the Free Software Foundation, either version 2 of the License, or
|
---|
| 12 | * (at your option) any later version.
|
---|
| 13 | *
|
---|
| 14 | * MoleCuilder is distributed in the hope that it will be useful,
|
---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 17 | * GNU General Public License for more details.
|
---|
| 18 | *
|
---|
| 19 | * You should have received a copy of the GNU General Public License
|
---|
| 20 | * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
|
---|
[3f9eba] | 21 | */
|
---|
| 22 |
|
---|
| 23 | /*
|
---|
| 24 | * RandomNumberGeneratorFactory.cpp
|
---|
| 25 | *
|
---|
| 26 | * Created on: Dec 31, 2010
|
---|
| 27 | * Author: heber
|
---|
| 28 | */
|
---|
| 29 |
|
---|
| 30 | // include config.h
|
---|
| 31 | #ifdef HAVE_CONFIG_H
|
---|
| 32 | #include <config.h>
|
---|
| 33 | #endif
|
---|
| 34 |
|
---|
[9eb71b3] | 35 | //#include "CodePatterns/MemDebug.hpp"
|
---|
[3f9eba] | 36 |
|
---|
| 37 | #include <utility>
|
---|
| 38 | #include "CodePatterns/Singleton_impl.hpp"
|
---|
| 39 | #include "CodePatterns/Assert.hpp"
|
---|
| 40 |
|
---|
| 41 | #include "TemplatePowerSetGenerator.hpp"
|
---|
[c9bc2b7] | 42 | #include "EmptyPrototypeTable.hpp"
|
---|
[3f9eba] | 43 |
|
---|
| 44 | #include <boost/preprocessor/facilities/empty.hpp>
|
---|
| 45 | #include <boost/preprocessor/punctuation/paren.hpp>
|
---|
[c9bc2b7] | 46 | #include <boost/preprocessor/seq/for_each_product.hpp>
|
---|
| 47 | #include <boost/preprocessor/facilities/identity.hpp>
|
---|
| 48 | #include <boost/preprocessor/facilities/expand.hpp>
|
---|
| 49 | #include <boost/preprocessor/seq/seq.hpp>
|
---|
| 50 |
|
---|
[1d5a871] | 51 | #include "RandomNumberGenerator_Encapsulation.hpp"
|
---|
[3f9eba] | 52 |
|
---|
| 53 | #include "RandomNumberGeneratorFactory.hpp"
|
---|
[1d5a871] | 54 | #include "RandomNumberGeneratorFactory.def"
|
---|
[3f9eba] | 55 |
|
---|
[1d5a871] | 56 | RandomNumberDistributionFactory::TypeList RandomNumberGeneratorFactory::distribution = (RandomNumberDistributionFactory::TypeList) 0;
|
---|
| 57 | RandomNumberEngineFactory::TypeList RandomNumberGeneratorFactory::engine = (RandomNumberEngineFactory::TypeList) 0;
|
---|
[c9bc2b7] | 58 | RandomNumberGeneratorFactory::EngineDistributionTable RandomNumberGeneratorFactory::GeneratorPrototypeTable;
|
---|
[3f9eba] | 59 |
|
---|
| 60 | RandomNumberGeneratorFactory::RandomNumberGeneratorFactory()
|
---|
| 61 | {
|
---|
[c9bc2b7] | 62 | FillPrototypeTable();
|
---|
[3f9eba] | 63 | }
|
---|
| 64 |
|
---|
| 65 | RandomNumberGeneratorFactory::~RandomNumberGeneratorFactory()
|
---|
| 66 | {
|
---|
| 67 | // clear out factories map to allow boost::shared_ptr to do their work (i.e. to release mem)
|
---|
| 68 | // this is necessary as factories is a object
|
---|
| 69 | for (EngineDistributionTable::iterator iter = GeneratorPrototypeTable.begin();
|
---|
| 70 | !GeneratorPrototypeTable.empty();
|
---|
| 71 | iter = GeneratorPrototypeTable.begin()) {
|
---|
[63839f] | 72 | EmptyPrototypeTable< std::map<RandomNumberDistributionFactory::TypeList,RandomNumberGenerator*> > (iter->second);
|
---|
[3f9eba] | 73 | GeneratorPrototypeTable.erase(iter);
|
---|
| 74 | }
|
---|
| 75 | GeneratorPrototypeTable.clear();
|
---|
| 76 | }
|
---|
| 77 |
|
---|
[c9bc2b7] | 78 | void RandomNumberGeneratorFactory::FillPrototypeTable()
|
---|
[3f9eba] | 79 | {
|
---|
| 80 | // fill GeneratorPrototypeTable
|
---|
[c9bc2b7] | 81 | #define SequenceElementizer(z,data) (data)
|
---|
[3f9eba] | 82 |
|
---|
[c9bc2b7] | 83 | #define distributionengine_seqseq \
|
---|
| 84 | BOOST_PP_SEQ_FOR_EACH_PRODUCT(SequenceElementizer, (engine_seq)(distribution_seq))
|
---|
| 85 | #define distributionengine_seqseq_a \
|
---|
| 86 | BOOST_PP_SEQ_FOR_EACH_PRODUCT(SequenceElementizer, (engine_seq_a)(distribution_seq))
|
---|
[3f9eba] | 87 |
|
---|
[076a77] | 88 | #define suffixseq ()(<>)
|
---|
[3f9eba] | 89 | #define tableseq (*EnginePrototypeTable)(*DistributionPrototypeTable)
|
---|
[c9bc2b7] | 90 | #define name_spaces_seq (RandomNumberEngineFactory::)(RandomNumberDistributionFactory::)
|
---|
| 91 |
|
---|
| 92 | #define size_tupels BOOST_PP_SEQ_SIZE(tableseq)
|
---|
[3f9eba] | 93 |
|
---|
| 94 | #define TableItemPrinter(z,n,sequence) \
|
---|
[c9bc2b7] | 95 | [ \
|
---|
| 96 | BOOST_PP_SEQ_ELEM(n,name_spaces_seq) \
|
---|
| 97 | BOOST_PP_SEQ_ELEM(n,sequence) \
|
---|
| 98 | ]
|
---|
| 99 |
|
---|
| 100 | #define TemplateItemPrinter(z,n,sequence) \
|
---|
[3f9eba] | 101 | BOOST_PP_COMMA_IF(n) \
|
---|
| 102 | boost:: \
|
---|
| 103 | BOOST_PP_SEQ_ELEM(n,sequence) \
|
---|
| 104 | BOOST_PP_SEQ_ELEM(n,suffixseq)
|
---|
| 105 |
|
---|
[c9bc2b7] | 106 | #define ParamItemPrinter(z,n,sequence)
|
---|
[3f9eba] | 107 |
|
---|
[076a77] | 108 | #define BOOST_PP_LOCAL_MACRO(n) seqitems_as_enum_key_multidimmap(~, n, size_tupels, distributionengine_seqseq, GeneratorPrototypeTable, TableItemPrinter, new RandomNumberGenerator_Encapsulation , TemplateItemPrinter, ParamItemPrinter)
|
---|
[3f9eba] | 109 | #define BOOST_PP_LOCAL_LIMITS (0, BOOST_PP_SEQ_SIZE(distributionengine_seqseq)-1 )
|
---|
| 110 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
| 111 |
|
---|
[076a77] | 112 | #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] | 113 | #define BOOST_PP_LOCAL_LIMITS (0, BOOST_PP_SEQ_SIZE(distributionengine_seqseq_a)-1 )
|
---|
| 114 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
| 115 | }
|
---|
| 116 |
|
---|
[63839f] | 117 | RandomNumberGenerator& RandomNumberGeneratorFactory::makeRandomNumberGenerator() const
|
---|
[3f9eba] | 118 | {
|
---|
| 119 | // Instantiate and return (implicitly creates a copy of the stored prototype)
|
---|
[9964ff] | 120 | RandomNumberEngineFactory::TypeList eng_type =
|
---|
| 121 | RandomNumberEngineFactory::getInstance().getCurrentTypeEnum();
|
---|
| 122 | RandomNumberDistributionFactory::TypeList dis_type =
|
---|
| 123 | RandomNumberDistributionFactory::getInstance().getCurrentTypeEnum();
|
---|
| 124 |
|
---|
[63839f] | 125 | return (*GeneratorPrototypeTable[ eng_type ][ dis_type ]);
|
---|
[3f9eba] | 126 | }
|
---|
| 127 |
|
---|
[63839f] | 128 | RandomNumberGenerator& RandomNumberGeneratorFactory::makeRandomNumberGenerator(
|
---|
[c9bc2b7] | 129 | std::string engine_type,
|
---|
| 130 | std::string distribution_type
|
---|
| 131 | ) const
|
---|
[3f9eba] | 132 | {
|
---|
[1d5a871] | 133 | RandomNumberEngineFactory::TypeList eng_type;
|
---|
| 134 | RandomNumberDistributionFactory::TypeList dis_type;
|
---|
[3f9eba] | 135 |
|
---|
| 136 | // Instantiate and return (implicitly creates a copy of the stored prototype)
|
---|
| 137 | if (!engine_type.empty()) {
|
---|
[c9bc2b7] | 138 | eng_type = RandomNumberEngineFactory::getInstance().getEnum(engine_type);
|
---|
[3f9eba] | 139 | } else
|
---|
[9964ff] | 140 | eng_type = RandomNumberEngineFactory::getInstance().getCurrentTypeEnum();
|
---|
[3f9eba] | 141 | if (!distribution_type.empty()) {
|
---|
[c9bc2b7] | 142 | dis_type = RandomNumberDistributionFactory::getInstance().getEnum(distribution_type);
|
---|
[3f9eba] | 143 | } else
|
---|
[9964ff] | 144 | dis_type = RandomNumberDistributionFactory::getInstance().getCurrentTypeEnum();
|
---|
[63839f] | 145 | return (*GeneratorPrototypeTable[ eng_type ][ dis_type ]);
|
---|
[9964ff] | 146 |
|
---|
[63839f] | 147 | return (*GeneratorPrototypeTable[ eng_type ][ dis_type ]);
|
---|
[3f9eba] | 148 | }
|
---|
| 149 |
|
---|
| 150 | void RandomNumberGeneratorFactory::setEngine(std::string engine_type)
|
---|
| 151 | {
|
---|
[9964ff] | 152 | RandomNumberEngineFactory::getInstance().setCurrentType(engine_type);
|
---|
[3f9eba] | 153 | }
|
---|
| 154 |
|
---|
[c9bc2b7] | 155 | const std::string &RandomNumberGeneratorFactory::getEngineName() const
|
---|
[3f9eba] | 156 | {
|
---|
[9964ff] | 157 | return RandomNumberEngineFactory::getInstance().getCurrentTypeName();
|
---|
[3f9eba] | 158 | }
|
---|
| 159 |
|
---|
| 160 | void RandomNumberGeneratorFactory::setDistribution(std::string distribution_type)
|
---|
| 161 | {
|
---|
[9964ff] | 162 | RandomNumberDistributionFactory::getInstance().setCurrentType(distribution_type);
|
---|
[3f9eba] | 163 | }
|
---|
| 164 |
|
---|
[c9bc2b7] | 165 | const std::string &RandomNumberGeneratorFactory::getDistributionName() const
|
---|
[3f9eba] | 166 | {
|
---|
[9964ff] | 167 | return RandomNumberDistributionFactory::getInstance().getCurrentTypeName();
|
---|
[3f9eba] | 168 | }
|
---|
| 169 |
|
---|
| 170 | CONSTRUCT_SINGLETON(RandomNumberGeneratorFactory)
|
---|
| 171 |
|
---|
[1d5a871] | 172 | #include "RandomNumberGeneratorFactory.undef"
|
---|
[3f9eba] | 173 |
|
---|