| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 | 
|---|
| 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/>.
 | 
|---|
| 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 | 
 | 
|---|
| 35 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| 36 | 
 | 
|---|
| 37 | #include <utility>
 | 
|---|
| 38 | #include "CodePatterns/Singleton_impl.hpp"
 | 
|---|
| 39 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| 40 | 
 | 
|---|
| 41 | #include "TemplatePowerSetGenerator.hpp"
 | 
|---|
| 42 | #include "EmptyPrototypeTable.hpp"
 | 
|---|
| 43 | 
 | 
|---|
| 44 | #include <boost/preprocessor/facilities/empty.hpp>
 | 
|---|
| 45 | #include <boost/preprocessor/punctuation/paren.hpp>
 | 
|---|
| 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 | 
 | 
|---|
| 51 | #include "RandomNumberGenerator_Encapsulation.hpp"
 | 
|---|
| 52 | 
 | 
|---|
| 53 | #include "RandomNumberGeneratorFactory.hpp"
 | 
|---|
| 54 | #include "RandomNumberGeneratorFactory.def"
 | 
|---|
| 55 | 
 | 
|---|
| 56 | RandomNumberDistributionFactory::TypeList RandomNumberGeneratorFactory::distribution = (RandomNumberDistributionFactory::TypeList) 0;
 | 
|---|
| 57 | RandomNumberEngineFactory::TypeList RandomNumberGeneratorFactory::engine = (RandomNumberEngineFactory::TypeList) 0;
 | 
|---|
| 58 | RandomNumberGeneratorFactory::EngineDistributionTable RandomNumberGeneratorFactory::GeneratorPrototypeTable;
 | 
|---|
| 59 | 
 | 
|---|
| 60 | RandomNumberGeneratorFactory::RandomNumberGeneratorFactory()
 | 
|---|
| 61 | {
 | 
|---|
| 62 |         FillPrototypeTable();
 | 
|---|
| 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()) {
 | 
|---|
| 72 |     EmptyPrototypeTable< std::map<RandomNumberDistributionFactory::TypeList,RandomNumberGenerator*> > (iter->second);
 | 
|---|
| 73 |     GeneratorPrototypeTable.erase(iter);
 | 
|---|
| 74 |   }
 | 
|---|
| 75 |   GeneratorPrototypeTable.clear();
 | 
|---|
| 76 | }
 | 
|---|
| 77 | 
 | 
|---|
| 78 | void RandomNumberGeneratorFactory::FillPrototypeTable()
 | 
|---|
| 79 | {
 | 
|---|
| 80 |   // fill GeneratorPrototypeTable
 | 
|---|
| 81 | #define SequenceElementizer(z,data) (data)
 | 
|---|
| 82 | 
 | 
|---|
| 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))
 | 
|---|
| 87 | 
 | 
|---|
| 88 | #define suffixseq ()(<>)
 | 
|---|
| 89 | #define tableseq (*EnginePrototypeTable)(*DistributionPrototypeTable)
 | 
|---|
| 90 | #define name_spaces_seq (RandomNumberEngineFactory::)(RandomNumberDistributionFactory::)
 | 
|---|
| 91 | 
 | 
|---|
| 92 | #define size_tupels BOOST_PP_SEQ_SIZE(tableseq)
 | 
|---|
| 93 | 
 | 
|---|
| 94 | #define TableItemPrinter(z,n,sequence) \
 | 
|---|
| 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) \
 | 
|---|
| 101 |   BOOST_PP_COMMA_IF(n) \
 | 
|---|
| 102 |   boost:: \
 | 
|---|
| 103 |   BOOST_PP_SEQ_ELEM(n,sequence) \
 | 
|---|
| 104 |   BOOST_PP_SEQ_ELEM(n,suffixseq)
 | 
|---|
| 105 | 
 | 
|---|
| 106 | #define ParamItemPrinter(z,n,sequence)
 | 
|---|
| 107 | 
 | 
|---|
| 108 | #define BOOST_PP_LOCAL_MACRO(n) seqitems_as_enum_key_multidimmap(~, n, size_tupels, distributionengine_seqseq, GeneratorPrototypeTable, TableItemPrinter, new RandomNumberGenerator_Encapsulation , TemplateItemPrinter, ParamItemPrinter)
 | 
|---|
| 109 | #define BOOST_PP_LOCAL_LIMITS  (0, BOOST_PP_SEQ_SIZE(distributionengine_seqseq)-1 )
 | 
|---|
| 110 | #include BOOST_PP_LOCAL_ITERATE()
 | 
|---|
| 111 | 
 | 
|---|
| 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)
 | 
