| 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 |  * ActionTraits.cpp
 | 
|---|
| 10 |  *
 | 
|---|
| 11 |  *  Created on: Oct 26, 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 "Actions/ActionTraits.hpp"
 | 
|---|
| 23 | 
 | 
|---|
| 24 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| 25 | 
 | 
|---|
| 26 | #include <iostream>
 | 
|---|
| 27 | #include <sstream>
 | 
|---|
| 28 | #include <string>
 | 
|---|
| 29 | #include <typeinfo>
 | 
|---|
| 30 | 
 | 
|---|
| 31 | using namespace MoleCuilder;
 | 
|---|
| 32 | 
 | 
|---|
| 33 | /** Copy constructor for base class ActionTraits.
 | 
|---|
| 34 |  * \param &_Traits source ActionTraits class to copy
 | 
|---|
| 35 |  */
 | 
|---|
| 36 | ActionTraits::ActionTraits(const std::string &_token) :
 | 
|---|
| 37 |   OptionTrait(_token,
 | 
|---|
| 38 |       &typeid(void),
 | 
|---|
| 39 |       "this is an invisible action",
 | 
|---|
| 40 |       "",
 | 
|---|
| 41 |       ""
 | 
|---|
| 42 |   )
 | 
|---|
| 43 | {
 | 
|---|
| 44 |   //std::cout << "ActionTraits::ActionTraits(string &) with " << getName() << ", type " << getTypeName() << " and description " << getDescription() << std::endl;
 | 
|---|
| 45 | }
 | 
|---|
| 46 | 
 | 
|---|
| 47 | /** Copy constructor for base class ActionTraits.
 | 
|---|
| 48 |  * we have to make our own copy in order to avoid references and deep-copy everything.
 | 
|---|
| 49 |  * \param &_Traits source ActionTraits class to copy
 | 
|---|
| 50 |  */
 | 
|---|
| 51 | ActionTraits::ActionTraits(const ActionTraits &_Traits) :
 | 
|---|
| 52 |   OptionTrait(_Traits.getName(),
 | 
|---|
| 53 |       _Traits.getType(),
 | 
|---|
| 54 |       _Traits.getDescription(),
 | 
|---|
| 55 |       _Traits.hasDefaultValue() ? _Traits.getDefaultValue() : "",
 | 
|---|
| 56 |       _Traits.hasShortForm() ? _Traits.getShortForm() : ""
 | 
|---|
| 57 |   ),
 | 
|---|
| 58 |   MenuTitle(_Traits.MenuTitle),
 | 
|---|
| 59 |   MenuPosition(_Traits.MenuPosition)
 | 
|---|
| 60 | {
 | 
|---|
| 61 |   for (options_const_iterator iter = _Traits.Options.begin(); iter != _Traits.Options.end(); ++iter) {
 | 
|---|
| 62 |     Options.insert(
 | 
|---|
| 63 |         std::pair< std::string, OptionTrait *> (
 | 
|---|
| 64 |             iter->first,
 | 
|---|
| 65 |             new OptionTrait(iter->second->getName(),iter->second->getType(),iter->second->getDescription(), iter->second->getDefaultValue(), iter->second->getShortForm())
 | 
|---|
| 66 |         )
 | 
|---|
| 67 |     );
 | 
|---|
| 68 |   }
 | 
|---|
| 69 |   //std::cout << "ActionTraits::ActionTraits(ActionTraits &) with " << getName() << ", type " << getTypeName() << " and description " << getDescription() << std::endl;
 | 
|---|
| 70 | }
 | 
|---|
| 71 | 
 | 
|---|
| 72 | /** Copy constructor for base class ActionTraits.
 | 
|---|
| 73 |  * we have to make our own copy in order to avoid references and deep-copy everything.
 | 
|---|
| 74 |  * \param &_Traits source OptionTrait class to copy
 | 
|---|
| 75 |  */
 | 
|---|
| 76 | ActionTraits::ActionTraits(const OptionTrait &_Traits, const std::string _MenuTitle, const int _MenuPosition) :
 | 
|---|
| 77 |   OptionTrait(_Traits),
 | 
|---|
| 78 |   MenuTitle(_MenuTitle),
 | 
|---|
| 79 |   MenuPosition(_MenuPosition)
 | 
|---|
| 80 | {
 | 
|---|
| 81 |   //std::cout << "ActionTraits::ActionTraits(OptionTrait &) with " << getName() << ", type " << getTypeName() << " and description " << getDescription() << std::endl;
 | 
|---|
| 82 | }
 | 
|---|
| 83 | 
 | 
|---|
| 84 | /** Constructor for base class ActionTraits.
 | 
|---|
| 85 |  * Deletes all present Options.
 | 
|---|
| 86 |  */
 | 
|---|
| 87 | ActionTraits::~ActionTraits()
 | 
