| [bcf653] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
| [0aa122] | 4 |  * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 | 
|---|
| [5aaa43] | 5 |  * Copyright (C)  2013 Frederik Heber. All rights reserved.
 | 
|---|
| [94d5ac6] | 6 |  * 
 | 
|---|
 | 7 |  *
 | 
|---|
 | 8 |  *   This file is part of MoleCuilder.
 | 
|---|
 | 9 |  *
 | 
|---|
 | 10 |  *    MoleCuilder is free software: you can redistribute it and/or modify
 | 
|---|
 | 11 |  *    it under the terms of the GNU General Public License as published by
 | 
|---|
 | 12 |  *    the Free Software Foundation, either version 2 of the License, or
 | 
|---|
 | 13 |  *    (at your option) any later version.
 | 
|---|
 | 14 |  *
 | 
|---|
 | 15 |  *    MoleCuilder is distributed in the hope that it will be useful,
 | 
|---|
 | 16 |  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
 | 17 |  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
 | 18 |  *    GNU General Public License for more details.
 | 
|---|
 | 19 |  *
 | 
|---|
 | 20 |  *    You should have received a copy of the GNU General Public License
 | 
|---|
 | 21 |  *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>.
 | 
|---|
| [bcf653] | 22 |  */
 | 
|---|
 | 23 | 
 | 
|---|
| [cc04b7] | 24 | /*
 | 
|---|
| [b2d8d0] | 25 |  * Registry<Action>.cpp
 | 
|---|
| [cc04b7] | 26 |  *
 | 
|---|
 | 27 |  *  Created on: Jan 7, 2010
 | 
|---|
 | 28 |  *      Author: crueger
 | 
|---|
 | 29 |  */
 | 
|---|
 | 30 | 
 | 
|---|
| [bf3817] | 31 | // include config.h
 | 
|---|
 | 32 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 33 | #include <config.h>
 | 
|---|
 | 34 | #endif
 | 
|---|
 | 35 | 
 | 
|---|
| [ad011c] | 36 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| [112b09] | 37 | 
 | 
|---|
| [22b786] | 38 | #include "CodePatterns/Assert.hpp"
 | 
|---|
 | 39 | 
 | 
|---|
| [cc04b7] | 40 | #include "Actions/ActionRegistry.hpp"
 | 
|---|
| [ad011c] | 41 | #include "CodePatterns/Singleton_impl.hpp"
 | 
|---|
 | 42 | #include "CodePatterns/Registry_impl.hpp"
 | 
|---|
| [cc04b7] | 43 | 
 | 
|---|
| [83e90c] | 44 | #include "GlobalListOfActions.hpp"
 | 
|---|
 | 45 | 
 | 
|---|
 | 46 | #include "AllActionHeaders.hpp"
 | 
|---|
 | 47 | 
 | 
|---|
 | 48 | #include <boost/preprocessor/iteration/local.hpp>
 | 
|---|
 | 49 | #include <boost/preprocessor/seq/elem.hpp>
 | 
|---|
 | 50 | #include <boost/preprocessor/seq/size.hpp>
 | 
|---|
 | 51 | 
 | 
|---|
| [ce7fdc] | 52 | using namespace MoleCuilder;
 | 
|---|
 | 53 | 
 | 
|---|
| [2aad5a] | 54 | // static entities
 | 
|---|
 | 55 | bool ActionRegistry::completely_instatiated = false;
 | 
|---|
 | 56 | 
 | 
|---|
| [d0fbec] | 57 | /** Constructor for class ActionRegistry.
 | 
|---|
 | 58 |  */
 | 
|---|
| [cc04b7] | 59 | ActionRegistry::ActionRegistry()
 | 
|---|
| [83e90c] | 60 | {
 | 
|---|
 | 61 |   //std::cout << "ActionRegistry::ActionRegistry() called, instance is " << this << "." << std::endl;
 | 
|---|
 | 62 |   fillRegistry();
 | 
|---|
| [2aad5a] | 63 |   completely_instatiated = true;
 | 
|---|
| [83e90c] | 64 | }
 | 
