summaryrefslogtreecommitdiffstats
path: root/Source/CPack
diff options
context:
space:
mode:
authorMarc Chevrier <marc.chevrier@gmail.com>2020-03-02 09:41:17 (GMT)
committerMarc Chevrier <marc.chevrier@gmail.com>2020-03-08 15:32:44 (GMT)
commit44867a8c013a2ae6b828ef12af8e81cb73795483 (patch)
treec81f902ed5de798ea88a6c4e5bd1f7d57c1936ee /Source/CPack
parent1bb7474abac4eb3aa7dd1b52d9d09d2205e104f3 (diff)
downloadCMake-44867a8c013a2ae6b828ef12af8e81cb73795483.zip
CMake-44867a8c013a2ae6b828ef12af8e81cb73795483.tar.gz
CMake-44867a8c013a2ae6b828ef12af8e81cb73795483.tar.bz2
Modernize memory management
Update internals of various classes.
Diffstat (limited to 'Source/CPack')
-rw-r--r--Source/CPack/OSXScriptLauncher.cxx16
-rw-r--r--Source/CPack/WiX/cmCPackWIXGenerator.cxx13
-rw-r--r--Source/CPack/WiX/cmCPackWIXGenerator.h5
-rw-r--r--Source/CPack/WiX/cmWIXPatch.cxx2
-rw-r--r--Source/CPack/WiX/cmWIXPatchParser.cxx22
-rw-r--r--Source/CPack/WiX/cmWIXPatchParser.h8
-rw-r--r--Source/CPack/cmCPackLog.cxx40
-rw-r--r--Source/CPack/cmCPackLog.h21
8 files changed, 58 insertions, 69 deletions
diff --git a/Source/CPack/OSXScriptLauncher.cxx b/Source/CPack/OSXScriptLauncher.cxx
index 21d27a0..bdaf779 100644
--- a/Source/CPack/OSXScriptLauncher.cxx
+++ b/Source/CPack/OSXScriptLauncher.cxx
@@ -5,6 +5,8 @@
#include <string>
#include <vector>
+#include <cm/memory>
+
#include <CoreFoundation/CoreFoundation.h>
#include "cmsys/FStream.hxx"
@@ -26,7 +28,6 @@ int main(int argc, char* argv[])
CFStringRef fileName;
CFBundleRef appBundle;
CFURLRef scriptFileURL;
- UInt8* path;
// get CF URL for script
if (!(appBundle = CFBundleGetMainBundle())) {
@@ -41,13 +42,15 @@ int main(int argc, char* argv[])
}
// create path string
- if (!(path = new UInt8[PATH_MAX])) {
+ auto path = cm::make_unique<UInt8[]>(PATH_MAX);
+ if (!path) {
return 1;
}
// get the file system path of the url as a cstring
// in an encoding suitable for posix apis
- if (!CFURLGetFileSystemRepresentation(scriptFileURL, true, path, PATH_MAX)) {
+ if (!CFURLGetFileSystemRepresentation(scriptFileURL, true, path.get(),
+ PATH_MAX)) {
DebugError("CFURLGetFileSystemRepresentation failed");
return 1;
}
@@ -55,10 +58,10 @@ int main(int argc, char* argv[])
// dispose of the CF variable
CFRelease(scriptFileURL);
- std::string fullScriptPath = reinterpret_cast<char*>(path);
- delete[] path;
+ std::string fullScriptPath = reinterpret_cast<char*>(path.get());
+ path.reset();
- if (!cmsys::SystemTools::FileExists(fullScriptPath.c_str())) {
+ if (!cmsys::SystemTools::FileExists(fullScriptPath)) {
return 1;
}
@@ -80,7 +83,6 @@ int main(int argc, char* argv[])
cmsysProcess_SetTimeout(cp, 0);
cmsysProcess_Execute(cp);
- std::vector<char> tempOutput;
char* data;
int length;
while (cmsysProcess_WaitForData(cp, &data, &length, nullptr)) {
diff --git a/Source/CPack/WiX/cmCPackWIXGenerator.cxx b/Source/CPack/WiX/cmCPackWIXGenerator.cxx
index e71a38f..67d3d32 100644
--- a/Source/CPack/WiX/cmCPackWIXGenerator.cxx
+++ b/Source/CPack/WiX/cmCPackWIXGenerator.cxx
@@ -4,6 +4,7 @@
#include <algorithm>
+#include <cm/memory>
#include <cm/string_view>
#include "cmsys/Directory.hxx"
@@ -35,22 +36,16 @@
#include "cmCMakeToWixPath.h"
cmCPackWIXGenerator::cmCPackWIXGenerator()
- : Patch(0)
- , ComponentGuidType(cmWIXSourceWriter::WIX_GENERATED_GUID)
+ : ComponentGuidType(cmWIXSourceWriter::WIX_GENERATED_GUID)
{
}
-cmCPackWIXGenerator::~cmCPackWIXGenerator()
-{
- if (this->Patch) {
- delete this->Patch;
- }
-}
+cmCPackWIXGenerator::~cmCPackWIXGenerator() = default;
int cmCPackWIXGenerator::InitializeInternal()
{
componentPackageMethod = ONE_PACKAGE;
- this->Patch = new cmWIXPatch(this->Logger);
+ this->Patch = cm::make_unique<cmWIXPatch>(this->Logger);
return this->Superclass::InitializeInternal();
}
diff --git a/Source/CPack/WiX/cmCPackWIXGenerator.h b/Source/CPack/WiX/cmCPackWIXGenerator.h
index d193348..d5a16ec 100644
--- a/Source/CPack/WiX/cmCPackWIXGenerator.h
+++ b/Source/CPack/WiX/cmCPackWIXGenerator.h
@@ -4,6 +4,7 @@
#define cmCPackWIXGenerator_h
#include <map>
+#include <memory>
#include <string>
#include "cmCPackGenerator.h"
@@ -24,6 +25,8 @@ public:
cmCPackTypeMacro(cmCPackWIXGenerator, cmCPackGenerator);
cmCPackWIXGenerator();
+ cmCPackWIXGenerator(const cmCPackWIXGenerator&) = delete;
+ const cmCPackWIXGenerator& operator=(const cmCPackWIXGenerator&) = delete;
~cmCPackWIXGenerator();
protected:
@@ -157,7 +160,7 @@ private:
std::string CPackTopLevel;
- cmWIXPatch* Patch;
+ std::unique_ptr<cmWIXPatch> Patch;
cmWIXSourceWriter::GuidType ComponentGuidType;
};
diff --git a/Source/CPack/WiX/cmWIXPatch.cxx b/Source/CPack/WiX/cmWIXPatch.cxx
index ca232f9..122ffaf 100644
--- a/Source/CPack/WiX/cmWIXPatch.cxx
+++ b/Source/CPack/WiX/cmWIXPatch.cxx
@@ -41,7 +41,7 @@ void cmWIXPatch::ApplyFragment(std::string const& id,
void cmWIXPatch::ApplyElementChildren(const cmWIXPatchElement& element,
cmWIXSourceWriter& writer)
{
- for (cmWIXPatchNode* node : element.children) {
+ for (const auto& node : element.children) {
switch (node->type()) {
case cmWIXPatchNode::ELEMENT:
ApplyElement(dynamic_cast<const cmWIXPatchElement&>(*node), writer);
diff --git a/Source/CPack/WiX/cmWIXPatchParser.cxx b/Source/CPack/WiX/cmWIXPatchParser.cxx
index fd9103b..5588d2d 100644
--- a/Source/CPack/WiX/cmWIXPatchParser.cxx
+++ b/Source/CPack/WiX/cmWIXPatchParser.cxx
@@ -2,6 +2,10 @@
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmWIXPatchParser.h"
+#include <utility>
+
+#include <cm/memory>
+
#include "cm_expat.h"
#include "cmCPackGenerator.h"
@@ -20,12 +24,8 @@ cmWIXPatchNode::~cmWIXPatchNode()
{
}
-cmWIXPatchElement::~cmWIXPatchElement()
-{
- for (cmWIXPatchNode* child : children) {
- delete child;
- }
-}
+cmWIXPatchElement::cmWIXPatchElement() = default;
+cmWIXPatchElement::~cmWIXPatchElement() = default;
cmWIXPatchParser::cmWIXPatchParser(fragment_map_t& fragments,
cmCPackLog* logger)
@@ -54,8 +54,7 @@ void cmWIXPatchParser::StartElement(const std::string& name, const char** atts)
} else if (State == INSIDE_FRAGMENT) {
cmWIXPatchElement& parent = *ElementStack.back();
- cmWIXPatchElement* element = new cmWIXPatchElement;
- parent.children.push_back(element);
+ auto element = cm::make_unique<cmWIXPatchElement>();
element->name = name;
@@ -66,7 +65,8 @@ void cmWIXPatchParser::StartElement(const std::string& name, const char** atts)
element->attributes[key] = value;
}
- ElementStack.push_back(element);
+ ElementStack.push_back(element.get());
+ parent.children.push_back(std::move(element));
}
}
@@ -130,10 +130,10 @@ void cmWIXPatchParser::CharacterDataHandler(const char* data, int length)
std::string::size_type last = text.find_last_not_of(whitespace);
if (first != std::string::npos && last != std::string::npos) {
- cmWIXPatchText* text_node = new cmWIXPatchText;
+ auto text_node = cm::make_unique<cmWIXPatchText>();
text_node->text = text.substr(first, last - first + 1);
- parent.children.push_back(text_node);
+ parent.children.push_back(std::move(text_node));
}
}
}
diff --git a/Source/CPack/WiX/cmWIXPatchParser.h b/Source/CPack/WiX/cmWIXPatchParser.h
index 87dd892..8d5d2ad 100644
--- a/Source/CPack/WiX/cmWIXPatchParser.h
+++ b/Source/CPack/WiX/cmWIXPatchParser.h
@@ -4,6 +4,7 @@
#define cmCPackWIXPatchParser_h
#include <map>
+#include <memory>
#include <vector>
#include "cmCPackLog.h"
@@ -33,9 +34,14 @@ struct cmWIXPatchElement : cmWIXPatchNode
{
virtual Type type();
+ cmWIXPatchElement();
+
+ cmWIXPatchElement(const cmWIXPatchElement&) = delete;
+ const cmWIXPatchElement& operator=(const cmWIXPatchElement&) = delete;
+
~cmWIXPatchElement();
- using child_list_t = std::vector<cmWIXPatchNode*>;
+ using child_list_t = std::vector<std::unique_ptr<cmWIXPatchNode>>;
using attributes_t = std::map<std::string, std::string>;
std::string name;
diff --git a/Source/CPack/cmCPackLog.cxx b/Source/CPack/cmCPackLog.cxx
index ca675fd..49e4113 100644
--- a/Source/CPack/cmCPackLog.cxx
+++ b/Source/CPack/cmCPackLog.cxx
@@ -4,54 +4,38 @@
#include <iostream>
+#include <cm/memory>
+
#include "cmGeneratedFileStream.h"
#include "cmSystemTools.h"
cmCPackLog::cmCPackLog()
{
- this->Verbose = false;
- this->Debug = false;
- this->Quiet = false;
- this->NewLine = true;
-
- this->LastTag = cmCPackLog::NOTAG;
this->DefaultOutput = &std::cout;
this->DefaultError = &std::cerr;
-
- this->LogOutput = nullptr;
- this->LogOutputCleanup = false;
}
-cmCPackLog::~cmCPackLog()
-{
- this->SetLogOutputStream(nullptr);
-}
+cmCPackLog::~cmCPackLog() = default;
void cmCPackLog::SetLogOutputStream(std::ostream* os)
{
- if (this->LogOutputCleanup && this->LogOutput) {
- delete this->LogOutput;
- }
- this->LogOutputCleanup = false;
+ this->LogOutputStream.reset();
this->LogOutput = os;
}
bool cmCPackLog::SetLogOutputFile(const char* fname)
{
- cmGeneratedFileStream* cg = nullptr;
+ this->LogOutputStream.reset();
if (fname) {
- cg = new cmGeneratedFileStream(fname);
- }
- if (cg && !*cg) {
- delete cg;
- cg = nullptr;
+ this->LogOutputStream = cm::make_unique<cmGeneratedFileStream>(fname);
}
- this->SetLogOutputStream(cg);
- if (!cg) {
- return false;
+ if (this->LogOutputStream && !*this->LogOutputStream) {
+ this->LogOutputStream.reset();
}
- this->LogOutputCleanup = true;
- return true;
+
+ this->LogOutput = this->LogOutputStream.get();
+
+ return this->LogOutput != nullptr;
}
void cmCPackLog::Log(int tag, const char* file, int line, const char* msg,
diff --git a/Source/CPack/cmCPackLog.h b/Source/CPack/cmCPackLog.h
index 1cb1643..68ffcce 100644
--- a/Source/CPack/cmCPackLog.h
+++ b/Source/CPack/cmCPackLog.h
@@ -5,6 +5,7 @@
#include "cmConfigure.h" // IWYU pragma: keep
+#include <memory>
#include <ostream>
#include <string>
@@ -97,13 +98,13 @@ public:
void SetErrorPrefix(std::string const& pfx) { this->ErrorPrefix = pfx; }
private:
- bool Verbose;
- bool Debug;
- bool Quiet;
+ bool Verbose = false;
+ bool Debug = false;
+ bool Quiet = false;
- bool NewLine;
+ bool NewLine = true;
- int LastTag;
+ int LastTag = cmCPackLog::NOTAG;
std::string Prefix;
std::string OutputPrefix;
@@ -112,13 +113,11 @@ private:
std::string WarningPrefix;
std::string ErrorPrefix;
- std::ostream* DefaultOutput;
- std::ostream* DefaultError;
+ std::ostream* DefaultOutput = nullptr;
+ std::ostream* DefaultError = nullptr;
- std::string LogOutputFileName;
- std::ostream* LogOutput;
- // Do we need to cleanup log output stream
- bool LogOutputCleanup;
+ std::ostream* LogOutput = nullptr;
+ std::unique_ptr<std::ostream> LogOutputStream;
};
class cmCPackLogWrite