Command.hpp

00001 /***************************************************************************
00002   tag: FMTC  do nov 2 13:06:06 CET 2006  Command.hpp
00003 
00004                         Command.hpp -  description
00005                            -------------------
00006     begin                : do november 02 2006
00007     copyright            : (C) 2006 FMTC
00008     email                : peter.soetens@fmtc.be
00009 
00010  ***************************************************************************
00011  *   This library is free software; you can redistribute it and/or         *
00012  *   modify it under the terms of the GNU General Public                   *
00013  *   License as published by the Free Software Foundation;                 *
00014  *   version 2 of the License.                                             *
00015  *                                                                         *
00016  *   As a special exception, you may use this file as part of a free       *
00017  *   software library without restriction.  Specifically, if other files   *
00018  *   instantiate templates or use macros or inline functions from this     *
00019  *   file, or you compile this file and link it with other files to        *
00020  *   produce an executable, this file does not by itself cause the         *
00021  *   resulting executable to be covered by the GNU General Public          *
00022  *   License.  This exception does not however invalidate any other        *
00023  *   reasons why the executable file might be covered by the GNU General   *
00024  *   Public License.                                                       *
00025  *                                                                         *
00026  *   This library is distributed in the hope that it will be useful,       *
00027  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00028  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00029  *   Lesser General Public License for more details.                       *
00030  *                                                                         *
00031  *   You should have received a copy of the GNU General Public             *
00032  *   License along with this library; if not, write to the Free Software   *
00033  *   Foundation, Inc., 59 Temple Place,                                    *
00034  *   Suite 330, Boston, MA  02111-1307  USA                                *
00035  *                                                                         *
00036  ***************************************************************************/
00037 
00038 
00039 #ifndef ORO_TASK_COMMAND_HPP
00040 #define ORO_TASK_COMMAND_HPP
00041 
00042 #include <string>
00043 #include <boost/static_assert.hpp>
00044 #include <boost/type_traits/is_same.hpp>
00045 #include "CommandBase.hpp"
00046 #include "LocalCommand.hpp"
00047 #include "UnMember.hpp"
00048 
00049 namespace RTT
00050 {
00092     template<class CommandT>
00093     class Command
00094         : public detail::InvokerSignature<boost::function_traits<CommandT>::arity, CommandT, detail::CommandBase<CommandT>* >
00095     {
00096     protected:
00097         std::string mname;
00098         typedef detail::InvokerSignature<boost::function_traits<CommandT>::arity, CommandT, detail::CommandBase<CommandT>* > Base;
00099 
00100         BOOST_STATIC_ASSERT(( boost::is_same<typename boost::function_traits<CommandT>::result_type,bool>::value ));
00101     public:
00102         typedef CommandT Signature;
00103 
00107         typedef DispatchInterface::Status Status;
00108 
00114         Command()
00115             : Base(0), mname()
00116         {}
00117 
00124         Command(std::string name)
00125             : Base(0), mname(name)
00126         {}
00127 
00131         Command(const Command& c)
00132             : Base( c.impl ? c.impl->cloneI() : 0), mname(c.mname)
00133         {
00134         }
00135 
00139         Command& operator=(const Command& c)
00140         {
00141             if ( &c == this )
00142                 return *this;
00143             this->mname = c.mname;
00144             delete this->impl;
00145             this->impl = 0;
00146             if ( c.impl )
00147                 this->impl = c.impl->cloneI();
00148             return *this;
00149         }
00150 
00162         template<class CommandF, class ConditionF, class ObjectT>
00163         Command(std::string name, CommandF com, ConditionF con, ObjectT t, bool invert = false)
00164             : Base( new detail::LocalCommand<CommandT>(com,con,t, invert)),
00165               mname(name)
00166         {
00167         }
00168 
00182         template<class CommandF, class ConditionF, class ObjectT>
00183         Command(std::string name, CommandF com, ConditionF con, ObjectT t, CommandProcessor* commandp, bool invert = false)
00184             : Base( new detail::LocalCommand<CommandT>(com,con,t,commandp, invert)),
00185               mname(name)
00186         {
00187         }
00188 
00198         template<class CommandF, class ConditionF>
00199         Command(std::string name, CommandF com, ConditionF con, CommandProcessor* commandp, bool invert = false)
00200             : Base( new detail::LocalCommand<CommandT>(com,con,commandp, invert)),
00201               mname(name)
00202         {
00203         }
00204 
00212         Command(DispatchInterface* implementation)
00213             : Base( dynamic_cast< detail::CommandBase<CommandT>* >(implementation) ),
00214               mname()
00215         {
00216             // If not convertible, delete the implementation.
00217             if ( !this->impl && implementation) {
00218                 log(Error) << "Tried to assign Command from incompatible type."<< endlog();
00219                 delete implementation;
00220             }
00221         }
00222 
00226         ~Command()
00227         {
00228             delete this->impl;
00229         }
00230 
00238         Command& operator=(DispatchInterface* implementation)
00239         {
00240             if ( this->impl && this->impl == implementation)
00241                 return *this;
00242             delete this->impl;
00243             this->impl = dynamic_cast< detail::CommandBase<CommandT>* >(implementation);
00244             if (this->impl == 0 && implementation) {
00245                 log(Error) << "Tried to assign Command from incompatible type."<< endlog();
00246                 delete implementation;
00247             }
00248             return *this;
00249         }
00250 
00256         bool ready() const {
00257             return this->impl && this->impl->ready();
00258         }
00259 
00264         bool done() const {
00265             if (!this->impl) return false;
00266             return this->impl->done();
00267         }
00268 
00273         void reset() {
00274             if (!this->impl) return;
00275             return this->impl->reset();
00276         }
00277 
00282         bool sent() const {
00283             if (!this->impl) return false;
00284             return this->impl->sent();
00285         }
00286 
00292         bool accepted() const {
00293             if (!this->impl) return false;
00294             return this->impl->accepted();
00295         }
00296 
00301         bool executed() const {
00302             if (!this->impl) return false;
00303             return this->impl->executed();
00304         }
00305 
00310         bool valid() const {
00311             if (!this->impl) return false;
00312             return this->impl->valid();
00313         }
00314 
00320         const std::string& getName() const {
00321             return mname;
00322         }
00323 
00331         detail::CommandBase<CommandT>* getCommandImpl() const {
00332             return this->impl;
00333         }
00334 
00340         void setCommandImpl(detail::CommandBase<CommandT>* new_impl) const {
00341             delete this->impl;
00342             return this->impl = new_impl;
00343         }
00344     };
00345 
00362     template<class ComF, class ConF, class Object>
00363     Command< typename detail::UnMember<ComF>::type > command(std::string name, ComF command, ConF condition, Object object, bool invert = false) {
00364         return Command<  typename detail::UnMember<ComF>::type >(name, command, condition, object, invert);
00365     }
00366 
00382     template<class ComF, class ConF, class Object>
00383     Command< typename detail::UnMember<ComF>::type > command(std::string name, ComF command, ConF condition, Object object, CommandProcessor* cp, bool invert = false) {
00384         return Command<  typename detail::UnMember<ComF>::type >(name, command, condition, object, cp, invert);
00385     }
00386 
00400     template<class ComF, class ConF>
00401     Command< typename detail::UnMember<ComF>::type > command(std::string name, ComF command, ConF condition, CommandProcessor* cp, bool invert = false) {
00402         return Command<  typename detail::UnMember<ComF>::type >(name, command, condition, cp, invert);
00403     }
00404 
00405 }
00406 #endif
Generated on Thu Dec 23 13:22:36 2010 for Orocos Real-Time Toolkit by  doxygen 1.6.3