Skip to content

Commit 36705b4

Browse files
committed
added safe_ptr as pointer wrapper to ensure actual method constness
1 parent 4d18f3e commit 36705b4

4 files changed

Lines changed: 75 additions & 0 deletions

File tree

lib/cppcheck.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@
162162
<ClInclude Include="preprocessor.h" />
163163
<ClInclude Include="programmemory.h" />
164164
<ClInclude Include="reverseanalyzer.h" />
165+
<ClInclude Include="safeptr.h" />
165166
<ClInclude Include="settings.h" />
166167
<ClInclude Include="smallvector.h" />
167168
<ClInclude Include="standards.h" />

lib/lib.pri

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ HEADERS += $${PWD}/analyzer.h \
5656
$${PWD}/preprocessor.h \
5757
$${PWD}/programmemory.h \
5858
$${PWD}/reverseanalyzer.h \
59+
$${PWD}/safeptr.h \
5960
$${PWD}/settings.h \
6061
$${PWD}/smallvector.h \
6162
$${PWD}/standards.h \

lib/safeptr.h

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Cppcheck - A tool for static C/C++ code analysis
3+
* Copyright (C) 2007-2023 Cppcheck team.
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
//---------------------------------------------------------------------------
20+
#ifndef safePtrH
21+
#define safePtrH
22+
//---------------------------------------------------------------------------
23+
24+
#include "config.h"
25+
26+
// as std::optional behaves similarly so we could use that instead of if we ever move to C++17.
27+
// it is not a simple drop-in as our "operator bool()" indicates if the pointer is non-null
28+
// whereas std::optional indicates if a value is set.
29+
//
30+
// This is similar to std::experimental::propagate_const
31+
// see https://en.cppreference.com/w/cpp/experimental/propagate_const
32+
template<typename T>
33+
class safe_ptr
34+
{
35+
public:
36+
explicit safe_ptr(T* p)
37+
: mPtr(p)
38+
{}
39+
40+
T* get() NOEXCEPT {
41+
return mPtr;
42+
}
43+
44+
const T* get() const NOEXCEPT {
45+
return mPtr;
46+
}
47+
48+
T* operator->() NOEXCEPT {
49+
return mPtr;
50+
}
51+
52+
const T* operator->() const NOEXCEPT {
53+
return mPtr;
54+
}
55+
56+
T& operator*() NOEXCEPT {
57+
return *mPtr;
58+
}
59+
60+
const T& operator*() const NOEXCEPT {
61+
return *mPtr;
62+
}
63+
64+
explicit operator bool() const NOEXCEPT {
65+
return mPtr != nullptr;
66+
}
67+
68+
private:
69+
T* mPtr;
70+
};
71+
72+
#endif // safePtrH

tools/dmake.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ int main(int argc, char **argv)
316316
libfiles_h.emplace_back("config.h");
317317
libfiles_h.emplace_back("json.h");
318318
libfiles_h.emplace_back("precompiled.h");
319+
libfiles_h.emplace_back("safeptr.h");
319320
libfiles_h.emplace_back("smallvector.h");
320321
libfiles_h.emplace_back("standards.h");
321322
libfiles_h.emplace_back("tokenrange.h");

0 commit comments

Comments
 (0)