CppParser
Loading...
Searching...
No Matches
cppcompound-info-accessor.h
Go to the documentation of this file.
1/*
2The MIT License (MIT)
3
4Copyright (c) 2018 Satya Das
5
6Permission is hereby granted, free of charge, to any person obtaining a copy of
7this software and associated documentation files (the "Software"), to deal in
8the Software without restriction, including without limitation the rights to
9use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10 the Software, and to permit persons to whom the Software is furnished to do so,
11subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24#pragma once
25
26#include "cppast.h"
27#include "cppconst.h"
28#include "cpputil.h"
29
31
32#include <functional>
33
34 inline bool forEachMember(CppCompoundEPtr compound, std::function<bool(CppObj*)> visitor)
35{
36for (auto& mem : compound->members())
37{
38 if (visitor(mem.get()))
39 return true;
40}
41
42return false;
43}
44
45inline bool forEachMember(CppCompoundEPtr compound,
46 CppAccessType memAccessType,
47 std::function<bool(CppObj*)> visitor)
48{
49for (auto& mem : compound->members())
50{
51 if ((accessType(mem.get()) == memAccessType) && visitor(mem.get()))
52 return true;
53}
54
55return false;
56}
57
58inline bool isNamespace(CppCompoundEPtr compound)
59{
60return compound->compoundType() == CppCompoundType::kNamespace;
61}
62inline bool isClass(CppCompoundEPtr compound)
63{
64return compound->compoundType() == CppCompoundType::kClass;
65}
66inline bool isStruct(CppCompoundEPtr compound)
67{
68return compound->compoundType() == CppCompoundType::kStruct;
69}
70inline bool isUnion(CppCompoundEPtr compound)
71{
72return compound->compoundType() == CppCompoundType::kUnion;
73}
74inline bool isCppFile(CppCompoundEPtr compound)
75{
76return compound->compoundType() == CppCompoundType::kCppFile;
77}
78inline bool isBlock(CppCompoundEPtr compound)
79{
80return compound->compoundType() == CppCompoundType::kBlock;
81}
82
84inline std::string fullName(CppCompoundEPtr compound)
85{
86if (!isNamespaceLike(compound))
87 return "";
88if (compound->owner() && isNamespaceLike(compound->owner()))
89 return fullName(compound->owner()) + "::" + compound->name();
90else
91 return compound->name();
92}
93
94inline bool traverse(CppCompoundEPtr compound, std::function<bool(CppObj*)> visitor)
95{
96forEachMember(compound, [&](CppObj* mem) {
97 if (isNamespaceLike(mem) && traverse(mem, visitor))
98 return true;
99 if (visitor(mem))
100 return true;
101
102 return false;
103});
104
105return false;
106}
107
108inline bool traversePreorder(CppCompoundEPtr compound, std::function<bool(CppObj*)> visitor)
109{
110forEachMember(compound, [&](CppObj* mem) {
111 if (visitor(mem))
112 return true;
113 if (isNamespaceLike(mem) && traverse(mem, visitor))
114 return true;
115
116 return false;
117});
118
119return false;
120}
Helps working with raw or unique_ptr of CppObj in a uniform way.
Definition: cppeasyptr.h:44
bool isNamespace(CppCompoundEPtr compound)
bool forEachMember(CppCompoundEPtr compound, std::function< bool(CppObj *)> visitor)
bool isClass(CppCompoundEPtr compound)
bool traverse(CppCompoundEPtr compound, std::function< bool(CppObj *)> visitor)
bool traversePreorder(CppCompoundEPtr compound, std::function< bool(CppObj *)> visitor)
bool isStruct(CppCompoundEPtr compound)
bool isCppFile(CppCompoundEPtr compound)
std::string fullName(CppCompoundEPtr compound)
bool isUnion(CppCompoundEPtr compound)
bool isBlock(CppCompoundEPtr compound)
CppAccessType
Definition: cppconst.h:103
@ kBlock
Definition: cppconst.h:98
@ kNamespace
Definition: cppconst.h:94
@ kUnion
Definition: cppconst.h:97
@ kClass
Definition: cppconst.h:95
@ kCppFile
Definition: cppconst.h:93
@ kStruct
Definition: cppconst.h:96
CppAccessType accessType(CppObj *cppObj)
bool isNamespaceLike(CppObj *cppObj)
An abstract class that is used as base class of all other classes.
Definition: cppast.h:134