1
0
mirror of https://github.com/Kitware/CMake.git synced 2025-10-15 20:46:37 +08:00

cmSourceGroup: code improvements; use std::string and C++11 loops

Topic-rename: cmSourceGroup-modern-cxx
This commit is contained in:
Vitaly Stakhovsky
2018-01-10 19:01:49 -05:00
parent ddc4f9a3c0
commit 969c1f94ae
3 changed files with 24 additions and 46 deletions

View File

@@ -1961,7 +1961,7 @@ cmSourceGroup* cmMakefile::GetSourceGroup(
if (sg != nullptr) { if (sg != nullptr) {
// iterate through its children to find match source group // iterate through its children to find match source group
for (unsigned int i = 1; i < name.size(); ++i) { for (unsigned int i = 1; i < name.size(); ++i) {
sg = sg->LookupChild(name[i].c_str()); sg = sg->LookupChild(name[i]);
if (sg == nullptr) { if (sg == nullptr) {
break; break;
} }
@@ -2005,7 +2005,7 @@ void cmMakefile::AddSourceGroup(const std::vector<std::string>& name,
if (i == -1) { if (i == -1) {
// group does not exist nor belong to any existing group // group does not exist nor belong to any existing group
// add its first component // add its first component
this->SourceGroups.push_back(cmSourceGroup(name[0].c_str(), regex)); this->SourceGroups.push_back(cmSourceGroup(name[0], regex));
sg = this->GetSourceGroup(currentName); sg = this->GetSourceGroup(currentName);
i = 0; // last component found i = 0; // last component found
} }
@@ -2015,9 +2015,8 @@ void cmMakefile::AddSourceGroup(const std::vector<std::string>& name,
} }
// build the whole source group path // build the whole source group path
for (++i; i <= lastElement; ++i) { for (++i; i <= lastElement; ++i) {
sg->AddChild( sg->AddChild(cmSourceGroup(name[i], nullptr, sg->GetFullName().c_str()));
cmSourceGroup(name[i].c_str(), nullptr, sg->GetFullName().c_str())); sg = sg->LookupChild(name[i]);
sg = sg->LookupChild(name[i].c_str());
} }
sg->SetGroupRegex(regex); sg->SetGroupRegex(regex);

View File

@@ -8,7 +8,7 @@ public:
std::vector<cmSourceGroup> GroupChildren; std::vector<cmSourceGroup> GroupChildren;
}; };
cmSourceGroup::cmSourceGroup(const char* name, const char* regex, cmSourceGroup::cmSourceGroup(const std::string& name, const char* regex,
const char* parentName) const char* parentName)
: Name(name) : Name(name)
{ {
@@ -70,14 +70,14 @@ std::string const& cmSourceGroup::GetFullName() const
return this->FullName; return this->FullName;
} }
bool cmSourceGroup::MatchesRegex(const char* name) bool cmSourceGroup::MatchesRegex(const std::string& name)
{ {
return this->GroupRegex.find(name); return this->GroupRegex.find(name);
} }
bool cmSourceGroup::MatchesFiles(const char* name) bool cmSourceGroup::MatchesFiles(const std::string& name) const
{ {
return this->GroupFiles.find(name) != this->GroupFiles.end(); return this->GroupFiles.find(name) != this->GroupFiles.cend();
} }
void cmSourceGroup::AssignSource(const cmSourceFile* sf) void cmSourceGroup::AssignSource(const cmSourceFile* sf)
@@ -95,21 +95,12 @@ void cmSourceGroup::AddChild(cmSourceGroup const& child)
this->Internal->GroupChildren.push_back(child); this->Internal->GroupChildren.push_back(child);
} }
cmSourceGroup* cmSourceGroup::LookupChild(const char* name) const cmSourceGroup* cmSourceGroup::LookupChild(const std::string& name)
{ {
// initializing iterators for (cmSourceGroup& group : this->Internal->GroupChildren) {
std::vector<cmSourceGroup>::const_iterator iter =
this->Internal->GroupChildren.begin();
const std::vector<cmSourceGroup>::const_iterator end =
this->Internal->GroupChildren.end();
// st
for (; iter != end; ++iter) {
std::string const& sgName = iter->GetName();
// look if descenened is the one were looking for // look if descenened is the one were looking for
if (sgName == name) { if (group.GetName() == name) {
return const_cast<cmSourceGroup*>(&(*iter)); // if it so return it return (&group); // if it so return it
} }
} }
@@ -117,19 +108,13 @@ cmSourceGroup* cmSourceGroup::LookupChild(const char* name) const
return nullptr; return nullptr;
} }
cmSourceGroup* cmSourceGroup::MatchChildrenFiles(const char* name) cmSourceGroup* cmSourceGroup::MatchChildrenFiles(const std::string& name)
{ {
// initializing iterators
std::vector<cmSourceGroup>::iterator iter =
this->Internal->GroupChildren.begin();
std::vector<cmSourceGroup>::iterator end =
this->Internal->GroupChildren.end();
if (this->MatchesFiles(name)) { if (this->MatchesFiles(name)) {
return this; return this;
} }
for (; iter != end; ++iter) { for (cmSourceGroup& group : this->Internal->GroupChildren) {
cmSourceGroup* result = iter->MatchChildrenFiles(name); cmSourceGroup* result = group.MatchChildrenFiles(name);
if (result) { if (result) {
return result; return result;
} }
@@ -137,16 +122,10 @@ cmSourceGroup* cmSourceGroup::MatchChildrenFiles(const char* name)
return nullptr; return nullptr;
} }
cmSourceGroup* cmSourceGroup::MatchChildrenRegex(const char* name) cmSourceGroup* cmSourceGroup::MatchChildrenRegex(const std::string& name)
{ {
// initializing iterators for (cmSourceGroup& group : this->Internal->GroupChildren) {
std::vector<cmSourceGroup>::iterator iter = cmSourceGroup* result = group.MatchChildrenRegex(name);
this->Internal->GroupChildren.begin();
std::vector<cmSourceGroup>::iterator end =
this->Internal->GroupChildren.end();
for (; iter != end; ++iter) {
cmSourceGroup* result = iter->MatchChildrenRegex(name);
if (result) { if (result) {
return result; return result;
} }

View File

@@ -26,7 +26,7 @@ class cmSourceGroupInternals;
class cmSourceGroup class cmSourceGroup
{ {
public: public:
cmSourceGroup(const char* name, const char* regex, cmSourceGroup(const std::string& name, const char* regex,
const char* parentName = nullptr); const char* parentName = nullptr);
cmSourceGroup(cmSourceGroup const& r); cmSourceGroup(cmSourceGroup const& r);
~cmSourceGroup(); ~cmSourceGroup();
@@ -50,7 +50,7 @@ public:
/** /**
* Looks up child and returns it * Looks up child and returns it
*/ */
cmSourceGroup* LookupChild(const char* name) const; cmSourceGroup* LookupChild(const std::string& name);
/** /**
* Get the name of this group. * Get the name of this group.
@@ -65,23 +65,23 @@ public:
/** /**
* Check if the given name matches this group's regex. * Check if the given name matches this group's regex.
*/ */
bool MatchesRegex(const char* name); bool MatchesRegex(const std::string& name);
/** /**
* Check if the given name matches this group's explicit file list. * Check if the given name matches this group's explicit file list.
*/ */
bool MatchesFiles(const char* name); bool MatchesFiles(const std::string& name) const;
/** /**
* Check if the given name matches this group's explicit file list * Check if the given name matches this group's explicit file list
* in children. * in children.
*/ */
cmSourceGroup* MatchChildrenFiles(const char* name); cmSourceGroup* MatchChildrenFiles(const std::string& name);
/** /**
* Check if the given name matches this group's regex in children. * Check if the given name matches this group's regex in children.
*/ */
cmSourceGroup* MatchChildrenRegex(const char* name); cmSourceGroup* MatchChildrenRegex(const std::string& name);
/** /**
* Assign the given source file to this group. Used only by * Assign the given source file to this group. Used only by