Lucene++ - a full-featured, c++ search engine
API Documentation

- Main Page
- Related Pages
- Namespaces
- Data Structures
- Files

- File List
- Globals

All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
CloseableThreadLocal.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2011 Alan Wright. All rights reserved.
3 // Distributable under the terms of either the Apache License (Version 2.0)
4 // or the GNU Lesser General Public License.
6
7 #ifndef CLOSEABLETHREADLOCAL_H
8 #define CLOSEABLETHREADLOCAL_H
9
10 #include "LuceneThread.h"
11
12 namespace Lucene
13 {
15 template <typename TYPE>
16 class CloseableThreadLocal : public LuceneObject
17 {
18 public:
19 typedef boost::shared_ptr<TYPE> localDataPtr;
20 typedef Map<int64_t, localDataPtr> MapLocalData;
21
22 CloseableThreadLocal()
23 {
24 localData = MapLocalData::newInstance();
25 }
26
27 public:
28 localDataPtr get()
29 {
30 SyncLock syncLock(this);
31 typename MapLocalData::iterator local = localData.find(LuceneThread::currentId());
32 if (local != localData.end())
33 return local->second;
34 localDataPtr initial(initialValue());
35 if (initial)
36 localData.put(LuceneThread::currentId(), initial);
37 return initial;
38 }
39
40 void set(localDataPtr data)
41 {
42 SyncLock syncLock(this);
43 localData.put(LuceneThread::currentId(), data);
44 }
45
46 void close()
47 {
48 SyncLock syncLock(this);
49 localData.remove(LuceneThread::currentId());
50 }
51
52 protected:
53 MapLocalData localData;
54
55 virtual localDataPtr initialValue()
56 {
57 return localDataPtr(); // override
58 }
59 };
60 }
61
62 #endif

clucene.sourceforge.net