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
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
44namespace BLOCXX_NAMESPACE
45{
50 template <typename T>
52 {
53 static T* create()
54 {
55 return new T;
56 }
57 };
58
82 template <typename T, typename FactoryT = DefaultConstructorFactory<T> >
84 {
85 private:
86 struct InitPtr
87 {
88 InitPtr(T*& p)
89 : m_p(p)
90 {}
91 T*& m_p;
92
94 {
95 m_p = static_cast<T*>(FactoryT::create());
96 }
97 };
98
99 public:
105 T* get() const
106 {
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
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
#define BLOCXX_SAFE_BOOL_IMPL(classname, type, variable, test)
Definition SafeBool.hpp:58
This class can be used to store a global pointer.
Definition GlobalPtr.hpp:84
T * set(T *newP)
Sets the pointer.
T * get() const
Gets the current value of the pointer.
Taken from RFC 1321.
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 ...
This class is the default factory for GlobalPtr.
Definition GlobalPtr.hpp:52