Libbarrett  1.2.4
include/barrett/systems/helpers.h
Go to the documentation of this file.
00001 /*
00002         Copyright 2009, 2010 Barrett Technology <support@barrett.com>
00003 
00004         This file is part of libbarrett.
00005 
00006         This version of libbarrett is free software: you can redistribute it
00007         and/or modify it under the terms of the GNU General Public License as
00008         published by the Free Software Foundation, either version 3 of the
00009         License, or (at your option) any later version.
00010 
00011         This version of libbarrett is distributed in the hope that it will be
00012         useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
00013         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014         GNU General Public License for more details.
00015 
00016         You should have received a copy of the GNU General Public License along
00017         with this version of libbarrett.  If not, see
00018         <http://www.gnu.org/licenses/>.
00019 
00020         Further, non-binding information about licensing is available at:
00021         <http://wiki.barrett.com/libbarrett/wiki/LicenseNotes>
00022 */
00023 
00032 #ifndef BARRETT_SYSTEMS_HELPERS_H_
00033 #define BARRETT_SYSTEMS_HELPERS_H_
00034 
00035 
00036 #include <cassert>
00037 
00038 #include <barrett/thread/abstract/mutex.h>
00039 #include <barrett/systems/abstract/system.h>
00040 
00041 
00042 namespace barrett {
00043 namespace systems {
00044 
00045 
00046 template<typename T>
00047 void connect(System::Output<T>& output, System::Input<T>& input)
00048 {
00049         boost::lock_guard<thread::Mutex> inputLg(input.getEmMutex());
00050         boost::lock_guard<thread::Mutex> outputLg(output.getEmMutex());
00051 
00052 
00053         assert( !input.isConnected() );
00054 
00055         input.output = &output;
00056         output.inputs.push_back(input);
00057 
00058         input.pushExecutionManager();
00059 }
00060 
00061 template<typename T>
00062 void reconnect(System::Output<T>& newOutput, System::Input<T>& input)
00063 {
00064         BARRETT_SCOPED_LOCK(input.getEmMutex());
00065 
00066         assert(input.isConnected());
00067 
00068         disconnect(input);
00069         connect(newOutput, input);
00070 }
00071 
00072 template<typename T>
00073 void forceConnect(System::Output<T>& output, System::Input<T>& input)
00074 {
00075         BARRETT_SCOPED_LOCK(input.getEmMutex());
00076 
00077         if (input.isConnected()) {
00078                 reconnect(output, input);
00079         } else {
00080                 connect(output, input);
00081         }
00082 }
00083 
00084 template<typename T>
00085 void disconnect(System::Input<T>& input)
00086 {
00087         BARRETT_SCOPED_LOCK(input.getEmMutex());
00088 
00089         if (input.isConnected()) {
00090                 input.output->inputs.erase(System::Output<T>::connected_input_list_type::s_iterator_to(input));
00091                 input.unsetExecutionManager();
00092                 input.output = NULL;
00093         }
00094 }
00095 
00096 template<typename T>
00097 void disconnect(System::Output<T>& output)
00098 {
00099         BARRETT_SCOPED_LOCK(output.getEmMutex());
00100         assert(output.parentSys != NULL);
00101 
00102         output.inputs.clear_and_dispose(typename System::Input<T>::DisconnectDisposer());
00103         output.parentSys->unsetExecutionManager();
00104 }
00105 
00106 
00107 }
00108 }
00109 
00110 
00111 #endif /* BARRETT_SYSTEMS_HELPERS_H_ */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Defines