|---|
| [cc04b7] | 65 | 
 | 
|---|
| [d0fbec] | 66 | /** Destructor for class ActionRegistry.
 | 
|---|
 | 67 |  */
 | 
|---|
| [cc04b7] | 68 | ActionRegistry::~ActionRegistry()
 | 
|---|
| [95c0ab] | 69 | {
 | 
|---|
| [22b786] | 70 |   // first clear each macro action that require presence of all primitive ones
 | 
|---|
| [39986b] | 71 |   {
 | 
|---|
 | 72 |     MakroAction * presentAction =
 | 
|---|
 | 73 |         dynamic_cast<MakroAction *>(getActionByName("subgraph-dissection"));
 | 
|---|
 | 74 |     ASSERT( presentAction != NULL,
 | 
|---|
 | 75 |         "ActionRegistry::fillRegistry() - makro action has not been registered.");
 | 
|---|
 | 76 |     presentAction->unprepare(*this);
 | 
|---|
 | 77 |   }
 | 
|---|
 | 78 | #ifdef HAVE_JOBMARKET
 | 
|---|
 | 79 |   {
 | 
|---|
 | 80 |     MakroAction * presentAction =
 | 
|---|
 | 81 |         dynamic_cast<MakroAction *>(getActionByName("molecular-dynamics"));
 | 
|---|
 | 82 |     ASSERT( presentAction != NULL,
 | 
|---|
 | 83 |         "ActionRegistry::fillRegistry() - makro action has not been registered.");
 | 
|---|
 | 84 |     presentAction->unprepare(*this);
 | 
|---|
 | 85 |   }
 | 
|---|
 | 86 | #endif
 | 
|---|
| [22b786] | 87 | 
 | 
|---|
| [83e90c] | 88 |   //std::cout << "ActionRegistry::~ActionRegistry() called, instance is " << this << "." << std::endl;
 | 
|---|
| [95c0ab] | 89 |   cleanup();
 | 
|---|
 | 90 | }
 | 
|---|
| [cc04b7] | 91 | 
 | 
|---|
| [83e90c] | 92 | 
 | 
|---|
 | 93 | /** Instantiates each existing Action.
 | 
|---|
 | 94 |  *
 | 
|---|
 | 95 |  */
 | 
|---|
 | 96 | void ActionRegistry::fillRegistry()
 | 
|---|
 | 97 | {
 | 
|---|
 | 98 | #define instance_print(z,n,list) \
 | 
|---|
 | 99 |         { \
 | 
|---|
 | 100 |         Action * presentAction = new \
 | 
|---|
 | 101 |         BOOST_PP_CAT(BOOST_PP_SEQ_ELEM(n, list), Action) \
 | 
|---|
 | 102 |         (); \
 | 
|---|
 | 103 |         registerInstance(presentAction); \
 | 
|---|
 | 104 |         }
 | 
|---|
 | 105 | #define BOOST_PP_LOCAL_MACRO(n) instance_print(~, n, GLOBALLISTOFACTIONS)
 | 
|---|
 | 106 | #define BOOST_PP_LOCAL_LIMITS  (0, BOOST_PP_DEC(BOOST_PP_SEQ_SIZE(GLOBALLISTOFACTIONS)))
 | 
|---|
 | 107 | #include BOOST_PP_LOCAL_ITERATE()
 | 
|---|
 | 108 | #undef instance_print  
 | 
|---|
| [22b786] | 109 | 
 | 
|---|
 | 110 |   // now prepare each macro action that require presence of all primitive ones
 | 
|---|
| [39986b] | 111 |   {
 | 
|---|
 | 112 |     MakroAction * presentAction =
 | 
|---|
 | 113 |         dynamic_cast<MakroAction *>(getActionByName("subgraph-dissection"));
 | 
|---|
 | 114 |     ASSERT( presentAction != NULL,
 | 
|---|
 | 115 |         "ActionRegistry::fillRegistry() - makro action has not been registered.");
 | 
|---|
 | 116 |     presentAction->prepare(*this);
 | 
|---|
 | 117 |   }
 | 
|---|
 | 118 | #ifdef HAVE_JOBMARKET
 | 
|---|
 | 119 |   {
 | 
|---|
 | 120 |     MakroAction * presentAction =
 | 
|---|
 | 121 |         dynamic_cast<MakroAction *>(getActionByName("molecular-dynamics"));
 | 
|---|
 | 122 |     ASSERT( presentAction != NULL,
 | 
|---|
 | 123 |         "ActionRegistry::fillRegistry() - makro action has not been registered.");
 | 
|---|
 | 124 |     presentAction->prepare(*this);
 | 
|---|
 | 125 |   }
 | 
|---|
 | 126 | #endif
 | 
|---|
| [83e90c] | 127 | }
 | 
