| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2010-2011 University of Bonn. All rights reserved.
 | 
|---|
| 5 |  * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | /*
 | 
|---|
| 9 |  * ActionTrait.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/ActionTrait.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 ActionTrait.
 | 
|---|
| 34 |  * \param &_Traits source ActionTrait class to copy
 | 
|---|
| 35 |  */
 | 
|---|
| 36 | ActionTrait::ActionTrait(const std::string &_token) :
 | 
|---|
| 37 |   OptionTrait(_token,
 | 
|---|
| 38 |       &typeid(void),
 | 
|---|
| 39 |       "this is an invisible action",
 | 
|---|
| 40 |       "",
 | 
|---|
| 41 |       ""
 | 
|---|
| 42 |   )
 | 
|---|
| 43 | {
 | 
|---|
| 44 |   //std::cout << "ActionTrait::ActionTrait(string &) on instance " << this << " with " << getName() << ", type " << getTypeName() << " and description " << getDescription() << std::endl;
 | 
|---|
| 45 | }
 | 
|---|
| 46 | 
 | 
|---|
| 47 | /** Copy constructor for base class ActionTrait.
 | 
|---|
| 48 |  * we have to make our own copy in order to avoid references and deep-copy everything.
 | 
|---|
| 49 |  * \param &_Traits source ActionTrait class to copy
 | 
|---|
| 50 |  */
 | 
|---|
| 51 | ActionTrait::ActionTrait(const ActionTrait &_Traits) :
 | 
|---|
| 52 |   OptionTrait(_Traits),
 | 
|---|
| 53 |   MenuTitle(_Traits.MenuTitle),
 | 
|---|
| 54 |   MenuPosition(_Traits.MenuPosition)
 | 
|---|
| 55 | {
 | 
|---|
| 56 |   for (options_const_iterator iter = _Traits.Options.begin(); iter != _Traits.Options.end(); ++iter) {
 | 
|---|
| 57 |     Options.insert(
 | 
|---|
| 58 |         std::make_pair(
 | 
|---|
| 59 |             iter->first,
 | 
|---|
| 60 |             new OptionTrait(*iter->second)
 | 
|---|
| 61 |         )
 | 
|---|
| 62 |     );
 | 
|---|
| 63 |   }
 | 
|---|
| 64 |   //std::cout << "ActionTrait::ActionTrait(ActionTrait &) on instance " << this << " with " << getName() << ", type " << getTypeName() << " and description " << getDescription() << std::endl;
 | 
|---|
| 65 | }
 | 
|---|
| 66 | 
 | 
|---|
| 67 | /** Copy constructor for base class ActionTrait.
 | 
|---|
| 68 |  * we have to make our own copy in order to avoid references and deep-copy everything.
 | 
|---|
| 69 |  * \param &_Traits source OptionTrait class to copy
 | 
|---|
| 70 |  */
 | 
|---|
| 71 | ActionTrait::ActionTrait(const OptionTrait &_Traits, const std::string _MenuTitle, const int _MenuPosition) :
 | 
|---|
| 72 |   OptionTrait(_Traits),
 | 
|---|
| 73 |   MenuTitle(_MenuTitle),
 | 
|---|
| 74 |   MenuPosition(_MenuPosition)
 | 
|---|
| 75 | {
 | 
|---|
| 76 |   //std::cout << "ActionTrait::ActionTrait(OptionTrait &) on instance " << this << " with " << getName() << ", type " << getTypeName() << " and description " << getDescription() << std::endl;
 | 
|---|
| 77 | }
 | 
|---|
| 78 | 
 | 
|---|
| 79 | /** Constructor for base class ActionTrait.
 | 
|---|
| 80 |  * Deletes all present Options.
 | 
|---|
| 81 |  */
 | 
|---|
| 82 | ActionTrait::~ActionTrait()
 | 
|---|
| 83 | {
 | 
|---|
| 84 |   //std::cout << "ActionTrait::~ActionTrait() on instance " << this << " with name " << getName() << " called." << std::endl;
 | 
|---|
| 85 |   for (options_iterator iter = Options.begin(); iter != Options.end(); ++iter) {
 | 
|---|
| 86 |     //std::cout << "ActionTrait::~ActionTrait() removes option instance " << iter->second << " with name " << iter->first << "." << std::endl;
 | 
|---|
| 87 |     delete iter->second;
 | 
|---|
| 88 |   }
 | 
|---|
| 89 |   Options.clear();
 | 
|---|
| 90 | }
 | 
|---|
| 91 | 
 | 
|---|
| 92 | 
 | 
|---|
| 93 | /** Returns menu title for this ActionTrait.
 | 
|---|
| 94 |  * \return ActionTrait::MenuTitle as std::string
 | 
|---|
| 95 |  */
 | 
|---|
| 96 | const std::string& ActionTrait::getMenuName() const
 | 
|---|
| 97 | {
 | 
|---|
| 98 |   return MenuTitle;
 | 
|---|
| 99 | }
 | 
