1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-10-15 07:01:18 +02:00

Remove some naked new amd delete in: commands

This commit is contained in:
Paul Licameli
2016-08-07 14:56:54 -04:00
parent c3e53ea26e
commit 57afa1399e
9 changed files with 58 additions and 70 deletions

View File

@@ -39,36 +39,30 @@ CommandDirectory::CommandDirectory()
{
// Create the command map.
// Adding an entry here is the easiest way to register a Command class.
AddCommand(new ScreenshotCommandType());
AddCommand(new BatchEvalCommandType());
AddCommand(new ExecMenuCommandType());
AddCommand(new GetAllMenuCommandsType());
AddCommand(new MessageCommandType());
AddCommand(new GetTrackInfoCommandType());
AddCommand(new GetProjectInfoCommandType());
AddCommand(make_movable<ScreenshotCommandType>());
AddCommand(make_movable<BatchEvalCommandType>());
AddCommand(make_movable<ExecMenuCommandType>());
AddCommand(make_movable<GetAllMenuCommandsType>());
AddCommand(make_movable<MessageCommandType>());
AddCommand(make_movable<GetTrackInfoCommandType>());
AddCommand(make_movable<GetProjectInfoCommandType>());
AddCommand(new HelpCommandType());
AddCommand(new SelectCommandType());
AddCommand(new CompareAudioCommandType());
AddCommand(new SetTrackInfoCommandType());
AddCommand(new SetProjectInfoCommandType());
AddCommand(make_movable<HelpCommandType>());
AddCommand(make_movable<SelectCommandType>());
AddCommand(make_movable<CompareAudioCommandType>());
AddCommand(make_movable<SetTrackInfoCommandType>());
AddCommand(make_movable<SetProjectInfoCommandType>());
AddCommand(new SetPreferenceCommandType());
AddCommand(new GetPreferenceCommandType());
AddCommand(new ImportCommandType());
AddCommand(new ExportCommandType());
AddCommand(new OpenProjectCommandType());
AddCommand(new SaveProjectCommandType());
AddCommand(make_movable<SetPreferenceCommandType>());
AddCommand(make_movable<GetPreferenceCommandType>());
AddCommand(make_movable<ImportCommandType>());
AddCommand(make_movable<ExportCommandType>());
AddCommand(make_movable<OpenProjectCommandType>());
AddCommand(make_movable<SaveProjectCommandType>());
}
CommandDirectory::~CommandDirectory()
{
// Delete the factories
CommandMap::iterator iter;
for (iter = mCmdMap.begin(); iter != mCmdMap.end(); ++iter)
{
delete iter->second;
}
}
CommandType *CommandDirectory::LookUp(const wxString &cmdName) const
@@ -78,10 +72,10 @@ CommandType *CommandDirectory::LookUp(const wxString &cmdName) const
{
return NULL;
}
return iter->second;
return iter->second.get();
}
void CommandDirectory::AddCommand(CommandType *type)
void CommandDirectory::AddCommand(movable_ptr<CommandType> &&type)
{
wxASSERT(type != NULL);
wxString cmdName = type->GetName();
@@ -89,7 +83,7 @@ void CommandDirectory::AddCommand(CommandType *type)
, wxT("A command named ") + cmdName
+ wxT(" already exists."));
mCmdMap[cmdName] = type;
mCmdMap[cmdName] = std::move(type);
}
CommandDirectory *CommandDirectory::Get()

View File

@@ -44,7 +44,7 @@ public:
CommandType *LookUp(const wxString &cmdName) const;
/// Register a type of command with the directory.
void AddCommand(CommandType *type);
void AddCommand(movable_ptr<CommandType> &&type);
/// Get a pointer to the singleton instance
static CommandDirectory *Get();

View File

@@ -16,20 +16,21 @@
*//*******************************************************************/
#include <wx/event.h>
#include "../Audacity.h"
#include "CommandHandler.h"
#include <wx/event.h>
#include "../Project.h"
#include "Command.h"
#include "AppCommandEvent.h"
#include "ScriptCommandRelay.h"
CommandHandler::CommandHandler(AudacityApp &app)
: mCurrentContext(new CommandExecutionContext(&app, GetActiveProject()))
: mCurrentContext(std::make_unique<CommandExecutionContext>
(&app, GetActiveProject()))
{ }
CommandHandler::~CommandHandler()
{
delete mCurrentContext;
}
void CommandHandler::SetProject(AudacityProject *)

View File

