![]() |
![]() |
00001 #ifndef UTILITIES_GENERAL_MTMAP_H 00002 #define UTILITIES_GENERAL_MTMAP_H 00003 // 00004 // V 0.0 00005 // 00006 #include "Utilities/General/interface/MutexUtils.h" 00007 #include <map> 00008 00011 template<class K, class T> 00012 class MTmap { 00013 public: 00014 00015 typedef std::map< K, T, std::less<K> > SMAP; 00016 typedef typename SMAP::const_iterator const_iterator; 00017 typedef typename SMAP::iterator iterator; 00018 typedef typename SMAP::value_type value_type; 00019 typedef typename SMAP::key_type key_type; 00020 typedef T data_type; 00021 00023 MTmap(){} 00024 00025 00027 ~MTmap(){} 00028 00029 iterator find(const K& k) { 00030 LockMutex a(mutex_); 00031 return me.find(k); 00032 } 00033 00034 const_iterator find(const K& k) const { 00035 LockMutex a(mutex_); 00036 return me.find(k); 00037 } 00038 00039 T & operator[](const K& k) { 00040 LockMutex a(mutex_); 00041 return me[k]; 00042 } 00043 00044 std::pair<iterator,bool> insert(const value_type & elem) { 00045 LockMutex a(mutex_); 00046 return me.insert(elem); 00047 } 00048 00049 iterator begin() { 00050 return me.begin(); 00051 } 00052 00053 const_iterator begin() const { 00054 return me.begin(); 00055 } 00056 00057 iterator end() { 00058 return me.end(); 00059 } 00060 00061 const_iterator end() const { 00062 return me.end(); 00063 } 00064 00065 size_t size() const { 00066 return me.size(); 00067 } 00068 00069 bool empty() const { 00070 return me.empty(); 00071 } 00072 00073 SMAP & backdoor() { return me;} 00074 const SMAP & backdoor() const { return me;} 00075 00076 LockMutex::Mutex & mutex() const { return mutex_;} 00077 00078 void erase(iterator pos) { 00079 LockMutex a(mutex_); 00080 me.erase(pos); 00081 } 00082 00083 void clear() { 00084 LockMutex a(mutex_); 00085 me.clear(); 00086 } 00087 00088 protected: 00089 00090 SMAP me; 00091 00092 mutable LockMutex::Mutex mutex_; 00093 }; 00094 00095 00098 template<class T> 00099 class ThreadObjects { 00100 public: 00101 typedef MTmap<int, T> SMAP; 00102 00103 typedef typename SMAP::const_iterator const_iterator; 00104 typedef typename SMAP::iterator iterator; 00105 typedef std::pair<const int , T> value_type; 00106 00107 iterator find() { return them.find(here());} 00108 00109 const_iterator find() const { return them.find(here());} 00110 00111 T & operator()() { return them[here()];} 00112 00113 T & get() { return them[here()];} 00114 00115 std::pair<iterator,bool> insert(const T & t) { 00116 return them.insert(value_type(here(),t)); 00117 } 00118 void erase() { 00119 them.erase(find()); 00120 } 00121 00122 iterator begin() { 00123 return them.begin(); 00124 } 00125 00126 const_iterator begin() const { 00127 return them.begin(); 00128 } 00129 00130 iterator end() { 00131 return them.end(); 00132 } 00133 00134 const_iterator end() const { 00135 return them.end(); 00136 } 00137 00138 static int here() { return thread_self_tid();} 00139 private: 00140 00141 SMAP them; 00142 00143 }; 00144 00145 #endif // UTILITIES_GENERAL_MTMAP_H