| 1 | /* |
|---|
| 2 | * Copyright (c) 2005, Eric Crahen |
|---|
| 3 | * |
|---|
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
|---|
| 5 | * of this software and associated documentation files (the "Software"), to deal |
|---|
| 6 | * in the Software without restriction, including without limitation the rights |
|---|
| 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|---|
| 8 | * copies of the Software, and to permit persons to whom the Software is furnished |
|---|
| 9 | * to do so, subject to the following conditions: |
|---|
| 10 | * |
|---|
| 11 | * The above copyright notice and this permission notice shall be included in all |
|---|
| 12 | * copies or substantial portions of the Software. |
|---|
| 13 | * |
|---|
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|---|
| 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|---|
| 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|---|
| 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
|---|
| 18 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
|---|
| 19 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|---|
| 20 | * |
|---|
| 21 | */ |
|---|
| 22 | |
|---|
| 23 | #ifndef __ZTTSS_H__ |
|---|
| 24 | #define __ZTTSS_H__ |
|---|
| 25 | |
|---|
| 26 | #include <windows.h> |
|---|
| 27 | |
|---|
| 28 | namespace ZThread { |
|---|
| 29 | |
|---|
| 30 | /** |
|---|
| 31 | * @class TSS |
|---|
| 32 | * @author Eric Crahen <http://www.code-foo.com> |
|---|
| 33 | * @date <2003-07-27T14:18:43-0400> |
|---|
| 34 | * @version 2.3.0 |
|---|
| 35 | * |
|---|
| 36 | * An abstraction for dealing with WIN32 thread specific storage (tss). |
|---|
| 37 | * Provides get/set and creation/destruction. |
|---|
| 38 | */ |
|---|
| 39 | template <typename T> |
|---|
| 40 | class TSS { |
|---|
| 41 | |
|---|
| 42 | DWORD _key; |
|---|
| 43 | bool _valid; |
|---|
| 44 | |
|---|
| 45 | public: |
|---|
| 46 | |
|---|
| 47 | /** |
|---|
| 48 | * Create a new object for accessing tss. The def |
|---|
| 49 | */ |
|---|
| 50 | TSS() { |
|---|
| 51 | |
|---|
| 52 | _key = ::TlsAlloc(); |
|---|
| 53 | _valid = (_key != 0xFFFFFFFF); |
|---|
| 54 | |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | /** |
|---|
| 58 | * Destroy the underlying supoprt for accessing tss with this |
|---|
| 59 | * object. |
|---|
| 60 | */ |
|---|
| 61 | virtual ~TSS() { |
|---|
| 62 | |
|---|
| 63 | if(_valid) |
|---|
| 64 | ::TlsFree(_key); |
|---|
| 65 | |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | /** |
|---|
| 69 | * Get the value stored in tss. |
|---|
| 70 | * |
|---|
| 71 | * @return T |
|---|
| 72 | * |
|---|
| 73 | * @exception InvalidOp_exception thrown when the tss is not properly initialized |
|---|
| 74 | */ |
|---|
| 75 | inline T get() const { |
|---|
| 76 | |
|---|
| 77 | if(!_valid) |
|---|
| 78 | throw InvalidOp_Exception(); |
|---|
| 79 | |
|---|
| 80 | return static_cast<T>(::TlsGetValue(_key)); |
|---|
| 81 | |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | /** |
|---|
| 85 | * Store a value in tss. |
|---|
| 86 | * |
|---|
| 87 | * @param value T |
|---|
| 88 | * @return T old value |
|---|
| 89 | * |
|---|
| 90 | * @exception InvalidOp_exception thrown when the tss is not properly initialized |
|---|
| 91 | */ |
|---|
| 92 | inline T set(T value) const { |
|---|
| 93 | |
|---|
| 94 | T oldValue = get(); |
|---|
| 95 | ::TlsSetValue(_key, value); |
|---|
| 96 | |
|---|
| 97 | return oldValue; |
|---|
| 98 | |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | }; |
|---|
| 103 | |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | #endif |
|---|
| 107 | |
|---|
| 108 | |
|---|