| 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 | * RandomNumberEngineFactory.cpp
|
|---|
| 10 | *
|
|---|
| 11 | * Created on: Jan 03, 2011
|
|---|
| 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 <boost/preprocessor/facilities/empty.hpp>
|
|---|
| 27 | #include <boost/preprocessor/punctuation/paren.hpp>
|
|---|
| 28 |
|
|---|
| 29 | #include "TemplatePowerSetGenerator.hpp"
|
|---|
| 30 | #include "EmptyPrototypeTable.hpp"
|
|---|
| 31 |
|
|---|
| 32 | #include "RandomNumberEngine_Encapsulation.hpp"
|
|---|
| 33 |
|
|---|
| 34 | #include "RandomNumberEngineFactory.hpp"
|
|---|
| 35 | #include "RandomNumberEngineFactory.def"
|
|---|
| 36 |
|
|---|
| 37 | //enum RandomNumberEngineFactory::Engine RandomNumberEngineFactory::engine = (enum RandomNumberEngineFactory::Engine) 0;
|
|---|
| 38 | RandomNumberEngineFactory::EngineMap RandomNumberEngineFactory::engines;
|
|---|
| 39 | RandomNumberEngineFactory::EngineNamesMap RandomNumberEngineFactory::engineNames;
|
|---|
| 40 | RandomNumberEngineFactory::EngineTable RandomNumberEngineFactory::EnginePrototypeTable;
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 | RandomNumberEngineFactory::RandomNumberEngineFactory()
|
|---|
| 44 | {
|
|---|
| 45 | FillEnumTable();
|
|---|
| 46 | FillPrototypeTable();
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 | RandomNumberEngineFactory::~RandomNumberEngineFactory()
|
|---|
| 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 | engines.clear();
|
|---|
| 55 | engineNames.clear();
|
|---|
| 56 | EmptyPrototypeTable<EngineTable> (EnginePrototypeTable);
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | void RandomNumberEngineFactory::FillEnumTable()
|
|---|
| 60 | {
|
|---|
| 61 | // insert all known engines
|
|---|
| 62 | #define BOOST_PP_LOCAL_MACRO(n) seqitems_as_string_enum_map(~, n, engine_seq, engines)
|
|---|
| 63 | #define BOOST_PP_LOCAL_LIMITS (0, BOOST_PP_SEQ_SIZE(engine_seq)-1 )
|
|---|
| 64 | #include BOOST_PP_LOCAL_ITERATE()
|
|---|
| 65 | #define BOOST_PP_LOCAL_MACRO(n) seqitems_as_string_enum_map(~, n, engine_seq_a, engines)
|
|---|
| 66 | #define BOOST_PP_LOCAL_LIMITS (0, BOOST_PP_SEQ_SIZE(engine_seq_a)-1 )
|
|---|
| 67 | #include BOOST_PP_LOCAL_ITERATE()
|
|---|
| 68 | for (EngineMap::const_iterator iter = engines.begin();
|
|---|
| 69 | iter != engines.end();
|
|---|
| 70 | ++iter) {
|
|---|
| 71 | engineNames.insert(make_pair(iter->second, iter->first));
|
|---|
| 72 | }
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | void RandomNumberEngineFactory::FillPrototypeTable()
|
|---|
| 76 | {
|
|---|
| 77 | // fill EnginePrototypeTable
|
|---|
| 78 | #define BOOST_PP_LOCAL_MACRO(n) seqitems_as_enum_key_map(~, n, engine_seq, EnginePrototypeTable, new RandomNumberEngine_Creator< RandomNumberEngine_Encapsulation , boost::, >)
|
|---|
| 79 | #define BOOST_PP_LOCAL_LIMITS (0, BOOST_PP_SEQ_SIZE(engine_seq)-1 )
|
|---|
| 80 | #include BOOST_PP_LOCAL_ITERATE()
|
|---|
| 81 | #define BOOST_PP_LOCAL_MACRO(n) seqitems_as_enum_key_map(~, n, engine_seq_a, EnginePrototypeTable, new RandomNumberEngine_Creator< RandomNumberEngine_Encapsulation , boost::, >)
|
|---|
| 82 | #define BOOST_PP_LOCAL_LIMITS (0, BOOST_PP_SEQ_SIZE(engine_seq_a)-1 )
|
|---|
| 83 | #include BOOST_PP_LOCAL_ITERATE()
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | RandomNumberEngine* RandomNumberEngineFactory::getEngine(enum Engine engine_type) const
|
|---|
| 87 | {
|
|---|
| 88 | return EnginePrototypeTable[engine_type]->create();
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | RandomNumberEngine* RandomNumberEngineFactory::getEngine(const std::string engine_name) const
|
|---|
| 92 | {
|
|---|
| 93 | ASSERT(engines.count(engine_name) != 0,
|
|---|
| 94 | "RandomNumberEngineFactory::getEngine() - engine "+engine_name+" is not registered.");
|
|---|
| 95 | return EnginePrototypeTable[ engines[engine_name] ]->create();
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | const std::string &RandomNumberEngineFactory::getName(enum Engine engine_type) const
|
|---|
| 99 | {
|
|---|
| 100 | return engineNames[engine_type];
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | enum RandomNumberEngineFactory::Engine RandomNumberEngineFactory::getEnum(const std::string engine_name) const
|
|---|
| 104 | {
|
|---|
| 105 | ASSERT(engines.count(engine_name) != 0,
|
|---|
| 106 | "RandomNumberEngineFactory::getEnum() - engine "+engine_name+" is not registered.");
|
|---|
| 107 | return engines[engine_name];
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | CONSTRUCT_SINGLETON(RandomNumberEngineFactory)
|
|---|
| 111 |
|
|---|
| 112 | #include "RandomNumberEngineFactory.undef"
|
|---|
| 113 |
|
|---|