solarpowerlog trunk
/home/tobi/workspace/solarpowerlog/src/patterns/ICommand.cpp
Go to the documentation of this file.
00001 /* ----------------------------------------------------------------------------
00002  solarpowerlog
00003  Copyright (C) 2009  Tobias Frost
00004 
00005  This file is part of solarpowerlog.
00006 
00007  Solarpowerlog is free software; However, it is dual-licenced
00008  as described in the file "COPYING".
00009 
00010  For this file (ICommand.cpp), the license terms are:
00011 
00012  You can redistribute it and/or modify it under the terms of the GNU
00013  General Public License as published by the Free Software Foundation; either
00014  version 3 of the License, or (at your option) any later version.
00015 
00016  This program is distributed in the hope that it will be useful, but
00017  WITHOUT ANY WARRANTY; without even the implied warranty of
00018  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019  Lesser General Public License for more details.
00020 
00021  You should have received a copy of the GNU Library General Public
00022  License along with this proramm; if not, see
00023  <http://www.gnu.org/licenses/>.
00024  ----------------------------------------------------------------------------
00025  */
00026 
00033 #ifdef HAVE_CONFIG_H
00034 #include "config.h"
00035 #endif
00036 
00037 #include "patterns/ICommand.h"
00038 #include "patterns/ICommandTarget.h"
00039 
00040 ICommand::ICommand(int command, ICommandTarget *target, std::map<std::string,
00041           boost::any> dat)
00042 {
00043 
00044      this->cmd = command;
00045      this->trgt = target;
00046      this->dat = dat;
00047 }
00048 
00049 ICommand::ICommand(int command, ICommandTarget *target)
00050 {
00051      cmd = command;
00052      trgt = target;
00053 }
00054 
00056 ICommand::~ICommand()
00057 {
00058 }
00059 
00061 void ICommand::execute()
00062 {
00063      assert(trgt);
00064      trgt->ExecuteCommand(this);
00065 }
00066 
00068 int ICommand::getCmd() const
00069 {
00070      return cmd;
00071 }
00072 
00073 const boost::any ICommand::findData(const std::string &key) const
00074           throw(std::invalid_argument)
00075 {
00076 
00077      std::map<std::string, boost::any>::const_iterator it = dat.find(key);
00078      if (it != dat.end()) {
00079           return (*it).second;
00080      }
00081      throw(std::invalid_argument(key));
00082 }
00083 
00084 void ICommand::mergeData(const ICommand &other)
00085 {
00086      // first delete all duplicate data
00087      std::map<std::string, boost::any>::const_iterator it;
00088      for(it = other.dat.begin(); it != other.dat.end(); it++)
00089      {
00090           if(dat.count(it->first)) dat.erase(it->first);
00091      }
00092 
00093      // and then merge the others data into the map
00094      dat.insert(other.dat.begin(), other.dat.end());
00095 }