Lunchbox  1.4.0
lockable.h
00001 
00002 /* Copyright (c) 2009-2012, Stefan Eilemann <eile@equalizergraphics.com> 
00003  *
00004  * This library is free software; you can redistribute it and/or modify it under
00005  * the terms of the GNU Lesser General Public License version 2.1 as published
00006  * by the Free Software Foundation.
00007  *  
00008  * This library is distributed in the hope that it will be useful, but WITHOUT
00009  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00010  * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00011  * details.
00012  * 
00013  * You should have received a copy of the GNU Lesser General Public License
00014  * along with this library; if not, write to the Free Software Foundation, Inc.,
00015  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00016  */
00017 
00018 #ifndef LUNCHBOX_LOCKABLE_H
00019 #define LUNCHBOX_LOCKABLE_H
00020 
00021 #include <lunchbox/log.h> // used inline
00022 #include <lunchbox/nonCopyable.h> // base class
00023 #include <iostream>
00024 
00025 namespace lunchbox
00026 {
00032     template< class D, class L = Lock > class Lockable : public NonCopyable
00033     {
00034     public:
00036         explicit Lockable() {}
00037 
00039         explicit Lockable( const D& value ) : data( value ) {}
00040 
00042         template< class P1 > Lockable( const P1& p1 ) : data( p1 ) {}
00043 
00045         D* operator->() { return &data; }
00046 
00048         const D* operator->() const { return &data; }
00049 
00051         D& operator *() { return data; }
00052 
00054         const D& operator*() const { return data; }
00055 
00057         bool operator == ( const D& rhs ) const { return ( data == rhs ); }
00058 
00060         Lockable& operator = ( const D& rhs ) { data = rhs; return *this; }
00061 
00062         D data;
00063         mutable L lock;
00064     };
00065 
00067     template< class D, class L >
00068     inline std::ostream& operator << ( std::ostream& os,
00069                                        const Lockable<D, L>& l )
00070     {
00071         os << disableFlush << "<" << l.lock.isSet() << " " << l.data << ">"
00072            << enableFlush;
00073         return os;
00074     }
00075 }
00076 #endif // LUNCHBOX_LOCKABLE_H