blocxx
src
blocxx
GlobalPtr.hpp
Go to the documentation of this file.
1
/*******************************************************************************
2
* Copyright (C) 2005, Quest Software, Inc. All rights reserved.
3
* Copyright (C) 2006, Novell, Inc. All rights reserved.
4
*
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are met:
7
*
8
* * Redistributions of source code must retain the above copyright notice,
9
* this list of conditions and the following disclaimer.
10
* * Redistributions in binary form must reproduce the above copyright
11
* notice, this list of conditions and the following disclaimer in the
12
* documentation and/or other materials provided with the distribution.
13
* * Neither the name of
14
* Quest Software, Inc.,
15
* nor Novell, Inc.,
16
* nor the names of its contributors or employees may be used to
17
* endorse or promote products derived from this software without
18
* specific prior written permission.
19
*
20
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
* POSSIBILITY OF SUCH DAMAGE.
31
*******************************************************************************/
32
33
37
38
#ifndef BLOCXX_GLOBAL_PTR_HPP_INCLUDE_GUARD_
39
#define BLOCXX_GLOBAL_PTR_HPP_INCLUDE_GUARD_
40
#include "blocxx/BLOCXX_config.h"
41
#include "
blocxx/ThreadOnce.hpp
"
42
#include "
blocxx/SafeBool.hpp
"
43
44
namespace
BLOCXX_NAMESPACE
45
{
50
template
<
typename
T>
51
struct
DefaultConstructorFactory
52
{
53
static
T*
create
()
54
{
55
return
new
T;
56
}
57
};
58
82
template
<
typename
T,
typename
FactoryT = DefaultConstructorFactory<T> >
83
class
GlobalPtr
84
{
85
private
:
86
struct
InitPtr
87
{
88
InitPtr
(T*& p)
89
:
m_p
(p)
90
{}
91
T*&
m_p
;
92
93
void
operator()
()
94
{
95
m_p
=
static_cast<
T*
>
(FactoryT::create());
96
}
97
};
98
99
public
:
105
T*
get
()
const
106
{
107
callOnce
(
m_onceFlag
, InitPtr(
m_p
));
108
109
return
m_p
;
110
}
111
118
T*
set
(T* newP)
119
{
120
T* oldP =
get
();
121
m_p
= newP;
122
return
oldP;
123
}
124
125
BLOCXX_SAFE_BOOL_IMPL
(
GlobalPtr
, T*,
GlobalPtr::m_p
,
get
() != 0);
126
127
T &
operator*
()
const
128
{
129
return
*
get
();
130
}
131
T *
operator->
()
const
132
{
133
return
get
();
134
}
135
136
137
// These members should be treated as private. They aren't marked private because if they are, POD initializaer syntax can't be used.
138
mutable
T*
m_p
;
139
mutable
OnceFlag
m_onceFlag
;
140
};
141
146
#define BLOCXX_GLOBAL_PTR_INIT { 0, BLOCXX_ONCE_INIT }
147
148
149
}
// end namespace BLOCXX_NAMESPACE
150
151
152
#endif
153
154
SafeBool.hpp
BLOCXX_SAFE_BOOL_IMPL
#define BLOCXX_SAFE_BOOL_IMPL(classname, type, variable, test)
Definition
SafeBool.hpp:58
ThreadOnce.hpp
BLOCXX_NAMESPACE::GlobalPtr
This class can be used to store a global pointer.
Definition
GlobalPtr.hpp:84
BLOCXX_NAMESPACE::GlobalPtr::set
T * set(T *newP)
Sets the pointer.
Definition
GlobalPtr.hpp:118
BLOCXX_NAMESPACE::GlobalPtr::operator*
T & operator*() const
Definition
GlobalPtr.hpp:127
BLOCXX_NAMESPACE::GlobalPtr< FileSystemMockObject, NullFactory >::m_onceFlag
OnceFlag m_onceFlag
Definition
GlobalPtr.hpp:139
BLOCXX_NAMESPACE::GlobalPtr::operator->
T * operator->() const
Definition
GlobalPtr.hpp:131
BLOCXX_NAMESPACE::GlobalPtr::get
T * get() const
Gets the current value of the pointer.
Definition
GlobalPtr.hpp:105
BLOCXX_NAMESPACE::GlobalPtr< FileSystemMockObject, NullFactory >::m_p
FileSystemMockObject * m_p
Definition
GlobalPtr.hpp:138
BLOCXX_NAMESPACE
Taken from RFC 1321.
Definition
AppenderLogger.cpp:48
BLOCXX_NAMESPACE::callOnce
void BLOCXX_COMMON_API callOnce(OnceFlag &flag, FuncT F)
The first time callOnce is called with a given onceFlag argument, it calls func with no argument and ...
BLOCXX_NAMESPACE::DefaultConstructorFactory
This class is the default factory for GlobalPtr.
Definition
GlobalPtr.hpp:52
BLOCXX_NAMESPACE::DefaultConstructorFactory::create
static T * create()
Definition
GlobalPtr.hpp:53
BLOCXX_NAMESPACE::GlobalPtr::InitPtr::operator()
void operator()()
Definition
GlobalPtr.hpp:93
BLOCXX_NAMESPACE::GlobalPtr::InitPtr::m_p
T *& m_p
Definition
GlobalPtr.hpp:91
BLOCXX_NAMESPACE::GlobalPtr::InitPtr::InitPtr
InitPtr(T *&p)
Definition
GlobalPtr.hpp:88
Generated by
1.13.1