00001 /* 00002 * Licensed to the Apache Software Foundation (ASF) under one or more 00003 * contributor license agreements. See the NOTICE file distributed with 00004 * this work for additional information regarding copyright ownership. 00005 * The ASF licenses this file to You under the Apache License, Version 2.0 00006 * (the "License"); you may not use this file except in compliance with 00007 * the License. You may obtain a copy of the License at 00008 * 00009 * http://www.apache.org/licenses/LICENSE-2.0 00010 * 00011 * Unless required by applicable law or agreed to in writing, software 00012 * distributed under the License is distributed on an "AS IS" BASIS, 00013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 * See the License for the specific language governing permissions and 00015 * limitations under the License. 00016 */ 00017 00018 #ifndef _DECAF_LANG_THREADLOCAL_H_ 00019 #define _DECAF_LANG_THREADLOCAL_H_ 00020 00021 #include <decaf/util/Config.h> 00022 00023 #include <decaf/internal/util/concurrent/ThreadLocalImpl.h> 00024 00025 namespace decaf { 00026 namespace lang { 00027 00042 template <typename E> 00043 class ThreadLocal : protected decaf::internal::util::concurrent::ThreadLocalImpl { 00044 private: 00045 00046 ThreadLocal(const ThreadLocal&); 00047 ThreadLocal& operator= (const ThreadLocal&); 00048 00049 public: 00050 00054 ThreadLocal() : ThreadLocalImpl() {} 00055 00056 virtual ~ThreadLocal() { 00057 try { 00058 removeAll(); 00059 } catch(...) {} 00060 } 00061 00069 E& get() { 00070 void* bytes = getRawValue(); 00071 if (bytes == NULL) { 00072 E* value = new E(); 00073 *value = initialValue(); 00074 setRawValue((void*)value); 00075 bytes = value; 00076 } 00077 return *((E*)bytes); 00078 } 00079 00088 void set(const E& value) { 00089 setRawValue((void*)(new E(value))); 00090 } 00091 00099 void remove() { 00100 this->setRawValue(NULL); 00101 } 00102 00103 protected: 00104 00120 virtual E initialValue() const { 00121 return E(); 00122 } 00123 00124 protected: 00125 00126 virtual void doDelete(void* value) { 00127 if (value != 0) { 00128 delete static_cast<E*>(value); 00129 } 00130 } 00131 00132 }; 00133 00134 }} 00135 00136 #endif /* _DECAF_LANG_THREADLOCAL_H_ */
1.6.1