Skip to content

Commit faa1675

Browse files
committed
added safe_ptr as pointer wrapper to ensure actual method constness
1 parent a846bc2 commit faa1675

4 files changed

Lines changed: 70 additions & 0 deletions

File tree

lib/cppcheck.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@
159159
<ClInclude Include="preprocessor.h" />
160160
<ClInclude Include="programmemory.h" />
161161
<ClInclude Include="reverseanalyzer.h" />
162+
<ClInclude Include="safeptr.h" />
162163
<ClInclude Include="settings.h" />
163164
<ClInclude Include="smallvector.h" />
164165
<ClInclude Include="standards.h" />

lib/lib.pri

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ HEADERS += $${PWD}/analyzer.h \
5454
$${PWD}/preprocessor.h \
5555
$${PWD}/programmemory.h \
5656
$${PWD}/reverseanalyzer.h \
57+
$${PWD}/safeptr.h \
5758
$${PWD}/settings.h \
5859
$${PWD}/smallvector.h \
5960
$${PWD}/standards.h \

lib/safeptr.h

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
template<typename T>
28+
class safe_ptr
29+
{
30+
public:
31+
safe_ptr(T* p)
32+
: mPtr(p)
33+
{}
34+
35+
T* get() NOEXCEPT {
36+
return mPtr;
37+
}
38+
39+
const T* get() const NOEXCEPT {
40+
return mPtr;
41+
}
42+
43+
T* operator->() NOEXCEPT {
44+
return mPtr;
45+
}
46+
47+
const T* operator->() const NOEXCEPT {
48+
return mPtr;
49+
}
50+
51+
T& operator*() NOEXCEPT {
52+
return *mPtr;
53+
}
54+
55+
const T& operator*() const NOEXCEPT {
56+
return *mPtr;
57+
}
58+
59+
operator bool() const NOEXCEPT {
60+
return mPtr != nullptr;
61+
}
62+
63+
private:
64+
T* mPtr;
65+
};
66+
67+
#endif // safePtrH

tools/dmake.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ int main(int argc, char **argv)
313313
libfiles_h.emplace_back("calculate.h");
314314
libfiles_h.emplace_back("config.h");
315315
libfiles_h.emplace_back("precompiled.h");
316+
libfiles_h.emplace_back("safeptr.h");
316317
libfiles_h.emplace_back("smallvector.h");
317318
libfiles_h.emplace_back("standards.h");
318319
libfiles_h.emplace_back("tokenrange.h");

0 commit comments

Comments
 (0)