|---|
| 88 | {
 | 
|---|
| 89 |   for (options_iterator iter = Options.begin(); !Options.empty(); iter = Options.begin()) {
 | 
|---|
| 90 |     delete (iter->second);
 | 
|---|
| 91 |     Options.erase(iter);
 | 
|---|
| 92 |   }
 | 
|---|
| 93 | }
 | 
|---|
| 94 | 
 | 
|---|
| 95 | 
 | 
|---|
| 96 | /** Returns menu title for this ActionTrait.
 | 
|---|
| 97 |  * \return ActionTraits::MenuTitle as std::string
 | 
|---|
| 98 |  */
 | 
|---|
| 99 | const std::string& ActionTraits::getMenuName() const
 | 
|---|
| 100 | {
 | 
|---|
| 101 |   return MenuTitle;
 | 
|---|
| 102 | }
 | 
|---|
| 103 | 
 | 
|---|
| 104 | /** Returns menu title for this ActionTrait.
 | 
|---|
| 105 |  * \return ActionTraits::MenuPosition as std::string
 | 
|---|
| 106 |  */
 | 
|---|
| 107 | int ActionTraits::getMenuPosition() const
 | 
|---|
| 108 | {
 | 
|---|
| 109 |   return MenuPosition;
 | 
|---|
| 110 | }
 | 
|---|
| 111 | 
 | 
|---|
| 112 | /** Returns Description for the given option of this ActionTrait.
 | 
|---|
| 113 |  * \param &token token of option
 | 
|---|
| 114 |  * \return reference to OptionTrait
 | 
|---|
| 115 |  */
 | 
|---|
| 116 | OptionTrait const & ActionTraits::getOption(const std::string &token) const
 | 
|---|
| 117 | {
 | 
|---|
| 118 |   ASSERT(Options.find(token) != Options.end(),
 | 
|---|
| 119 |       "ActionTraits::getOption() - Option not found!");
 | 
|---|
| 120 |   return *(Options.find(token)->second);
 | 
|---|
| 121 | }
 | 
|---|
| 122 | 
 | 
|---|
| 123 | /** States whether given option (token) is present or not.
 | 
|---|
| 124 |  * \param &token name of option
 | 
|---|
| 125 |  * \return true - option present, false - not
 | 
|---|
| 126 |  */
 | 
|---|
| 127 | bool ActionTraits::hasOption(const std::string &token) const
 | 
|---|
| 128 | {
 | 
|---|
| 129 |   return (Options.find(token) != Options.end());
 | 
|---|
| 130 | }
 | 
|---|
| 131 | 
 | 
|---|
| 132 | /** States whether this Action has options at all.
 | 
|---|
| 133 |  * \return true - options present, false - no options
 | 
|---|
| 134 |  */
 | 
|---|
| 135 | bool ActionTraits::hasOptions() const
 | 
|---|
| 136 | {
 | 
|---|
| 137 |   return (Options.begin() != Options.end());
 | 
|---|
| 138 | }
 | 
|---|
| 139 | 
 | 
|---|
| 140 | /** Forward iterator from beginning of list of options.
 | 
|---|
| 141 |  * \return iterator
 | 
|---|
| 142 |  */
 | 
|---|
| 143 | ActionTraits::options_iterator ActionTraits::getBeginIter()
 | 
|---|
| 144 | {
 | 
|---|
| 145 |   return Options.begin();
 | 
|---|
| 146 | }
 | 
|---|
| 147 | 
 | 
|---|
| 148 | /** Forward iterator at end of list of options.
 | 
|---|
| 149 |  * \return iterator
 | 
|---|
| 150 |  */
 | 
|---|
| 151 | ActionTraits::options_iterator ActionTraits::getEndIter()
 | 
|---|
| 152 | {
 | 
|---|
| 153 |   return Options.end();
 | 
|---|
| 154 | }
 | 
|---|
| 155 | 
 | 
|---|
| 156 | /** Constant forward iterator from beginning of list of options.
 | 
|---|
| 157 |  * \return constant iterator
 | 
|---|
| 158 |  */
 | 
|---|
| 159 | ActionTraits::options_const_iterator ActionTraits::getBeginIter() const
 | 
|---|
| 160 | {
 | 
|---|
| 161 |   return Options.begin();
 | 
|---|
| 162 | }
 | 
|---|
| 163 | 
 | 
|---|
| 164 | /** Constant forward iterator at end of list of options.
 | 
|---|
| 165 |  * \return constant iterator
 | 
|---|
| 166 |  */
 | 
|---|
| 167 | ActionTraits::options_const_iterator ActionTraits::getEndIter() const
 | 
|---|
| 168 | {
 | 
|---|
| 169 |   return Options.end();
 | 
|---|
| 170 | }
 | 
|---|
| 171 | 
 | 
|---|
| 172 | 
 | 
|---|