@@ -16,6 +16,7 @@
#ifndef __COMMANDHANDLER__
#define __COMMANDHANDLER__
#include "../MemoryX.h"
#include "../AudacityApp.h"
class AudacityProject;
class AppCommandEvent;
@@ -24,7 +25,7 @@ class CommandExecutionContext;
class CommandHandler
{
private:
CommandExecutionContext *mCurrentContext;
std::unique_ptr<CommandExecutionContext> mCurrentContext;
public:
CommandHandler(AudacityApp &app);

View File

@@ -93,8 +93,8 @@ class AUDACITY_DLL_API CommandManager final : public XMLTagHandler
CommandManager();
virtual ~CommandManager();
CommandManager(const CommandManager&) = delete;
CommandManager &operator= (const CommandManager&) = delete;
CommandManager(const CommandManager&) PROHIBITED;
CommandManager &operator= (const CommandManager&) PROHIBITED;
void PurgeData();

View File

@@ -23,6 +23,7 @@
class CommandType;
// Map from parameter name to the value of the parameter
// to do: use hash
typedef std::map<wxString, wxVariant> ParamValueMap;
// Map from parameter name to a suitable Validator
@@ -30,6 +31,7 @@ typedef std::map<wxString, wxVariant> ParamValueMap;
typedef std::map<wxString, movable_ptr<Validator>> ValidatorMap;
// Map from command name to type
typedef std::map<wxString, CommandType*> CommandMap;
// to do: use hash
typedef std::map<wxString, movable_ptr<CommandType>> CommandMap;
#endif /* End of include guard: __COMMANDMISC__ */

View File

@@ -47,7 +47,7 @@ public:
};
/// Sends command progress information to a ProgressDialog
class GUIProgressTarget : public CommandProgressTarget
class GUIProgressTarget final : public CommandProgressTarget
{
private:
ProgressDialog &mProgress;
@@ -74,18 +74,17 @@ public:
class ProgressToMessageTarget final : public CommandProgressTarget
{
private:
CommandMessageTarget &mTarget;
std::unique_ptr<CommandMessageTarget> mTarget;
public:
ProgressToMessageTarget(CommandMessageTarget *target)
: mTarget(*target)
ProgressToMessageTarget(std::unique_ptr<CommandMessageTarget> &&target)
: mTarget(std::move(target))
{ }
virtual ~ProgressToMessageTarget()
{
// delete &mTarget;
}
void Update(double completed) override
{
mTarget.Update(wxString::Format(wxT("%.2f%%"), completed*100));
mTarget->Update(wxString::Format(wxT("%.2f%%"), completed*100));
}
};
@@ -94,7 +93,7 @@ class NullMessageTarget final : public CommandMessageTarget
{
public:
virtual ~NullMessageTarget() {}
void Update(const wxString &message) override {}
void Update(const wxString &) override {}
};
/// Displays messages from a command in a wxMessageBox
@@ -146,18 +145,17 @@ public:
class CombinedMessageTarget final : public CommandMessageTarget
{
private:
CommandMessageTarget *m1, *m2;
std::unique_ptr<CommandMessageTarget> m1, m2;
public:
CombinedMessageTarget(CommandMessageTarget *t1, CommandMessageTarget *t2)
: m1(t1), m2(t2)
CombinedMessageTarget(std::unique_ptr<CommandMessageTarget> &&t1,
std::unique_ptr<CommandMessageTarget> &&t2)
: m1(std::move(t1)), m2(std::move(t2))
{
wxASSERT(t1 != NULL);
wxASSERT(t2 != NULL);
wxASSERT(m1);
wxASSERT(m2);
}
~CombinedMessageTarget()
{
delete m1;
delete m2;
}
void Update(const wxString &message) override
{
@@ -167,7 +165,8 @@ public:
};
// By default, we ignore progress updates but display all other messages directly
// By default, we ignore progress updates but display all other messages
// directly
class TargetFactory
{
public:

View File

@@ -24,35 +24,25 @@ Also acts as a factory.
#include <wx/string.h>
CommandType::CommandType()
: mName(NULL), mSignature(NULL)
: mName{}, mSignature{}
{ }
CommandType::~CommandType()
{
if (mName != NULL)
{
delete mName;
}
if (mSignature != NULL)
{
delete mSignature;
}
}
wxString CommandType::GetName()
const wxString &CommandType::GetName()
{
if (mName == NULL)
{
mName = new wxString(BuildName());
}
return *mName;
if (mName.empty())
mName = BuildName();
return mName;
}
CommandSignature &CommandType::GetSignature()
{
if (mSignature == NULL)
if (!mSignature)
{
mSignature = new CommandSignature();
mSignature.create();
BuildSignature(*mSignature);
}
return *mSignature;

View File

@@ -17,6 +17,7 @@
#define __COMMANDTYPE__
#include "CommandMisc.h"
#include "CommandSignature.h"
#include "../MemoryX.h"
class Command;
@@ -28,13 +29,13 @@ class wxString;
class CommandType /* not final */
{
private:
wxString *mName;
CommandSignature *mSignature;
wxString mName;
Maybe<CommandSignature> mSignature;
public:
CommandType();
virtual ~CommandType();
wxString GetName();
const wxString &GetName();
CommandSignature &GetSignature();
wxString Describe();