Fixed memory leaks

This commit is contained in:
Leonetienne 2021-06-02 17:14:53 +02:00
parent c412e879a1
commit 131520e1ef
15 changed files with 84 additions and 56 deletions

Binary file not shown.

View File

@ -6,6 +6,7 @@ class FloatValue : public Value
{
public:
FloatValue(const long double& value);
~FloatValue() override {};
//! Will return a deeopopy of this object
Value* Deepcopy() const override;

View File

@ -5,7 +5,6 @@
#include "StringValue.h"
#include "ListValue.h"
#include "StringTools.h"
#include <iostream>
Hazelnupp::Hazelnupp()
{
@ -18,6 +17,16 @@ Hazelnupp::Hazelnupp(const int argc, const char* const* argv)
return;
}
Hazelnupp::~Hazelnupp()
{
for (auto& it : parameters)
delete it.second;
parameters.clear();
return;
}
void Hazelnupp::Parse(const int argc, const char* const* argv)
{
// Populate raw arguments
@ -42,8 +51,6 @@ void Hazelnupp::Parse(const int argc, const char* const* argv)
i++;
}
std::cout << "Terminated." << std::endl;
return;
}
@ -64,17 +71,24 @@ std::size_t Hazelnupp::ParseNextParameter(const std::size_t parIndex, Parameter*
}
Value* parsedVal = ParseValue(values);
out_Par = new Parameter(key, parsedVal);
if (parsedVal != nullptr)
{
out_Par = new Parameter(key, parsedVal);
delete parsedVal;
parsedVal = nullptr;
delete parsedVal;
parsedVal = nullptr;
}
else
throw std::runtime_error("Unable to parse parameter!");
return i;
}
void Hazelnupp::PopulateRawArgs(const int argc, const char* const* argv)
{
rawArgs.clear();
rawArgs.reserve(argc);
for (int i = 0; i < argc; i++)
rawArgs.emplace_back(std::string(argv[i]));

View File

@ -9,6 +9,8 @@ public:
Hazelnupp();
Hazelnupp(const int argc, const char* const* argv);
~Hazelnupp();
//! Will parse command line arguments
void Parse(const int argc, const char* const* argv);
@ -51,7 +53,4 @@ private:
std::unordered_map<std::string, std::string> abbreviations;
std::vector<std::string> rawArgs;
// EvaluateValues
friend class ListValue;
};

View File

