ReadOnlyPointer.hpp
Go to the documentation of this file.
00001 /***************************************************************************
00002   tag: Peter Soetens  Thu Oct 22 11:59:08 CEST 2009  ReadOnlyPointer.hpp
00003 
00004                         ReadOnlyPointer.hpp -  description
00005                            -------------------
00006     begin                : Thu October 22 2009
00007     copyright            : (C) 2009 Sylvain Joyeux
00008     email                : Sylvain Joyeux <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 #ifndef RTT_READ_WRITE_POINTER_HPP
00040 #define RTT_READ_WRITE_POINTER_HPP
00041 
00042 #include <boost/intrusive_ptr.hpp>
00043 #include <boost/call_traits.hpp>
00044 
00045 #include "../os/Mutex.hpp"
00046 #include "../os/MutexLock.hpp"
00047 
00048 namespace RTT
00049 { namespace extras {
00050 
00051         template<typename T>
00052         struct ROPtrInternal
00053         {
00054             os::Mutex  lock;
00055             T*     value;
00056             size_t readers;
00057 
00058             ROPtrInternal(T* value)
00059                 : value(value), readers(0) {}
00060             ~ROPtrInternal() { delete value; }
00061 
00062             void ref()
00063             { os::MutexLock do_lock(lock);
00064                 ++readers;
00065             }
00066             bool deref()
00067             { os::MutexLock do_lock(lock);
00068                 return (--readers) != 0;
00069             }
00070         };
00071 
00072         template<typename T>
00073         void intrusive_ptr_add_ref(ROPtrInternal<T>* data)
00074         {
00075             data->ref();
00076         }
00077         template<typename T>
00078         void intrusive_ptr_release(ROPtrInternal<T>* data)
00079         {
00080             if (!data->deref())
00081                 delete data;
00082         }
00083 
00097     template<typename T>
00098     class ReadOnlyPointer
00099     {
00100         typedef ROPtrInternal<T> Internal;
00101         boost::intrusive_ptr<Internal> internal;
00102         typedef boost::call_traits<T> traits;
00103 
00104     public:
00105         ReadOnlyPointer(T* ptr = 0)
00106             : internal(new Internal(ptr)) {}
00107 
00108         typename traits::const_reference operator *() const { return *(internal->value); }
00109         T const* operator ->() const { return internal->value; }
00110 
00112         bool valid() const
00113         { return internal->value != 0; }
00114 
00115         T const* get() const
00116         {
00117             return internal->value;
00118         }
00119 
00141         void reset(T* ptr)
00142         {
00143             boost::intrusive_ptr<Internal> safe = this->internal;
00144             if (!safe)
00145             {
00146                 internal = new Internal(ptr);
00147                 return;
00148             }
00149 
00150             if (safe->value == ptr)
00151                 return;
00152 
00153             { os::MutexLock do_lock(safe->lock);
00154                 if (safe->readers == 2) // we are sole owner
00155                 {
00156                     delete safe->value;
00157                     safe->value = ptr;
00158                     return;
00159                 }
00160             }
00161 
00162             // We must *not* change 'internal' while safe->lock is taken. The
00163             // above block returns in case we don't need to reallocate a new
00164             // Internal structure.
00165             //
00166             // In other words, if we are here, it is because we *need* to
00167             // reallocate.
00168             internal = new Internal(ptr);
00169         }
00170 
00180         T* try_write_access()
00181         {
00182             boost::intrusive_ptr<Internal> safe = this->internal;
00183             if (!safe)
00184                 return 0;
00185 
00186             { os::MutexLock do_lock(safe->lock);
00187                 if (safe->readers == 2)
00188                 { // we're the only owner (don't forget +safe+ above).
00189                   // Just promote the current copy
00190                     T* value = 0;
00191                     std::swap(value, safe->value);
00192                     return value;
00193                 }
00194                 else
00195                 { // there are other owners
00196                     return NULL;
00197                 }
00198             }
00199         }
00200 
00212         T* write_access()
00213         {
00214             boost::intrusive_ptr<Internal> safe = this->internal;
00215             if (!safe)
00216                 return 0;
00217 
00218             { os::MutexLock do_lock(safe->lock);
00219                 if (safe->readers == 2)
00220                 { // we're the only owner (don't forget +safe+ above).
00221                   // Just promote the current copy
00222                     T* value = 0;
00223                     std::swap(value, safe->value);
00224                     return value;
00225                 }
00226                 else
00227                 { // there are other owners, do a copy
00228                     return new T(*safe->value);
00229                 }
00230             }
00231         }
00232     };
00233 }}
00234 
00235 #endif
00236 


rtt
Author(s): RTT Developers
autogenerated on Wed Aug 26 2015 16:15:51