Browse Source

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

Topic-rename: cmSourceGroup-modern-cxx
Vitaly Stakhovsky 7 years ago
parent
commit
969c1f94ae
3 changed files with 24 additions and 46 deletions
  1. 4 5
      Source/cmMakefile.cxx
  2. 14 35
      Source/cmSourceGroup.cxx
  3. 6 6
      Source/cmSourceGroup.h

+ 4 - 5
Source/cmMakefile.cxx

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

+ 14 - 35
Source/cmSourceGroup.cxx

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

+ 6 - 6
Source/cmSourceGroup.h

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