00001 /* 00002 * (c) Copyright 2008, IBM Corporation. 00003 * Licensed under the Apache License, Version 2.0 (the "License"); 00004 * you may not use this file except in compliance with the License. 00005 * You may obtain a copy of the License at 00006 * 00007 * http://www.apache.org/licenses/LICENSE-2.0 00008 * 00009 * Unless required by applicable law or agreed to in writing, software 00010 * distributed under the License is distributed on an "AS IS" BASIS, 00011 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00012 * See the License for the specific language governing permissions and 00013 * limitations under the License. 00014 * 00015 */ 00016 00017 #ifndef SYNC_LIST_H_ 00018 #define SYNC_LIST_H_ 00019 00020 #include "lock.h" 00021 #include "mutex.h" 00022 #include <list> 00023 #include <algorithm> 00024 00025 namespace amino { 00037 template<class T, class Sequence = std::list<T> > 00038 class SyncList { 00039 private: 00043 Sequence internalList; 00047 mutex lock; 00048 00049 public: 00050 typedef typename Sequence::iterator iterator; 00051 typedef typename Sequence::const_iterator const_iterator; 00052 typedef typename Sequence::size_type size_type; 00053 typedef typename Sequence::reference reference; 00054 typedef typename Sequence::const_reference const_reference; 00055 typedef typename Sequence::value_type value_type; 00056 00063 bool empty() { 00064 unique_lock<mutex> common_lock(lock); 00065 return internalList.empty(); 00066 } 00067 00074 size_type size() { 00075 unique_lock<mutex> common_lock(lock); 00076 return internalList.size(); 00077 } 00078 00088 bool front(reference ret) { 00089 unique_lock<mutex> common_lock(lock); 00090 ret = internalList.front(); 00091 return true; 00092 } 00093 00102 T& operator[](int index) { 00103 unique_lock<mutex> common_lock(lock); 00104 } 00105 00114 T& at(int index) { 00115 unique_lock<mutex> common_lock(lock); 00116 } 00117 00129 bool insert(int index, const T& e) { 00130 unique_lock<mutex> common_lock(lock); 00131 iterator ite = internalList.begin(); 00132 for(int i = 0; i < index; ++i) { 00133 if(ite == internalList.end()) { 00134 break; 00135 } 00136 ite++; 00137 } 00138 00139 if(ite != internalList.end()) { 00140 internalList.insert(ite, e); 00141 return true; 00142 } else { 00143 return false; 00144 } 00145 } 00146 00153 bool push_front(const T& x) { 00154 unique_lock<mutex> common_lock(lock); 00155 internalList.push_front(x); 00156 return true; 00157 } 00158 00165 bool push_back(const T& x) { 00166 unique_lock<mutex> common_lock(lock); 00167 internalList.push_back(x); 00168 return true; 00169 } 00170 00174 void pop_front() { 00175 unique_lock<mutex> common_lock(lock); 00176 internalList.pop_front(); 00177 } 00178 00182 void pop_back() { 00183 unique_lock<mutex> common_lock(lock); 00184 internalList.pop_back(); 00185 } 00186 00190 void clear() { 00191 unique_lock<mutex> common_lock(lock); 00192 internalList.clear(); 00193 } 00194 00203 bool remove(const T& value) { 00204 unique_lock<mutex> common_lock(lock); 00205 iterator ite = find(internalList.begin(), internalList.end(), value); 00206 if(ite != internalList.end()) { 00207 internalList.erase(ite); 00208 return true; 00209 } else { 00210 return false; 00211 } 00212 } 00213 00217 void unique() { 00218 unique_lock<mutex> common_lock(lock); 00219 internalList.unique(); 00220 } 00221 00230 void merge(Sequence& x) { 00231 unique_lock<mutex> common_lock(lock); 00232 internalList.merge(x); 00233 } 00234 00239 void reverse() { 00240 unique_lock<mutex> common_lock(lock); 00241 internalList.reverse(); 00242 } 00243 00247 void sort() { 00248 unique_lock<mutex> common_lock(lock); 00249 internalList.reverse(); 00250 } 00251 00252 iterator begin() { 00253 return internalList.begin(); 00254 } 00255 00256 iterator end() { 00257 return internalList.begin(); 00258 } 00259 00260 const_iterator begin() const { 00261 return internalList.begin(); 00262 } 00263 00264 const_iterator end() const { 00265 return internalList.begin(); 00266 } 00267 00268 }; 00269 00270 }; 00271 #endif /* SYNC_LIST_H_ */