Lunchbox
1.8.0
Main Page
Related Pages
Namespaces
Classes
Files
File List
File Members
install
include
lunchbox
perThreadRef.h
1
2
/* Copyright (c) 2008-2013, Stefan Eilemann <eile@equalizergraphics.com>
3
*
4
* This library is free software; you can redistribute it and/or modify it under
5
* the terms of the GNU Lesser General Public License version 2.1 as published
6
* by the Free Software Foundation.
7
*
8
* This library is distributed in the hope that it will be useful, but WITHOUT
9
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11
* details.
12
*
13
* You should have received a copy of the GNU Lesser General Public License
14
* along with this library; if not, write to the Free Software Foundation, Inc.,
15
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16
*/
17
18
#ifndef LUNCHBOX_PERTHREADREF_H
19
#define LUNCHBOX_PERTHREADREF_H
20
21
#include <lunchbox/tls.h>
// member
22
23
namespace
lunchbox
24
{
25
27
template
<
typename
T >
class
PerThreadRef
:
public
NonCopyable
28
{
29
public
:
31
PerThreadRef
();
33
~PerThreadRef
();
34
36
PerThreadRef<T>
&
operator =
(
RefPtr< T >
data );
37
39
PerThreadRef<T>
&
operator =
(
const
PerThreadRef<T>
& rhs );
40
42
RefPtr< const T >
get
()
const
;
44
RefPtr< T >
get
();
45
50
T*
getPointer
();
51
56
T*
operator->
();
57
62
const
T*
operator->
()
const
;
63
68
bool
operator ==
(
const
PerThreadRef
& rhs )
const
69
{
return
(
get
() == rhs.
get
( )); }
70
75
bool
operator ==
(
const
RefPtr< T >
& rhs )
const
76
{
return
(
get
()==rhs ); }
77
82
bool
operator !=
(
const
RefPtr< T >
& rhs )
const
83
{
return
(
get
()!=rhs ); }
84
89
bool
operator !
()
const
;
90
95
bool
isValid
()
const
;
96
97
private
:
98
TLS
tls_;
99
};
100
101
template
<
typename
T >
PerThreadRef<T>::PerThreadRef
()
102
: tls_( 0 )
103
{}
104
105
template
<
typename
T >
PerThreadRef<T>::~PerThreadRef
()
106
{
107
RefPtr< T >
object
=
get
();
108
object
.unref();
109
}
110
111
template
<
typename
T >
112
PerThreadRef<T>
&
PerThreadRef<T>::operator =
(
RefPtr< T >
data )
113
{
114
data.ref();
// ref new
115
116
RefPtr< T >
object
=
get
();
117
tls_.set( static_cast<const void*>( data.
get
( )));
118
119
object
.unref();
// unref old
120
return
*
this
;
121
}
122
123
template
<
typename
T >
124
PerThreadRef<T>
&
PerThreadRef<T>::operator =
(
const
PerThreadRef<T>
& rhs )
125
{
126
RefPtr< T >
newObject = rhs.
get
();
127
newObject.ref();
// ref new
128
129
RefPtr< T >
object
=
get
();
130
tls_.set( rhs.tls_.
get
( ));
131
132
object
.unref();
// unref old
133
return
*
this
;
134
}
135
136
template
<
typename
T >
RefPtr< const T >
PerThreadRef<T>::get
()
const
137
{
138
return
static_cast<
const
T*
>
( tls_.get( ));
139
}
140
141
template
<
typename
T >
RefPtr< T >
PerThreadRef<T>::get
()
142
{
143
return
static_cast<
T*
>
( tls_.get( ));
144
}
145
146
template
<
typename
T >
147
T*
PerThreadRef<T>::getPointer
()
148
{
149
return
static_cast<
T*
>
( tls_.get( ));
150
}
151
152
template
<
typename
T >
153
T*
PerThreadRef<T>::operator->
()
154
{
155
LBASSERT( tls_.get( ));
156
return
static_cast<
T*
>
( tls_.get( ));
157
}
158
159
template
<
typename
T >
160
const
T*
PerThreadRef<T>::operator->
()
const
161
{
162
LBASSERT( tls_.get( ));
163
return
static_cast<
const
T*
>
( tls_.get( ));
164
}
165
166
template
<
typename
T >
167
bool
PerThreadRef<T>::operator !
()
const
168
{
169
return
tls_.get() == 0;
170
}
171
172
template
<
typename
T >
173
bool
PerThreadRef<T>::isValid
()
const
174
{
175
return
tls_.get() != 0;
176
}
177
178
}
179
#endif //LUNCHBOX_PERTHREADREF_H
Generated on Thu Jun 27 2013 13:28:50 for Lunchbox by
1.8.1.2