OpenWalnut  1.4.0
WSharedAssociativeContainer.h
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #ifndef WSHAREDASSOCIATIVECONTAINER_H
00026 #define WSHAREDASSOCIATIVECONTAINER_H
00027 
00028 #include <utility>
00029 
00030 #include <boost/thread.hpp>
00031 
00032 #include "WSharedObject.h"
00033 
00034 /**
00035  * This class provides a common interface for thread-safe access to associative containers (set, multiset, map, multimap).
00036  */
00037 template < typename T >
00038 class WSharedAssociativeContainer: public WSharedObject< T >
00039 {
00040 public:
00041     // Some helpful typedefs
00042 
00043     /**
00044      * A typedef for the correct const iterator useful to traverse this sequence container.
00045      */
00046     typedef typename T::const_iterator   ConstIterator;
00047 
00048     /**
00049      * A typedef for the correct iterator to traverse this sequence container.
00050      */
00051     typedef typename T::iterator         Iterator;
00052 
00053     /**
00054      * The type of the elements
00055      */
00056     typedef typename T::value_type value_type;
00057 
00058     /**
00059      * The type of the key used in this associative container
00060      */
00061     typedef typename T::key_type key_type;
00062 
00063     /**
00064      * Default constructor.
00065      */
00066     WSharedAssociativeContainer();
00067 
00068     /**
00069      * Destructor.
00070      */
00071     virtual ~WSharedAssociativeContainer();
00072 
00073     /**
00074      * Clears the container.
00075      */
00076     void clear();
00077 
00078     /**
00079      * Return true if the container is empty. The sense and non-sense of this method in a multi threaded environment is questionable.
00080      *
00081      * \return true if empty
00082      */
00083     bool empty() const;
00084 
00085     /**
00086      * The current size of the container. 0 if empty. The sense and non-sense of this method in a multi threaded environment is questionable.
00087      *
00088      * \return the size.
00089      */
00090     size_t size() const;
00091 
00092     /**
00093      * The maximum size of a container.
00094      *
00095      * \return the maximum size
00096      */
00097     size_t max_size() const;
00098 
00099     /**
00100      * Count elements with a specific key. The sense and non-sense of this method in a multi threaded environment is questionable.
00101      *
00102      * \param x the key
00103      *
00104      * \return the count, 0 if none found.
00105      */
00106     size_t count( const key_type& x ) const;
00107 
00108     /**
00109      * Erases the element with the specified key.
00110      *
00111      * \param x the key
00112      *
00113      * \return the number of elements erased
00114      */
00115     size_t erase( const key_type& x );
00116 
00117     /**
00118      * Inserts the specified element.
00119      *
00120      * \param x the element to add
00121      *
00122      * \return a pair containing the Iterator pointing to the inserted element and the bool is true if the element not existed before.
00123      */
00124     std::pair< Iterator, bool > insert( const value_type& x );
00125 
00126 protected:
00127 private:
00128 };
00129 
00130 template < typename T >
00131 WSharedAssociativeContainer< T >::WSharedAssociativeContainer():
00132     WSharedObject< T >()
00133 {
00134     // init members
00135 }
00136 
00137 template < typename T >
00138 WSharedAssociativeContainer< T >::~WSharedAssociativeContainer()
00139 {
00140     // clean up
00141 }
00142 
00143 template < typename T >
00144 void WSharedAssociativeContainer< T >::clear()
00145 {
00146     typename WSharedAssociativeContainer< T >::WriteTicket w = WSharedObject< T >::getWriteTicket();
00147     w->get().clear();
00148 }
00149 
00150 template < typename T >
00151 bool WSharedAssociativeContainer< T >::empty() const
00152 {
00153     typename WSharedAssociativeContainer< T >::ReadTicket r = WSharedObject< T >::getReadTicket();
00154     return r->get().empty();
00155 }
00156 
00157 template < typename T >
00158 size_t WSharedAssociativeContainer< T >::size() const
00159 {
00160     typename WSharedAssociativeContainer< T >::ReadTicket r = WSharedObject< T >::getReadTicket();
00161     return r->get().size();
00162 }
00163 
00164 template < typename T >
00165 size_t WSharedAssociativeContainer< T >::max_size() const
00166 {
00167     typename WSharedAssociativeContainer< T >::ReadTicket r = WSharedObject< T >::getReadTicket();
00168     return r->get().max_size();
00169 }
00170 
00171 template < typename T >
00172 size_t WSharedAssociativeContainer< T >::count( const key_type& x ) const
00173 {
00174     typename WSharedAssociativeContainer< T >::ReadTicket r = WSharedObject< T >::getReadTicket();
00175     return r->get().count( x );
00176 }
00177 
00178 template < typename T >
00179 size_t WSharedAssociativeContainer< T >::erase( const key_type& x )
00180 {
00181     typename WSharedAssociativeContainer< T >::WriteTicket w = WSharedObject< T >::getWriteTicket();
00182     return w->get().erase( x );
00183 }
00184 
00185 template < typename T >
00186 std::pair< typename WSharedAssociativeContainer< T >::Iterator, bool > WSharedAssociativeContainer< T >::insert( const value_type& x )
00187 {
00188     typename WSharedAssociativeContainer< T >::WriteTicket w = WSharedObject< T >::getWriteTicket();
00189     return w->get().insert( x );
00190 }
00191 
00192 #endif  // WSHAREDASSOCIATIVECONTAINER_H
00193