Skip to content

Commit 922dcdc

Browse files
committed
added constness_ptr as pointer wrapper to ensure actual method constness
1 parent 6a71e3b commit 922dcdc

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

lib/constnessptr.h

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/* -*- C++ -*-
2+
* Cppcheck - A tool for static C/C++ code analysis
3+
* Copyright (C) 2007-2025 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 constnessPtrH
21+
#define constnessPtrH
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 constness_ptr
34+
{
35+
public:
36+
explicit constness_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 // constnessPtrH

lib/cppcheck.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@
133133
<ClInclude Include="clangimport.h" />
134134
<ClInclude Include="color.h" />
135135
<ClInclude Include="config.h" />
136+
<ClInclude Include="constnessptr.h" />
136137
<ClInclude Include="cppcheck.h" />
137138
<ClInclude Include="ctu.h" />
138139
<ClInclude Include="errorlogger.h" />

tools/dmake/dmake.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@ int main(int argc, char **argv)
468468
libfiles_h.emplace("analyzer.h");
469469
libfiles_h.emplace("calculate.h");
470470
libfiles_h.emplace("config.h");
471+
libfiles_h.emplace("constnessptr.h");
471472
libfiles_h.emplace("filesettings.h");
472473
libfiles_h.emplace("findtoken.h");
473474
libfiles_h.emplace("json.h");

0 commit comments

Comments
 (0)