@ -5,6 +5,7 @@ class IntValue : public Value
{
public:
IntValue(const long long int& value);
~IntValue() override {};
//! Will return a deeopopy of this object
Value* Deepcopy() const override;

View File

@ -1,5 +1,4 @@
#include "ListValue.h"
#include "Hazelnupp.h"
#include <sstream>
ListValue::ListValue() :

View File

@ -6,7 +6,7 @@ class ListValue : public Value
{
public:
ListValue();
~ListValue();
~ListValue() override;
//! Will return a deeopopy of this object
Value* Deepcopy() const override;

View File

@ -8,6 +8,14 @@ Parameter::Parameter(const std::string& key, const ::Value* value)
return;
}
Parameter::~Parameter()
{
delete value;
value = nullptr;
return;
}
const std::string& Parameter::Key() const
{
return key;

View File

@ -7,6 +7,7 @@ class Parameter
{
public:
explicit Parameter(const std::string& key, const Value* value);
~Parameter();
//! Will return the key of this parameter
const std::string& Key() const;

View File

@ -9,7 +9,7 @@ bool StringTools::Contains(const std::string& str, const char c)
return false;
}
std::string StringTools::Replace(const std::string str, const char find, const std::string subst)
std::string StringTools::Replace(const std::string& str, const char find, const std::string& subst)
{
std::stringstream ss;
@ -22,7 +22,7 @@ std::string StringTools::Replace(const std::string str, const char find, const s
return ss.str();
}
std::string StringTools::Replace(const std::string str, const std::string find, const std::string subst)
std::string StringTools::Replace(const std::string& str, const std::string& find, const std::string& subst)
{
if (find.length() == 0) return str;
@ -51,7 +51,7 @@ std::string StringTools::Replace(const std::string str, const std::string find,
}
bool StringTools::IsNumeric(const std::string str, const bool allowDecimalPoint)
bool StringTools::IsNumeric(const std::string& str, const bool allowDecimalPoint)
{
if (str.length() == 0) return false;
@ -77,7 +77,7 @@ bool StringTools::IsNumeric(const std::string str, const bool allowDecimalPoint)
return digitCount > 0;
}
bool StringTools::ParseNumber(const std::string str, bool& out_isInt, long double& out_number)
bool StringTools::ParseNumber(const std::string& str, bool& out_isInt, long double& out_number)
{
bool isNormalNum = true;
@ -124,14 +124,14 @@ bool StringTools::ParseNumber(const std::string str, bool& out_isInt, long doubl
return true;
}
std::vector<std::string> StringTools::SplitString(const std::string str, const char delimiter)
std::vector<std::string> StringTools::SplitString(const std::string& str, const char delimiter)
{
if (str.length() == 0) return std::vector<std::string>();
return SplitString(str, delimiter);
}
std::vector<std::string> StringTools::SplitString(const std::string str, const std::string delimiter)
std::vector<std::string> StringTools::SplitString(const std::string& str, const std::string& delimiter)
{
if (str.length() == 0) return std::vector<std::string>();
@ -172,7 +172,7 @@ std::vector<std::string> StringTools::SplitString(const std::string str, const s
return parts;
}
std::string StringTools::ToLower(const std::string str)
std::string StringTools::ToLower(const std::string& str)
{
std::stringstream ss;
for (std::size_t i = 0; i < str.length(); i++)

View File

@ -14,25 +14,25 @@ public:
static bool Contains(const std::string& str, const char c);
//! Will replace a part of a string with another string
static std::string Replace(const std::string str, const char find, const std::string subst);
static std::string Replace(const std::string& str, const char find, const std::string& subst);
//! Will replace a part of a string with another string
static std::string Replace(const std::string str, const std::string find, const std::string subst);
static std::string Replace(const std::string& str, const std::string& find, const std::string& subst);
//! Will return true if the given string consists only of digits (including signage)
static bool IsNumeric(const std::string str, const bool allowDecimalPoint = false);
static bool IsNumeric(const std::string& str, const bool allowDecimalPoint = false);
//! Will convert the number in str to a number.
//! Returns wether or not the operation was successful.
//! Also returns wether the number is an integer, or floating point. If int, cast out_number to int.
static bool ParseNumber(const std::string str, bool& out_isInt, long double& out_number);
static bool ParseNumber(const std::string& str, bool& out_isInt, long double& out_number);
//! Will split a string by a delimiter char. The delimiter will be excluded!
static std::vector<std::string> SplitString(const std::string str, const char delimiter);
static std::vector<std::string> SplitString(const std::string& str, const char delimiter);
//! Will split a string by a delimiter string. The delimiter will be excluded!
static std::vector<std::string> SplitString(const std::string str, const std::string delimiter);
static std::vector<std::string> SplitString(const std::string& str, const std::string& delimiter);
//! Will make a string all lower-case
static std::string ToLower(const std::string str);
static std::string ToLower(const std::string& str);
};

View File

@ -6,6 +6,7 @@ class StringValue : public Value
{
public:
StringValue(const std::string& value);
~StringValue() override {};
//! Will return a deeopopy of this object
Value* Deepcopy() const override;

View File

@ -6,6 +6,8 @@
class Value
{
public:
virtual ~Value() {};
//! Will return a deeopopy of this object
virtual Value* Deepcopy() const = 0;

View File

@ -5,6 +5,7 @@ class VoidValue : public Value
{
public:
VoidValue();
~VoidValue() override {};
//! Will return a deeopopy of this object
Value* Deepcopy() const override;

View File

@ -1,42 +1,43 @@
#include <iostream>
#include <array>
#include "Hazelnupp.h"
#include "IntValue.h"
int main(int argc, char** argv)
{
std::string arg0 = "meinpfad";
std::string arg1 = "-w";
std::string arg2 = "6669";
std::string arg3 = "--alfred";
std::string arg4 = "banane7";
std::array<const char*, 5> testArgv = {
arg0.data(),
arg1.data(),
arg2.data(),
arg3.data(),
arg4.data()
};
Hazelnupp args;
args.RegisterAbbreviation("-w", "--word");
//args.Parse(testArgv.size(), testArgv.data());
args.Parse(argc, argv);
if (args.HasParam("--word"))
while (1)
{
int i = args["--word"]->GetInt32();
std::cout << i << std::endl;
}
else
{
std::cout << "No --word!" << std::endl;
}
std::vector<const char*> testArgv = {
"meinpfad",
"-w",
"hallo",
"--alfred",
"apfel",
"banane",
"triangle",
"--numbers",
"1",
"2",
"3",
"4",
"5",
};
//std::cout << args.GetExecutableName() << std::endl;
Hazelnupp args;
args.RegisterAbbreviation("-w", "--word");
args.Parse(testArgv.size(), testArgv.data());
//args.Parse(argc, argv);
if (args.HasParam("--word"))
{
std::cout << *args["--word"] << std::endl;
}
else
{
std::cout << "No --word!" << std::endl;
}
}
return 0;
}