log4cplus 2.0.8
pointer.h
Go to the documentation of this file.
1// -*- C++ -*-
2// Module: Log4CPLUS
3// File: pointer.h
4// Created: 6/2001
5// Author: Tad E. Smith
6//
7//
8// Copyright 2001-2017 Tad E. Smith
9//
10// Licensed under the Apache License, Version 2.0 (the "License");
11// you may not use this file except in compliance with the License.
12// You may obtain a copy of the License at
13//
14// http://www.apache.org/licenses/LICENSE-2.0
15//
16// Unless required by applicable law or agreed to in writing, software
17// distributed under the License is distributed on an "AS IS" BASIS,
18// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19// See the License for the specific language governing permissions and
20// limitations under the License.
21
22//
23// Note: Some of this code uses ideas from "More Effective C++" by Scott
24// Myers, Addison Wesley Longmain, Inc., (c) 1996, Chapter 29, pp. 183-213
25//
26
29#ifndef LOG4CPLUS_HELPERS_POINTERS_HEADER_
30#define LOG4CPLUS_HELPERS_POINTERS_HEADER_
31
32#include <log4cplus/config.hxx>
33
34#if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
35#pragma once
36#endif
37
39#include <algorithm>
40#include <cassert>
41#if ! defined (LOG4CPLUS_SINGLE_THREADED)
42#include <atomic>
43#endif
44
45
46namespace log4cplus {
47 namespace helpers {
48
49 /******************************************************************************
50 * Class SharedObject (from pp. 204-205) *
51 ******************************************************************************/
52
54 {
55 public:
57 void removeReference() const;
58
59 protected:
60 // Ctor
62 : access_mutex()
63 , count__(0)
64 { }
65
67 : access_mutex()
68 , count__(0)
69 { }
70
72 : access_mutex()
73 , count__(0)
74 { }
75
76 // Dtor
77 virtual ~SharedObject();
78
79 // Operators
82
83 public:
85
86 private:
87#if defined (LOG4CPLUS_SINGLE_THREADED)
88 typedef unsigned count_type;
89#else
90 typedef std::atomic<unsigned> count_type;
91#endif
92 mutable count_type count__;
93 };
94
95
96 /******************************************************************************
97 * Template Class SharedObjectPtr (from pp. 203, 206) *
98 ******************************************************************************/
99 template<class T>
101 {
102 public:
103 // Ctor
104 explicit
106 : pointee(realPtr)
107 {
108 addref ();
109 }
110
112 : pointee(rhs.pointee)
113 {
114 addref ();
115 }
116
118 : pointee (std::move (rhs.pointee))
119 {
120 rhs.pointee = 0;
121 }
122
124 {
125 rhs.swap (*this);
126 return *this;
127 }
128
129 // Dtor
131 {
132 if (pointee)
133 pointee->removeReference();
134 }
135
136 // Operators
137 bool operator==(const SharedObjectPtr& rhs) const
138 { return (pointee == rhs.pointee); }
139 bool operator!=(const SharedObjectPtr& rhs) const
140 { return (pointee != rhs.pointee); }
141 bool operator==(const T* rhs) const { return (pointee == rhs); }
142 bool operator!=(const T* rhs) const { return (pointee != rhs); }
143 T* operator->() const {assert (pointee); return pointee; }
144 T& operator*() const {assert (pointee); return *pointee; }
145
147 {
148 return this->operator = (rhs.pointee);
149 }
150
152 {
153 SharedObjectPtr<T> (rhs).swap (*this);
154 return *this;
155 }
156
157 // Methods
158 T* get() const { return pointee; }
159
161 {
162 std::swap (pointee, other.pointee);
163 }
164
165 typedef T * (SharedObjectPtr:: * unspec_bool_type) () const;
166 operator unspec_bool_type () const
167 {
168 return pointee ? &SharedObjectPtr::get : 0;
169 }
170
171 bool operator ! () const
172 {
173 return ! pointee;
174 }
175
176 private:
177 // Methods
178 void addref() const LOG4CPLUS_NOEXCEPT
179 {
180 if (pointee)
181 pointee->addReference();
182 }
183
184 // Data
185 T* pointee;
186 };
187
188
191 inline
192 void
194 {
195 so->addReference();
196 }
197
198 inline
199 void
201 {
202 so->removeReference();
203 }
205
206 } // end namespace helpers
207} // end namespace log4cplus
208
209
210#endif // LOG4CPLUS_HELPERS_POINTERS_HEADER_
SharedObjectPtr(SharedObjectPtr &&rhs) LOG4CPLUS_NOEXCEPT
Definition pointer.h:117
SharedObjectPtr & operator=(T *rhs)
Definition pointer.h:151
bool operator!=(const T *rhs) const
Definition pointer.h:142
SharedObjectPtr & operator=(SharedObjectPtr &&rhs) LOG4CPLUS_NOEXCEPT
Definition pointer.h:123
T *(SharedObjectPtr::* unspec_bool_type)() const
Definition pointer.h:165
bool operator==(const SharedObjectPtr &rhs) const
Definition pointer.h:137
SharedObjectPtr(T *realPtr=0) LOG4CPLUS_NOEXCEPT
Definition pointer.h:105
SharedObjectPtr(const SharedObjectPtr &rhs) LOG4CPLUS_NOEXCEPT
Definition pointer.h:111
void swap(SharedObjectPtr &other) LOG4CPLUS_NOEXCEPT
Definition pointer.h:160
bool operator!=(const SharedObjectPtr &rhs) const
Definition pointer.h:139
SharedObjectPtr & operator=(const SharedObjectPtr &rhs)
Definition pointer.h:146
bool operator==(const T *rhs) const
Definition pointer.h:141
SharedObject(SharedObject &&)
Definition pointer.h:71
SharedObject(const SharedObject &)
Definition pointer.h:66
void addReference() const LOG4CPLUS_NOEXCEPT
SharedObject & operator=(SharedObject &&) LOG4CPLUS_NOEXCEPT
Definition pointer.h:81
SharedObject & operator=(const SharedObject &) LOG4CPLUS_NOEXCEPT
Definition pointer.h:80
#define LOG4CPLUS_NOEXCEPT
Definition config.hxx:101
void intrusive_ptr_add_ref(SharedObject const *so)
Boost intrusive_ptr helpers.
Definition pointer.h:193
void intrusive_ptr_release(SharedObject const *so)
Definition pointer.h:200
#define LOG4CPLUS_EXPORT
Definition win32.h:141