Orocos Real-Time Toolkit  2.6.0
ChannelInterface.cpp
00001 /***************************************************************************
00002   tag: Peter Soetens  Thu Oct 22 11:59:07 CEST 2009  ChannelInterface.cpp
00003 
00004                         ChannelInterface.cpp -  description
00005                            -------------------
00006     begin                : Thu October 22 2009
00007     copyright            : (C) 2009 Sylvain Joyeux
00008     email                : sylvain.joyeux@m4x.org
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 #include "../internal/Channels.hpp"
00040 #include "../os/Atomic.hpp"
00041 #include "../os/MutexLock.hpp"
00042 
00043 using namespace RTT;
00044 using namespace RTT::detail;
00045 
00046 ChannelElementBase::ChannelElementBase()
00047     : input(0)
00048 
00049 {
00050     ORO_ATOMIC_SETUP(&refcount,0);
00051 }
00052 
00053 ChannelElementBase::~ChannelElementBase()
00054 {
00055     ORO_ATOMIC_CLEANUP(&refcount);
00056 }
00057 
00058 ChannelElementBase::shared_ptr ChannelElementBase::getInput()
00059 { RTT::os::MutexLock lock(inout_lock);
00060     return ChannelElementBase::shared_ptr(input);
00061 }
00062 
00063 ChannelElementBase::shared_ptr ChannelElementBase::getOutput()
00064 { RTT::os::MutexLock lock(inout_lock);
00065     return ChannelElementBase::shared_ptr(output);
00066 }
00067 
00068 void ChannelElementBase::setOutput(shared_ptr output)
00069 {
00070     this->output = output;
00071     if (output)
00072         output->input = this;
00073 }
00074 
00075 void ChannelElementBase::disconnect(bool forward)
00076 {
00077     if (forward)
00078     {
00079         shared_ptr output = getOutput();
00080         if (output)
00081             output->disconnect(true);
00082     }
00083     else
00084     {
00085         shared_ptr input = getInput();
00086         if (input)
00087             input->disconnect(false);
00088     }
00089 
00090     { RTT::os::MutexLock lock(inout_lock);
00091         this->input = 0;
00092         this->output = 0;
00093     }
00094 }
00095 
00096 ChannelElementBase::shared_ptr ChannelElementBase::getInputEndPoint()
00097 {
00098     shared_ptr input = getInput();
00099     return input ? input->getInputEndPoint() : this;
00100 }
00101 ChannelElementBase::shared_ptr ChannelElementBase::getOutputEndPoint()
00102 {
00103     shared_ptr output = getOutput();
00104     return output ? output->getOutputEndPoint() : this;
00105 }
00106 
00107 bool ChannelElementBase::inputReady()
00108 {
00109     // we go against the data stream
00110     shared_ptr input = getInput();
00111     if (input)
00112         return input->inputReady();
00113     return false;
00114 }
00115 
00116 void ChannelElementBase::clear()
00117 {
00118     shared_ptr input = getInput();
00119     if (input)
00120         input->clear();
00121 }
00122 
00123 bool ChannelElementBase::signal()
00124 {
00125     shared_ptr output = getOutput();
00126     if (output)
00127         return output->signal();
00128     return true;
00129 }
00130 
00131 PortInterface* ChannelElementBase::getPort() const {
00132     return 0;
00133 }
00134 
00135 void ChannelElementBase::ref()
00136 {
00137     oro_atomic_inc(&refcount);
00138 }
00139 
00140 void ChannelElementBase::deref()
00141 {
00142     if ( oro_atomic_dec_and_test(&refcount) ) delete this;
00143 }
00144 
00145 void RTT::base::intrusive_ptr_add_ref( ChannelElementBase* p )
00146 { p->ref(); }
00147 
00148 void RTT::base::intrusive_ptr_release( ChannelElementBase* p )
00149 { p->deref(); }
00150