|---|
| 113 | #define BOOST_PP_LOCAL_LIMITS  (0, BOOST_PP_SEQ_SIZE(distributionengine_seqseq_a)-1 )
 | 
|---|
| 114 | #include BOOST_PP_LOCAL_ITERATE()
 | 
|---|
| 115 | }
 | 
|---|
| 116 | 
 | 
|---|
| 117 | RandomNumberGenerator& RandomNumberGeneratorFactory::makeRandomNumberGenerator() const
 | 
|---|
| 118 | {
 | 
|---|
| 119 |   // Instantiate and return (implicitly creates a copy of the stored prototype)
 | 
|---|
| 120 |   RandomNumberEngineFactory::TypeList eng_type =
 | 
|---|
| 121 |       RandomNumberEngineFactory::getInstance().getCurrentTypeEnum();
 | 
|---|
| 122 |   RandomNumberDistributionFactory::TypeList dis_type =
 | 
|---|
| 123 |       RandomNumberDistributionFactory::getInstance().getCurrentTypeEnum();
 | 
|---|
| 124 | 
 | 
|---|
| 125 |   return (*GeneratorPrototypeTable[ eng_type ][ dis_type ]);
 | 
|---|
| 126 | }
 | 
|---|
| 127 | 
 | 
|---|
| 128 | RandomNumberGenerator& RandomNumberGeneratorFactory::makeRandomNumberGenerator(
 | 
|---|
| 129 |     std::string engine_type,
 | 
|---|
| 130 |     std::string distribution_type
 | 
|---|
| 131 |     ) const
 | 
|---|
| 132 | {
 | 
|---|
| 133 |   RandomNumberEngineFactory::TypeList eng_type;
 | 
|---|
| 134 |   RandomNumberDistributionFactory::TypeList dis_type;
 | 
|---|
| 135 | 
 | 
|---|
| 136 |   // Instantiate and return (implicitly creates a copy of the stored prototype)
 | 
|---|
| 137 |   if (!engine_type.empty()) {
 | 
|---|
| 138 |     eng_type = RandomNumberEngineFactory::getInstance().getEnum(engine_type);
 | 
|---|
| 139 |   } else
 | 
|---|
| 140 |     eng_type = RandomNumberEngineFactory::getInstance().getCurrentTypeEnum();
 | 
|---|
| 141 |   if (!distribution_type.empty()) {
 | 
|---|
| 142 |     dis_type = RandomNumberDistributionFactory::getInstance().getEnum(distribution_type);
 | 
|---|
| 143 |   } else
 | 
|---|
| 144 |     dis_type = RandomNumberDistributionFactory::getInstance().getCurrentTypeEnum();
 | 
|---|
| 145 |   return (*GeneratorPrototypeTable[ eng_type ][ dis_type ]);
 | 
|---|
| 146 | 
 | 
|---|
| 147 |   return (*GeneratorPrototypeTable[ eng_type ][ dis_type ]);
 | 
|---|
| 148 | }
 | 
|---|
| 149 | 
 | 
|---|
| 150 | void RandomNumberGeneratorFactory::setEngine(std::string engine_type)
 | 
|---|
| 151 | {
 | 
|---|
| 152 |   RandomNumberEngineFactory::getInstance().setCurrentType(engine_type);
 | 
|---|
| 153 | }
 | 
|---|
| 154 | 
 | 
|---|
| 155 | const std::string &RandomNumberGeneratorFactory::getEngineName() const
 | 
|---|
| 156 | {
 | 
|---|
| 157 |   return RandomNumberEngineFactory::getInstance().getCurrentTypeName();
 | 
|---|
| 158 | }
 | 
|---|
| 159 | 
 | 
|---|
| 160 | void RandomNumberGeneratorFactory::setDistribution(std::string distribution_type)
 | 
|---|
| 161 | {
 | 
|---|
| 162 |   RandomNumberDistributionFactory::getInstance().setCurrentType(distribution_type);
 | 
|---|
| 163 | }
 | 
|---|
| 164 | 
 | 
|---|
| 165 | const std::string &RandomNumberGeneratorFactory::getDistributionName() const
 | 
|---|
| 166 | {
 | 
|---|
| 167 |   return RandomNumberDistributionFactory::getInstance().getCurrentTypeName();
 | 
|---|
| 168 | }
 | 
|---|
| 169 | 
 | 
|---|
| 170 | CONSTRUCT_SINGLETON(RandomNumberGeneratorFactory)
 | 
|---|
| 171 | 
 | 
|---|
| 172 | #include "RandomNumberGeneratorFactory.undef"
 | 
|---|
| 173 | 
 | 
|---|