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
include
CloseableThreadLocal.h
Go to the documentation of this file.
1
2
// Copyright (c) 2009-2014 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
public
:
18
typedef
boost::shared_ptr<TYPE>
localDataPtr
;
19
typedef
Map<int64_t, localDataPtr>
MapLocalData
;
20
21
CloseableThreadLocal
() {
22
localData
=
MapLocalData::newInstance
();
23
}
24
25
public
:
26
localDataPtr
get
() {
27
SyncLock
syncLock(
this
);
28
typename
MapLocalData::iterator
local =
localData
.
find
(
LuceneThread::currentId
());
29
if
(local !=
localData
.
end
()) {
30
return
local->second;
31
}
32
localDataPtr
initial(
initialValue
());
33
if
(initial) {
34
localData
.
put
(
LuceneThread::currentId
(), initial);
35
}
36
return
initial;
37
}
38
39
void
set
(
const
localDataPtr
& data) {
40
SyncLock
syncLock(
this
);
41
localData
.
put
(
LuceneThread::currentId
(), data);
42
}
43
44
void
close
() {
45
SyncLock
syncLock(
this
);
46
localData
.
remove
(
LuceneThread::currentId
());
47
}
48
49
protected
:
50
MapLocalData
localData
;
51
52
virtual
localDataPtr
initialValue
() {
53
return
localDataPtr
();
// override
54
}
55
};
56
57
}
58
59
#endif
clucene.sourceforge.net