Orocos Real-Time Toolkit  2.5.0
ProgramService.cpp
00001 /***************************************************************************
00002   tag: Peter Soetens  Wed Jan 18 14:11:40 CET 2006  ProgramService.cpp
00003 
00004                         ProgramService.cpp -  description
00005                            -------------------
00006     begin                : Wed January 18 2006
00007     copyright            : (C) 2006 Peter Soetens
00008     email                : peter.soetens@mech.kuleuven.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 
00040 #include "ProgramService.hpp"
00041 
00042 #include "../OperationCaller.hpp"
00043 #include "../FactoryExceptions.hpp"
00044 #include "../ExecutionEngine.hpp"
00045 #include "../internal/DataSources.hpp"
00046 
00047 namespace RTT
00048 {
00049 
00050     using namespace detail;
00051 
00052     ProgramService::ProgramService(FunctionGraphPtr prog, TaskContext* tc)
00053         : Service( prog->getName(), tc),
00054           program( new ValueDataSource<ProgramInterfacePtr>(prog) ),
00055           function(prog)
00056     {
00057         this->doc("Orocos Program Script");
00058 
00059         // We need a weak pointer here in order to be able to unload programs that
00060         // reference self. The only way we can use weak_ptr with OperationCaller/Operation is by putting it in the data source
00061         // of the first argument. We can not 'boost::bind' to a weak pointer, only to a shared_ptr.
00062         DataSource<ProgramInterfacePtr>* ptr = program.get();
00063         // OperationCallers :
00064         addOperationDS("start", &ProgramInterface::start,ptr).doc("Start or continue this program.");
00065         addOperationDS("pause", &ProgramInterface::pause,ptr).doc("Pause this program.");
00066         addOperationDS("step", &ProgramInterface::step,ptr).doc("Step a paused program.");
00067         addOperationDS("stop", &ProgramInterface::stop,ptr).doc("Stop and reset this program.");
00068 
00069         // DataSources:
00070 
00071         addOperationDS("isRunning", &ProgramInterface::isRunning,ptr).doc("Is this program being executed and not paused ?");
00072         addOperationDS("inError", &ProgramInterface::inError,ptr).doc("Has this program executed an erroneous method ?");
00073         addOperationDS("isPaused", &ProgramInterface::isPaused,ptr).doc("Is this program running but paused ?");
00074     }
00075 
00076     ProgramService::~ProgramService() {
00077         // When the this Service is deleted, make sure the program does not reference us.
00078         FunctionGraphPtr prog = function;
00079         if ( prog ) {
00080             prog->setProgramService( ProgramServicePtr() );
00081         }
00082     }
00083 }