|
solarpowerlog trunk
|
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.h), the license terms are: 00011 00012 You can redistribute it and/or modify it under the terms of the GNU Lesser 00013 General Public License (LGPL) as published by the Free Software Foundation; 00014 either 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 #ifndef ICOMMAND_H_ 00034 #define ICOMMAND_H_ 00035 00036 #ifdef HAVE_CONFIG_H 00037 #include "config.h" 00038 #endif 00039 00040 #include <string> 00041 #include <map> 00042 #include <stdexcept> 00043 00044 #include <boost/any.hpp> 00045 00046 #include "configuration/ILogger.h" 00047 00048 // Tokens for ICommands (general meanings) 00049 00051 #define ICMD_ERRNO "ICMD_ERRNO" 00052 00055 #define ICMD_ERRNO_STR "ICMD_ERRMSG" 00056 00057 class ICommandTarget; 00058 00069 class ICommand 00070 { 00071 public: 00072 ICommand(int command, ICommandTarget *target, std::map<std::string, 00073 boost::any> dat); 00074 00075 ICommand(int command, ICommandTarget *target); 00076 00077 virtual ~ICommand(); 00078 00079 void execute(); 00080 00081 int getCmd() const; 00082 00094 const boost::any findData(const std::string & key) const 00095 throw(std::invalid_argument); 00096 00097 00098 void setCmd(int cmd) 00099 { 00100 this->cmd = cmd; 00101 } 00102 00103 void setTrgt(ICommandTarget *trgt) 00104 { 00105 this->trgt = trgt; 00106 } 00107 00114 inline void RemoveData(const std::string & key) 00115 { 00116 dat.erase(key); 00117 } 00118 00125 void addData(const std::string &key, const boost::any &data) 00126 { 00127 if (dat.count(key)) { 00128 dat.erase(key); 00129 } 00130 dat.insert(std::pair<std::string, boost::any>(key, data)); 00131 } 00132 00133 // Merge data from other ICommand into this one. 00134 void mergeData(const ICommand &other); 00135 00136 void DumpData(ILogger &logger) const { 00137 std::map<std::string, boost::any>::const_iterator it; 00138 LOGDEBUG(logger,"dumping available data: "); 00139 for(it=dat.begin(); it != dat.end(); it++) { 00140 LOGDEBUG(logger, it->first); 00141 } 00142 LOGDEBUG(logger,"dumping done."); 00143 } 00144 00145 private: 00146 int cmd; 00147 ICommandTarget *trgt; 00148 00149 std::map<std::string, boost::any> dat; 00150 }; 00151 00152 #endif /* ICOMMAND_H_ */