Coverage Report

Created: 2025-12-31 07:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/log4cplus/include/log4cplus/helpers/pointer.h
Line
Count
Source
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
27
/** @file */
28
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
38
#include <log4cplus/thread/syncprims.h>
39
#include <algorithm>
40
#include <cassert>
41
#if ! defined (LOG4CPLUS_SINGLE_THREADED)
42
#include <atomic>
43
#endif
44
45
46
namespace log4cplus {
47
    namespace helpers {
48
49
        /******************************************************************************
50
         *                       Class SharedObject (from pp. 204-205)                *
51
         ******************************************************************************/
52
53
        class LOG4CPLUS_EXPORT SharedObject
54
        {
55
        public:
56
            void addReference() const LOG4CPLUS_NOEXCEPT;
57
            void removeReference() const;
58
59
        protected:
60
          // Ctor
61
            SharedObject()
62
426k
                : access_mutex()
63
426k
                , count__(0)
64
426k
            { }
65
66
            SharedObject(const SharedObject&)
67
                : access_mutex()
68
                , count__(0)
69
0
            { }
70
71
            SharedObject(SharedObject &&)
72
                : access_mutex()
73
                , count__(0)
74
0
            { }
75
76
          // Dtor
77
            virtual ~SharedObject();
78
79
          // Operators
80
0
            SharedObject& operator=(const SharedObject&) LOG4CPLUS_NOEXCEPT { return *this; }
81
0
            SharedObject& operator=(SharedObject &&) LOG4CPLUS_NOEXCEPT { return *this; }
82
83
        public:
84
            thread::Mutex access_mutex;
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>
100
        class SharedObjectPtr
101
        {
102
        public:
103
            // Ctor
104
            explicit
105
            SharedObjectPtr(T* realPtr = 0) LOG4CPLUS_NOEXCEPT
106
1.85M
                : pointee(realPtr)
107
1.85M
            {
108
1.85M
                addref ();
109
1.85M
            }
log4cplus::helpers::SharedObjectPtr<log4cplus::spi::Filter>::SharedObjectPtr(log4cplus::spi::Filter*)
Line
Count
Source
106
426k
                : pointee(realPtr)
107
426k
            {
108
426k
                addref ();
109
426k
            }
log4cplus::helpers::SharedObjectPtr<log4cplus::Appender>::SharedObjectPtr(log4cplus::Appender*)
Line
Count
Source
106
1.42M
                : pointee(realPtr)
107
1.42M
            {
108
1.42M
                addref ();
109
1.42M
            }
log4cplus::helpers::SharedObjectPtr<log4cplus::spi::LoggerImpl>::SharedObjectPtr(log4cplus::spi::LoggerImpl*)
Line
Count
Source
106
394
                : pointee(realPtr)
107
394
            {
108
394
                addref ();
109
394
            }
Unexecuted instantiation: log4cplus::helpers::SharedObjectPtr<log4cplus::helpers::ConnectorThread>::SharedObjectPtr(log4cplus::helpers::ConnectorThread*)
Unexecuted instantiation: log4cplus::helpers::SharedObjectPtr<log4cplus::thread::AbstractThread>::SharedObjectPtr(log4cplus::thread::AbstractThread*)
Unexecuted instantiation: log4cplus::helpers::SharedObjectPtr<log4cplus::thread::Queue>::SharedObjectPtr(log4cplus::thread::Queue*)
Unexecuted instantiation: log4cplus::helpers::SharedObjectPtr<log4cplus::AsyncAppender>::SharedObjectPtr(log4cplus::AsyncAppender*)
110
111
            SharedObjectPtr(const SharedObjectPtr& rhs) LOG4CPLUS_NOEXCEPT
112
1.70M
                : pointee(rhs.pointee)
113
1.70M
            {
114
1.70M
                addref ();
115
1.70M
            }
Unexecuted instantiation: log4cplus::helpers::SharedObjectPtr<log4cplus::spi::Filter>::SharedObjectPtr(log4cplus::helpers::SharedObjectPtr<log4cplus::spi::Filter> const&)
log4cplus::helpers::SharedObjectPtr<log4cplus::Appender>::SharedObjectPtr(log4cplus::helpers::SharedObjectPtr<log4cplus::Appender> const&)
Line
Count
Source
112
1.70M
                : pointee(rhs.pointee)
113
1.70M
            {
114
1.70M
                addref ();
115
1.70M
            }
Unexecuted instantiation: log4cplus::helpers::SharedObjectPtr<log4cplus::thread::Queue>::SharedObjectPtr(log4cplus::helpers::SharedObjectPtr<log4cplus::thread::Queue> const&)
116
117
            SharedObjectPtr(SharedObjectPtr && rhs) LOG4CPLUS_NOEXCEPT
118
0
                : pointee (std::move (rhs.pointee))
119
0
            {
120
0
                rhs.pointee = 0;
121
0
            }
Unexecuted instantiation: log4cplus::helpers::SharedObjectPtr<log4cplus::spi::Filter>::SharedObjectPtr(log4cplus::helpers::SharedObjectPtr<log4cplus::spi::Filter>&&)
Unexecuted instantiation: log4cplus::helpers::SharedObjectPtr<log4cplus::thread::AbstractThread>::SharedObjectPtr(log4cplus::helpers::SharedObjectPtr<log4cplus::thread::AbstractThread>&&)
Unexecuted instantiation: log4cplus::helpers::SharedObjectPtr<log4cplus::Appender>::SharedObjectPtr(log4cplus::helpers::SharedObjectPtr<log4cplus::Appender>&&)
Unexecuted instantiation: log4cplus::helpers::SharedObjectPtr<log4cplus::AsyncAppender>::SharedObjectPtr(log4cplus::helpers::SharedObjectPtr<log4cplus::AsyncAppender>&&)
Unexecuted instantiation: log4cplus::helpers::SharedObjectPtr<log4cplus::thread::Queue>::SharedObjectPtr(log4cplus::helpers::SharedObjectPtr<log4cplus::thread::Queue>&&)
122
123
            SharedObjectPtr & operator = (SharedObjectPtr && rhs) LOG4CPLUS_NOEXCEPT
124
426k
            {
125
426k
                rhs.swap (*this);
126
426k
                return *this;
127
426k
            }
Unexecuted instantiation: log4cplus::helpers::SharedObjectPtr<log4cplus::spi::Filter>::operator=(log4cplus::helpers::SharedObjectPtr<log4cplus::spi::Filter>&&)
log4cplus::helpers::SharedObjectPtr<log4cplus::Appender>::operator=(log4cplus::helpers::SharedObjectPtr<log4cplus::Appender>&&)
Line
Count
Source
124
426k
            {
125
426k
                rhs.swap (*this);
126
426k
                return *this;
127
426k
            }
128
129
            // Dtor
130
            ~SharedObjectPtr()
131
2.92M
            {
132
2.92M
                if (pointee)
133
1.95M
                    pointee->removeReference();
134
2.92M
            }
log4cplus::helpers::SharedObjectPtr<log4cplus::spi::Filter>::~SharedObjectPtr()
Line
Count
Source
131
426k
            {
132
426k
                if (pointee)
133
0
                    pointee->removeReference();
134
426k
            }
log4cplus::helpers::SharedObjectPtr<log4cplus::Appender>::~SharedObjectPtr()
Line
Count
Source
131
2.50M
            {
132
2.50M
                if (pointee)
133
1.95M
                    pointee->removeReference();
134
2.50M
            }
log4cplus::helpers::SharedObjectPtr<log4cplus::spi::LoggerImpl>::~SharedObjectPtr()
Line
Count
Source
131
162
            {
132
162
                if (pointee)
133
0
                    pointee->removeReference();
134
162
            }
Unexecuted instantiation: log4cplus::helpers::SharedObjectPtr<log4cplus::helpers::ConnectorThread>::~SharedObjectPtr()
Unexecuted instantiation: log4cplus::helpers::SharedObjectPtr<log4cplus::thread::AbstractThread>::~SharedObjectPtr()
Unexecuted instantiation: log4cplus::helpers::SharedObjectPtr<log4cplus::AsyncAppender>::~SharedObjectPtr()
Unexecuted instantiation: log4cplus::helpers::SharedObjectPtr<log4cplus::thread::Queue>::~SharedObjectPtr()
135
136
            // Operators
137
            bool operator==(const SharedObjectPtr& rhs) const
138
0
            { 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
1.00M
            T* operator->() const {assert (pointee); return pointee; }
Unexecuted instantiation: log4cplus::helpers::SharedObjectPtr<log4cplus::spi::Filter>::operator->() const
log4cplus::helpers::SharedObjectPtr<log4cplus::Appender>::operator->() const
Line
Count
Source
143
1.00M
            T* operator->() const {assert (pointee); return pointee; }
Unexecuted instantiation: log4cplus::helpers::SharedObjectPtr<log4cplus::spi::LoggerImpl>::operator->() const
Unexecuted instantiation: log4cplus::helpers::SharedObjectPtr<log4cplus::helpers::ConnectorThread>::operator->() const
Unexecuted instantiation: log4cplus::helpers::SharedObjectPtr<log4cplus::AsyncAppender>::operator->() const
Unexecuted instantiation: log4cplus::helpers::SharedObjectPtr<log4cplus::thread::Queue>::operator->() const
Unexecuted instantiation: log4cplus::helpers::SharedObjectPtr<log4cplus::thread::AbstractThread>::operator->() const
144
729k
            T& operator*() const {assert (pointee); return *pointee; }
145
146
            SharedObjectPtr& operator=(const SharedObjectPtr& rhs)
147
123k
            {
148
123k
                return this->operator = (rhs.pointee);
149
123k
            }
log4cplus::helpers::SharedObjectPtr<log4cplus::Appender>::operator=(log4cplus::helpers::SharedObjectPtr<log4cplus::Appender> const&)
Line
Count
Source
147
123k
            {
148
123k
                return this->operator = (rhs.pointee);
149
123k
            }
Unexecuted instantiation: log4cplus::helpers::SharedObjectPtr<log4cplus::spi::Filter>::operator=(log4cplus::helpers::SharedObjectPtr<log4cplus::spi::Filter> const&)
Unexecuted instantiation: log4cplus::helpers::SharedObjectPtr<log4cplus::spi::LoggerImpl>::operator=(log4cplus::helpers::SharedObjectPtr<log4cplus::spi::LoggerImpl> const&)
150
151
            SharedObjectPtr& operator=(T* rhs)
152
123k
            {
153
123k
                SharedObjectPtr<T> (rhs).swap (*this);
154
123k
                return *this;
155
123k
            }
log4cplus::helpers::SharedObjectPtr<log4cplus::Appender>::operator=(log4cplus::Appender*)
Line
Count
Source
152
123k
            {
153
123k
                SharedObjectPtr<T> (rhs).swap (*this);
154
123k
                return *this;
155
123k
            }
Unexecuted instantiation: log4cplus::helpers::SharedObjectPtr<log4cplus::spi::Filter>::operator=(log4cplus::spi::Filter*)
log4cplus::helpers::SharedObjectPtr<log4cplus::spi::LoggerImpl>::operator=(log4cplus::spi::LoggerImpl*)
Line
Count
Source
152
162
            {
153
162
                SharedObjectPtr<T> (rhs).swap (*this);
154
162
                return *this;
155
162
            }
Unexecuted instantiation: log4cplus::helpers::SharedObjectPtr<log4cplus::helpers::ConnectorThread>::operator=(log4cplus::helpers::ConnectorThread*)
Unexecuted instantiation: log4cplus::helpers::SharedObjectPtr<log4cplus::thread::Queue>::operator=(log4cplus::thread::Queue*)
Unexecuted instantiation: log4cplus::helpers::SharedObjectPtr<log4cplus::thread::AbstractThread>::operator=(log4cplus::thread::AbstractThread*)
156
157
          // Methods
158
7.29M
            T* get() const { return pointee; }
log4cplus::helpers::SharedObjectPtr<log4cplus::spi::Filter>::get() const
Line
Count
Source
158
773k
            T* get() const { return pointee; }
log4cplus::helpers::SharedObjectPtr<log4cplus::spi::LoggerImpl>::get() const
Line
Count
Source
158
6.52M
            T* get() const { return pointee; }
Unexecuted instantiation: log4cplus::helpers::SharedObjectPtr<log4cplus::helpers::ConnectorThread>::get() const
Unexecuted instantiation: log4cplus::helpers::SharedObjectPtr<log4cplus::thread::Queue>::get() const
Unexecuted instantiation: log4cplus::helpers::SharedObjectPtr<log4cplus::thread::AbstractThread>::get() const
159
160
            void swap (SharedObjectPtr & other) LOG4CPLUS_NOEXCEPT
161
549k
            {
162
549k
                std::swap (pointee, other.pointee);
163
549k
            }
Unexecuted instantiation: log4cplus::helpers::SharedObjectPtr<log4cplus::spi::Filter>::swap(log4cplus::helpers::SharedObjectPtr<log4cplus::spi::Filter>&)
log4cplus::helpers::SharedObjectPtr<log4cplus::Appender>::swap(log4cplus::helpers::SharedObjectPtr<log4cplus::Appender>&)
Line
Count
Source
161
549k
            {
162
549k
                std::swap (pointee, other.pointee);
163
549k
            }
log4cplus::helpers::SharedObjectPtr<log4cplus::spi::LoggerImpl>::swap(log4cplus::helpers::SharedObjectPtr<log4cplus::spi::LoggerImpl>&)
Line
Count
Source
161
162
            {
162
162
                std::swap (pointee, other.pointee);
163
162
            }
Unexecuted instantiation: log4cplus::helpers::SharedObjectPtr<log4cplus::helpers::ConnectorThread>::swap(log4cplus::helpers::SharedObjectPtr<log4cplus::helpers::ConnectorThread>&)
Unexecuted instantiation: log4cplus::helpers::SharedObjectPtr<log4cplus::thread::Queue>::swap(log4cplus::helpers::SharedObjectPtr<log4cplus::thread::Queue>&)
Unexecuted instantiation: log4cplus::helpers::SharedObjectPtr<log4cplus::thread::AbstractThread>::swap(log4cplus::helpers::SharedObjectPtr<log4cplus::thread::AbstractThread>&)
164
165
            typedef T * (SharedObjectPtr:: * unspec_bool_type) () const;
166
            operator unspec_bool_type () const
167
0
            {
168
0
                return pointee ? &SharedObjectPtr::get : 0;
169
0
            }
Unexecuted instantiation: log4cplus::helpers::SharedObjectPtr<log4cplus::spi::Filter>::operator log4cplus::spi::Filter* (log4cplus::helpers::SharedObjectPtr<log4cplus::spi::Filter>::*)() const() const
Unexecuted instantiation: log4cplus::helpers::SharedObjectPtr<log4cplus::spi::LoggerImpl>::operator log4cplus::spi::LoggerImpl* (log4cplus::helpers::SharedObjectPtr<log4cplus::spi::LoggerImpl>::*)() const() const
Unexecuted instantiation: log4cplus::helpers::SharedObjectPtr<log4cplus::helpers::ConnectorThread>::operator log4cplus::helpers::ConnectorThread* (log4cplus::helpers::SharedObjectPtr<log4cplus::helpers::ConnectorThread>::*)() const() const
Unexecuted instantiation: log4cplus::helpers::SharedObjectPtr<log4cplus::thread::Queue>::operator log4cplus::thread::Queue* (log4cplus::helpers::SharedObjectPtr<log4cplus::thread::Queue>::*)() const() const
Unexecuted instantiation: log4cplus::helpers::SharedObjectPtr<log4cplus::thread::AbstractThread>::operator log4cplus::thread::AbstractThread* (log4cplus::helpers::SharedObjectPtr<log4cplus::thread::AbstractThread>::*)() const() const
170
171
            bool operator ! () const
172
549k
            {
173
549k
                return ! pointee;
174
549k
            }
Unexecuted instantiation: log4cplus::helpers::SharedObjectPtr<log4cplus::spi::Filter>::operator!() const
log4cplus::helpers::SharedObjectPtr<log4cplus::Appender>::operator!() const
Line
Count
Source
172
549k
            {
173
549k
                return ! pointee;
174
549k
            }
175
176
        private:
177
          // Methods
178
            void addref() const LOG4CPLUS_NOEXCEPT
179
3.55M
            {
180
3.55M
                if (pointee)
181
1.95M
                    pointee->addReference();
182
3.55M
            }
log4cplus::helpers::SharedObjectPtr<log4cplus::spi::Filter>::addref() const
Line
Count
Source
179
426k
            {
180
426k
                if (pointee)
181
0
                    pointee->addReference();
182
426k
            }
log4cplus::helpers::SharedObjectPtr<log4cplus::Appender>::addref() const
Line
Count
Source
179
3.12M
            {
180
3.12M
                if (pointee)
181
1.95M
                    pointee->addReference();
182
3.12M
            }
log4cplus::helpers::SharedObjectPtr<log4cplus::spi::LoggerImpl>::addref() const
Line
Count
Source
179
394
            {
180
394
                if (pointee)
181
162
                    pointee->addReference();
182
394
            }
Unexecuted instantiation: log4cplus::helpers::SharedObjectPtr<log4cplus::helpers::ConnectorThread>::addref() const
Unexecuted instantiation: log4cplus::helpers::SharedObjectPtr<log4cplus::thread::AbstractThread>::addref() const
Unexecuted instantiation: log4cplus::helpers::SharedObjectPtr<log4cplus::thread::Queue>::addref() const
Unexecuted instantiation: log4cplus::helpers::SharedObjectPtr<log4cplus::AsyncAppender>::addref() const
183
184
          // Data
185
            T* pointee;
186
        };
187
188
189
        //! Boost `intrusive_ptr` helpers.
190
        //! @{
191
        inline
192
        void
193
        intrusive_ptr_add_ref (SharedObject const * so)
194
0
        {
195
0
            so->addReference();
196
0
        }
197
198
        inline
199
        void
200
        intrusive_ptr_release (SharedObject const * so)
201
0
        {
202
0
            so->removeReference();
203
0
        }
204
        //! @}
205
206
    } // end namespace helpers
207
} // end namespace log4cplus
208
209
210
#endif // LOG4CPLUS_HELPERS_POINTERS_HEADER_