|---|
 | 128 | 
 | 
|---|
| [b2d8d0] | 129 | /** Just passes on call to Registry<Action>::getByName().
 | 
|---|
 | 130 |  * \param name name of Action
 | 
|---|
| [d0fbec] | 131 |  * \return pointer to Action
 | 
|---|
 | 132 |  */
 | 
|---|
| [b2d8d0] | 133 | Action* ActionRegistry::getActionByName(const std::string name)
 | 
|---|
| [d0fbec] | 134 | {
 | 
|---|
| [b2d8d0] | 135 |   return getByName(name);
 | 
|---|
| [d0fbec] | 136 | }
 | 
|---|
 | 137 | 
 | 
|---|
| [b2d8d0] | 138 | /** Just passes on call to Registry<Action>::isPresentByName().
 | 
|---|
 | 139 |  * \param name name of Action
 | 
|---|
 | 140 |  * \return true - Action instance present, false - not
 | 
|---|
| [d0fbec] | 141 |  */
 | 
|---|
| [e4afb4] | 142 | bool ActionRegistry::isActionPresentByName(const std::string name) const
 | 
|---|
| [d0fbec] | 143 | {
 | 
|---|
| [b2d8d0] | 144 |   return isPresentByName(name);
 | 
|---|
| [d0fbec] | 145 | }
 | 
|---|
 | 146 | 
 | 
|---|
| [b59da6] | 147 | /** Returns the last present action position in the requested menu.
 | 
|---|
 | 148 |  * \param &token token of the menu
 | 
|---|
 | 149 |  * \return last used position
 | 
|---|
 | 150 |  */
 | 
|---|
 | 151 | int ActionRegistry::getLastPosition(const std::string &token) const
 | 
|---|
 | 152 | {
 | 
|---|
 | 153 |   int position = 0;
 | 
|---|
 | 154 |   for (const_iterator iter = getBeginIter();
 | 
|---|
 | 155 |       iter != getEndIter();
 | 
|---|
 | 156 |       ++iter) {
 | 
|---|
 | 157 |     const std::string &MenuName = (iter->second)->Traits.getMenuName();
 | 
|---|
 | 158 |     const int &MenuPosition = (iter->second)->Traits.getMenuPosition();
 | 
|---|
 | 159 |     if ((MenuName == token) && (position < MenuPosition))
 | 
|---|
 | 160 |       position = MenuPosition;
 | 
|---|
 | 161 |   }
 | 
|---|
 | 162 |   return position;
 | 
|---|
 | 163 | }
 | 
|---|
 | 164 | 
 | 
|---|
 | 165 | 
 | 
|---|
| [e73a8a2] | 166 | CONSTRUCT_SINGLETON(ActionRegistry)
 | 
|---|
| [b2d8d0] | 167 | CONSTRUCT_REGISTRY(Action)
 | 
|---|