resolve cyclic dependency with zstd
[platform/upstream/cmake.git] / Source / cmSourceGroup.cxx
1 /* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2    file Copyright.txt or https://cmake.org/licensing for details.  */
3 #include "cmSourceGroup.h"
4
5 #include <utility>
6
7 #include <cm/memory>
8
9 #include "cmStringAlgorithms.h"
10
11 class cmSourceGroupInternals
12 {
13 public:
14   std::vector<cmSourceGroup> GroupChildren;
15 };
16
17 cmSourceGroup::cmSourceGroup(std::string name, const char* regex,
18                              const char* parentName)
19   : Name(std::move(name))
20 {
21   this->Internal = cm::make_unique<cmSourceGroupInternals>();
22   this->SetGroupRegex(regex);
23   if (parentName) {
24     this->FullName = cmStrCat(parentName, '\\');
25   }
26   this->FullName += this->Name;
27 }
28
29 cmSourceGroup::~cmSourceGroup() = default;
30
31 cmSourceGroup::cmSourceGroup(cmSourceGroup const& r)
32 {
33   this->Name = r.Name;
34   this->FullName = r.FullName;
35   this->GroupRegex = r.GroupRegex;
36   this->GroupFiles = r.GroupFiles;
37   this->SourceFiles = r.SourceFiles;
38   this->Internal = cm::make_unique<cmSourceGroupInternals>(*r.Internal);
39 }
40
41 cmSourceGroup& cmSourceGroup::operator=(cmSourceGroup const& r)
42 {
43   this->Name = r.Name;
44   this->GroupRegex = r.GroupRegex;
45   this->GroupFiles = r.GroupFiles;
46   this->SourceFiles = r.SourceFiles;
47   *(this->Internal) = *(r.Internal);
48   return *this;
49 }
50
51 void cmSourceGroup::SetGroupRegex(const char* regex)
52 {
53   if (regex) {
54     this->GroupRegex.compile(regex);
55   } else {
56     this->GroupRegex.compile("^$");
57   }
58 }
59
60 void cmSourceGroup::AddGroupFile(const std::string& name)
61 {
62   this->GroupFiles.insert(name);
63 }
64
65 std::string const& cmSourceGroup::GetName() const
66 {
67   return this->Name;
68 }
69
70 std::string const& cmSourceGroup::GetFullName() const
71 {
72   return this->FullName;
73 }
74
75 bool cmSourceGroup::MatchesRegex(const std::string& name)
76 {
77   return this->GroupRegex.find(name);
78 }
79
80 bool cmSourceGroup::MatchesFiles(const std::string& name) const
81 {
82   return this->GroupFiles.find(name) != this->GroupFiles.cend();
83 }
84
85 void cmSourceGroup::AssignSource(const cmSourceFile* sf)
86 {
87   this->SourceFiles.push_back(sf);
88 }
89
90 const std::vector<const cmSourceFile*>& cmSourceGroup::GetSourceFiles() const
91 {
92   return this->SourceFiles;
93 }
94
95 void cmSourceGroup::AddChild(cmSourceGroup const& child)
96 {
97   this->Internal->GroupChildren.push_back(child);
98 }
99
100 cmSourceGroup* cmSourceGroup::LookupChild(const std::string& name)
101 {
102   for (cmSourceGroup& group : this->Internal->GroupChildren) {
103     // look if descenened is the one were looking for
104     if (group.GetName() == name) {
105       return (&group); // if it so return it
106     }
107   }
108
109   // if no child with this name was found return NULL
110   return nullptr;
111 }
112
113 cmSourceGroup* cmSourceGroup::MatchChildrenFiles(const std::string& name)
114 {
115   if (this->MatchesFiles(name)) {
116     return this;
117   }
118   for (cmSourceGroup& group : this->Internal->GroupChildren) {
119     cmSourceGroup* result = group.MatchChildrenFiles(name);
120     if (result) {
121       return result;
122     }
123   }
124   return nullptr;
125 }
126
127 const cmSourceGroup* cmSourceGroup::MatchChildrenFiles(
128   const std::string& name) const
129 {
130   if (this->MatchesFiles(name)) {
131     return this;
132   }
133   for (const cmSourceGroup& group : this->Internal->GroupChildren) {
134     const cmSourceGroup* result = group.MatchChildrenFiles(name);
135     if (result) {
136       return result;
137     }
138   }
139   return nullptr;
140 }
141
142 cmSourceGroup* cmSourceGroup::MatchChildrenRegex(const std::string& name)
143 {
144   for (cmSourceGroup& group : this->Internal->GroupChildren) {
145     cmSourceGroup* result = group.MatchChildrenRegex(name);
146     if (result) {
147       return result;
148     }
149   }
150   if (this->MatchesRegex(name)) {
151     return this;
152   }
153
154   return nullptr;
155 }
156
157 std::vector<cmSourceGroup> const& cmSourceGroup::GetGroupChildren() const
158 {
159   return this->Internal->GroupChildren;
160 }