|---|
| 100 | 
 | 
|---|
| 101 | /** Returns menu title for this ActionTrait.
 | 
|---|
| 102 |  * \return ActionTrait::MenuPosition as std::string
 | 
|---|
| 103 |  */
 | 
|---|
| 104 | int ActionTrait::getMenuPosition() const
 | 
|---|
| 105 | {
 | 
|---|
| 106 |   return MenuPosition;
 | 
|---|
| 107 | }
 | 
|---|
| 108 | 
 | 
|---|
| 109 | /** Returns Description for the given option of this ActionTrait.
 | 
|---|
| 110 |  * \param &token token of option
 | 
|---|
| 111 |  * \return reference to OptionTrait
 | 
|---|
| 112 |  */
 | 
|---|
| 113 | OptionTrait const & ActionTrait::getOption(const std::string &token) const
 | 
|---|
| 114 | {
 | 
|---|
| 115 |   ASSERT(Options.find(token) != Options.end(),
 | 
|---|
| 116 |       "ActionTrait::getOption() - Option not found!");
 | 
|---|
| 117 |   return *(Options.find(token)->second);
 | 
|---|
| 118 | }
 | 
|---|
| 119 | 
 | 
|---|
| 120 | /** States whether given option (token) is present or not.
 | 
|---|
| 121 |  * \param &token name of option
 | 
|---|
| 122 |  * \return true - option present, false - not
 | 
|---|
| 123 |  */
 | 
|---|
| 124 | bool ActionTrait::hasOption(const std::string &token) const
 | 
|---|
| 125 | {
 | 
|---|
| 126 |   return (Options.find(token) != Options.end());
 | 
|---|
| 127 | }
 | 
|---|
| 128 | 
 | 
|---|
| 129 | /** States whether this Action has options at all.
 | 
|---|
| 130 |  * \return true - options present, false - no options
 | 
|---|
| 131 |  */
 | 
|---|
| 132 | bool ActionTrait::hasOptions() const
 | 
|---|
| 133 | {
 | 
|---|
| 134 |   return (Options.begin() != Options.end());
 | 
|---|
| 135 | }
 | 
|---|
| 136 | 
 | 
|---|
| 137 | /** Forward iterator from beginning of list of options.
 | 
|---|
| 138 |  * \return iterator
 | 
|---|
| 139 |  */
 | 
|---|
| 140 | ActionTrait::options_iterator ActionTrait::getBeginIter()
 | 
|---|
| 141 | {
 | 
|---|
| 142 |   return Options.begin();
 | 
|---|
| 143 | }
 | 
|---|
| 144 | 
 | 
|---|
| 145 | /** Forward iterator at end of list of options.
 | 
|---|
| 146 |  * \return iterator
 | 
|---|
| 147 |  */
 | 
|---|
| 148 | ActionTrait::options_iterator ActionTrait::getEndIter()
 | 
|---|
| 149 | {
 | 
|---|
| 150 |   return Options.end();
 | 
|---|
| 151 | }
 | 
|---|
| 152 | 
 | 
|---|
| 153 | /** Constant forward iterator from beginning of list of options.
 | 
|---|
| 154 |  * \return constant iterator
 | 
|---|
| 155 |  */
 | 
|---|
| 156 | ActionTrait::options_const_iterator ActionTrait::getBeginIter() const
 | 
|---|
| 157 | {
 | 
|---|
| 158 |   return Options.begin();
 | 
|---|
| 159 | }
 | 
|---|
| 160 | 
 | 
|---|
| 161 | /** Constant forward iterator at end of list of options.
 | 
|---|
| 162 |  * \return constant iterator
 | 
|---|
| 163 |  */
 | 
|---|
| 164 | ActionTrait::options_const_iterator ActionTrait::getEndIter() const
 | 
|---|
| 165 | {
 | 
|---|
| 166 |   return Options.end();
 | 
|---|
| 167 | }
 | 
|---|
| 168 | 
 | 
|---|
| 169 | /** Output operator for ActionTrait.
 | 
|---|
| 170 |  *
 | 
|---|
| 171 |  * \param &out stream to output to
 | 
|---|
| 172 |  */
 | 
|---|
| 173 | std::ostream &operator<<(std::ostream &out, const ActionTrait &a)
 | 
|---|
| 174 | {
 | 
|---|
| 175 |   out << "ActionTrait(" << &a << ") with " << static_cast<const OptionTrait &>(a) << " has the following options: " << std::endl;
 | 
|---|
| 176 |   for (ActionTrait::options_const_iterator iter = a.getBeginIter();
 | 
|---|
| 177 |     iter != a.getEndIter();
 | 
|---|
| 178 |     ++iter) {
 | 
|---|
| 179 |     out << "\t - " << *iter->second << ")" << std::endl;
 | 
|---|
| 180 |   }
 | 
|---|
| 181 |   return out;
 | 
|---|
| 182 | }
 | 
|---|
| 183 | 
 | 
|---|