cmDocumentation: Get rid of raw pointers in AllSections map

Also simplify a lot of logic around adding sections into it.
Prefer move sematics over references.
This commit is contained in:
Artur Ryt
2018-11-30 20:08:50 +01:00
committed by Brad King
parent 4308eb3d16
commit 57862079d8
2 changed files with 40 additions and 84 deletions
+35 -78
View File
@@ -2,7 +2,6 @@
file Copyright.txt or https://cmake.org/licensing for details. */ file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmDocumentation.h" #include "cmDocumentation.h"
#include "cmAlgorithms.h"
#include "cmDocumentationEntry.h" #include "cmDocumentationEntry.h"
#include "cmDocumentationSection.h" #include "cmDocumentationSection.h"
#include "cmRST.h" #include "cmRST.h"
@@ -55,11 +54,6 @@ cmDocumentation::cmDocumentation()
this->ShowGenerators = true; this->ShowGenerators = true;
} }
cmDocumentation::~cmDocumentation()
{
cmDeleteAll(this->AllSections);
}
bool cmDocumentation::PrintVersion(std::ostream& os) bool cmDocumentation::PrintVersion(std::ostream& os)
{ {
/* clang-format off */ /* clang-format off */
@@ -174,20 +168,16 @@ void cmDocumentation::WarnFormFromFilename(
void cmDocumentation::addCommonStandardDocSections() void cmDocumentation::addCommonStandardDocSections()
{ {
cmDocumentationSection* sec; cmDocumentationSection sec{ "Options" };
sec.Append(cmDocumentationStandardOptions);
sec = new cmDocumentationSection("Options"); this->AllSections.emplace("Options", std::move(sec));
sec->Append(cmDocumentationStandardOptions);
this->AllSections["Options"] = sec;
} }
void cmDocumentation::addCMakeStandardDocSections() void cmDocumentation::addCMakeStandardDocSections()
{ {
cmDocumentationSection* sec; cmDocumentationSection sec{ "Generators" };
sec.Append(cmDocumentationGeneratorsHeader);
sec = new cmDocumentationSection("Generators"); this->AllSections.emplace("Generators", std::move(sec));
sec->Append(cmDocumentationGeneratorsHeader);
this->AllSections["Generators"] = sec;
} }
void cmDocumentation::addCTestStandardDocSections() void cmDocumentation::addCTestStandardDocSections()
@@ -199,11 +189,7 @@ void cmDocumentation::addCTestStandardDocSections()
void cmDocumentation::addCPackStandardDocSections() void cmDocumentation::addCPackStandardDocSections()
{ {
cmDocumentationSection* sec; addCMakeStandardDocSections();
sec = new cmDocumentationSection("Generators");
sec->Append(cmDocumentationGeneratorsHeader);
this->AllSections["Generators"] = sec;
} }
bool cmDocumentation::CheckOptions(int argc, const char* const* argv, bool cmDocumentation::CheckOptions(int argc, const char* const* argv,
@@ -364,85 +350,59 @@ void cmDocumentation::SetName(const std::string& name)
} }
void cmDocumentation::SetSection(const char* name, void cmDocumentation::SetSection(const char* name,
cmDocumentationSection* section) cmDocumentationSection section)
{ {
if (this->AllSections.find(name) != this->AllSections.end()) { this->SectionAtName(name) = std::move(section);
delete this->AllSections[name];
}
this->AllSections[name] = section;
} }
void cmDocumentation::SetSection(const char* name, void cmDocumentation::SetSection(const char* name,
std::vector<cmDocumentationEntry>& docs) std::vector<cmDocumentationEntry>& docs)
{ {
cmDocumentationSection* sec = new cmDocumentationSection(name); cmDocumentationSection sec{ name };
sec->Append(docs); sec.Append(docs);
this->SetSection(name, sec); this->SetSection(name, std::move(sec));
} }
void cmDocumentation::SetSection(const char* name, const char* docs[][2]) void cmDocumentation::SetSection(const char* name, const char* docs[][2])
{ {
cmDocumentationSection* sec = new cmDocumentationSection(name); cmDocumentationSection sec{ name };
sec->Append(docs); sec.Append(docs);
this->SetSection(name, sec); this->SetSection(name, std::move(sec));
} }
void cmDocumentation::SetSections( void cmDocumentation::SetSections(
std::map<std::string, cmDocumentationSection*>& sections) std::map<std::string, cmDocumentationSection> sections)
{ {
for (auto const& s : sections) { for (auto& s : sections) {
this->SetSection(s.first.c_str(), s.second); this->SetSection(s.first.c_str(), std::move(s.second));
} }
} }
cmDocumentationSection& cmDocumentation::SectionAtName(const char* name)
{
return this->AllSections.emplace(name, cmDocumentationSection{ name })
.first->second;
}
void cmDocumentation::PrependSection(const char* name, const char* docs[][2]) void cmDocumentation::PrependSection(const char* name, const char* docs[][2])
{ {
cmDocumentationSection* sec = nullptr; this->SectionAtName(name).Prepend(docs);
if (this->AllSections.find(name) == this->AllSections.end()) {
sec = new cmDocumentationSection(name);
this->SetSection(name, sec);
} else {
sec = this->AllSections[name];
}
sec->Prepend(docs);
} }
void cmDocumentation::PrependSection(const char* name, void cmDocumentation::PrependSection(const char* name,
std::vector<cmDocumentationEntry>& docs) std::vector<cmDocumentationEntry>& docs)
{ {
cmDocumentationSection* sec = nullptr; this->SectionAtName(name).Prepend(docs);
if (this->AllSections.find(name) == this->AllSections.end()) {
sec = new cmDocumentationSection(name);
this->SetSection(name, sec);
} else {
sec = this->AllSections[name];
}
sec->Prepend(docs);
} }
void cmDocumentation::AppendSection(const char* name, const char* docs[][2]) void cmDocumentation::AppendSection(const char* name, const char* docs[][2])
{ {
cmDocumentationSection* sec = nullptr; this->SectionAtName(name).Append(docs);
if (this->AllSections.find(name) == this->AllSections.end()) {
sec = new cmDocumentationSection(name);
this->SetSection(name, sec);
} else {
sec = this->AllSections[name];
}
sec->Append(docs);
} }
void cmDocumentation::AppendSection(const char* name, void cmDocumentation::AppendSection(const char* name,
std::vector<cmDocumentationEntry>& docs) std::vector<cmDocumentationEntry>& docs)
{ {
cmDocumentationSection* sec = nullptr; this->SectionAtName(name).Append(docs);
if (this->AllSections.find(name) == this->AllSections.end()) {
sec = new cmDocumentationSection(name);
this->SetSection(name, sec);
} else {
sec = this->AllSections[name];
}
sec->Append(docs);
} }
void cmDocumentation::AppendSection(const char* name, void cmDocumentation::AppendSection(const char* name,
@@ -625,11 +585,10 @@ bool cmDocumentation::PrintHelpListPolicies(std::ostream& os)
bool cmDocumentation::PrintHelpListGenerators(std::ostream& os) bool cmDocumentation::PrintHelpListGenerators(std::ostream& os)
{ {
std::map<std::string, cmDocumentationSection*>::iterator si; const auto si = this->AllSections.find("Generators");
si = this->AllSections.find("Generators");
if (si != this->AllSections.end()) { if (si != this->AllSections.end()) {
this->Formatter.SetIndent(" "); this->Formatter.SetIndent(" ");
this->Formatter.PrintSection(os, *si->second); this->Formatter.PrintSection(os, si->second);
} }
return true; return true;
} }
@@ -655,29 +614,27 @@ bool cmDocumentation::PrintHelpListVariables(std::ostream& os)
bool cmDocumentation::PrintUsage(std::ostream& os) bool cmDocumentation::PrintUsage(std::ostream& os)
{ {
std::map<std::string, cmDocumentationSection*>::iterator si; const auto si = this->AllSections.find("Usage");
si = this->AllSections.find("Usage");
if (si != this->AllSections.end()) { if (si != this->AllSections.end()) {
this->Formatter.PrintSection(os, *si->second); this->Formatter.PrintSection(os, si->second);
} }
return true; return true;
} }
bool cmDocumentation::PrintHelp(std::ostream& os) bool cmDocumentation::PrintHelp(std::ostream& os)
{ {
std::map<std::string, cmDocumentationSection*>::iterator si; auto si = this->AllSections.find("Usage");
si = this->AllSections.find("Usage");
if (si != this->AllSections.end()) { if (si != this->AllSections.end()) {
this->Formatter.PrintSection(os, *si->second); this->Formatter.PrintSection(os, si->second);
} }
si = this->AllSections.find("Options"); si = this->AllSections.find("Options");
if (si != this->AllSections.end()) { if (si != this->AllSections.end()) {
this->Formatter.PrintSection(os, *si->second); this->Formatter.PrintSection(os, si->second);
} }
if (this->ShowGenerators) { if (this->ShowGenerators) {
si = this->AllSections.find("Generators"); si = this->AllSections.find("Generators");
if (si != this->AllSections.end()) { if (si != this->AllSections.end()) {
this->Formatter.PrintSection(os, *si->second); this->Formatter.PrintSection(os, si->second);
} }
} }
return true; return true;
+5 -6
View File
@@ -6,13 +6,13 @@
#include "cmConfigure.h" // IWYU pragma: keep #include "cmConfigure.h" // IWYU pragma: keep
#include "cmDocumentationFormatter.h" #include "cmDocumentationFormatter.h"
#include "cmDocumentationSection.h"
#include <iosfwd> #include <iosfwd>
#include <map> #include <map>
#include <string> #include <string>
#include <vector> #include <vector>
class cmDocumentationSection;
struct cmDocumentationEntry; struct cmDocumentationEntry;
/** Class to generate documentation. */ /** Class to generate documentation. */
@@ -21,8 +21,6 @@ class cmDocumentation : public cmDocumentationEnums
public: public:
cmDocumentation(); cmDocumentation();
~cmDocumentation();
/** /**
* Check command line arguments for documentation options. Returns * Check command line arguments for documentation options. Returns
* true if documentation options are found, and false otherwise. * true if documentation options are found, and false otherwise.
@@ -52,11 +50,11 @@ public:
/** Set a section of the documentation. Typical sections include Name, /** Set a section of the documentation. Typical sections include Name,
Usage, Description, Options */ Usage, Description, Options */
void SetSection(const char* sectionName, cmDocumentationSection* section); void SetSection(const char* sectionName, cmDocumentationSection section);
void SetSection(const char* sectionName, void SetSection(const char* sectionName,
std::vector<cmDocumentationEntry>& docs); std::vector<cmDocumentationEntry>& docs);
void SetSection(const char* sectionName, const char* docs[][2]); void SetSection(const char* sectionName, const char* docs[][2]);
void SetSections(std::map<std::string, cmDocumentationSection*>& sections); void SetSections(std::map<std::string, cmDocumentationSection> sections);
/** Add the documentation to the beginning/end of the section */ /** Add the documentation to the beginning/end of the section */
void PrependSection(const char* sectionName, const char* docs[][2]); void PrependSection(const char* sectionName, const char* docs[][2]);
@@ -110,7 +108,8 @@ private:
bool ShowGenerators; bool ShowGenerators;
std::string NameString; std::string NameString;
std::map<std::string, cmDocumentationSection*> AllSections; std::map<std::string, cmDocumentationSection> AllSections;
cmDocumentationSection& SectionAtName(const char* name);
std::string CurrentArgument; std::string CurrentArgument;