Merge pull request #1126 from turtleli/replace-scoped-array-with-unique-ptr

Replace ScopedArray with unique_ptr
This commit is contained in:
Gregory Hainaut 2016-01-19 16:22:13 +01:00
commit 71c440862c
10 changed files with 25 additions and 141 deletions

View File

@ -140,121 +140,6 @@ public:
} }
}; };
// --------------------------------------------------------------------------------------
// ScopedArray - same as ScopedPtr but uses delete[], and has operator[]
// --------------------------------------------------------------------------------------
template< typename T >
class ScopedArray
{
DeclareNoncopyableObject(ScopedArray);
protected:
T* m_array;
uint m_valid_range;
public:
typedef T element_type;
wxEXPLICIT ScopedArray(T * ptr = NULL)
{
m_array = ptr;
m_valid_range = 0xffffffff;
}
wxEXPLICIT ScopedArray( size_t size )
{
m_array = new T[size];
m_valid_range = size;
}
~ScopedArray() throw()
{ Delete(); }
ScopedArray& Reassign(T * ptr = NULL)
{
if( ptr != m_array )
{
Delete();
m_array = ptr;
m_valid_range = 0xffffffff;
}
return *this;
}
ScopedArray& Delete() throw()
{
// Thread-safe deletion: Set the pointer to NULL first, and then issue
// the deletion. This allows pending Application messages that might be
// dependent on the current object to nullify their actions.
T* deleteme = m_array;
m_array = NULL;
delete[] deleteme;
return *this;
}
// Removes the pointer from scoped management, but does not delete!
T *DetachPtr()
{
T *ptr = m_array;
m_array = NULL;
return ptr;
}
// Returns the managed pointer. Can return NULL as a valid result if the ScopedPtr
// has no object in management.
T* GetPtr() const
{
return m_array;
}
void SwapPtr(ScopedArray& other)
{
T * const tmp = other.m_array;
other.m_array = m_array;
m_array = tmp;
}
// ----------------------------------------------------------------------------
// ScopedPtr Operators
// ----------------------------------------------------------------------------
// I've decided to use the ATL's approach to pointer validity tests, opposed to
// the wx/boost approach (which uses some bizarre member method pointer crap, and can't
// allow the T* implicit casting.
bool operator!() const throw()
{
return m_array == NULL;
}
// Equality
bool operator==(T* pT) const throw()
{
return m_array == pT;
}
// Inequality
bool operator!=(T* pT) const throw()
{
return !operator==(pT);
}
// Convenient assignment operator. ScopedPtr = NULL will issue an automatic deletion
// of the managed pointer.
ScopedArray& operator=( T* src )
{
return Reassign( src );
}
T& operator[]( uint idx ) const
{
pxAssertDev( idx < m_valid_range, "Array index out of bounds on ScopedArray." );
return m_array[idx];
}
};
// -------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------
// pxObjPtr -- fancified version of wxScopedPtr // pxObjPtr -- fancified version of wxScopedPtr
// -------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------

View File

@ -23,6 +23,7 @@
#elif defined(__APPLE__) #elif defined(__APPLE__)
# include <aio.h> # include <aio.h>
#endif #endif
#include <memory>
class AsyncFileReader class AsyncFileReader
{ {
@ -151,7 +152,7 @@ class BlockdumpFileReader : public AsyncFileReader
s32 m_blockofs; s32 m_blockofs;
// index table // index table
ScopedArray<u32> m_dtable; std::unique_ptr<u32[]> m_dtable;
int m_dtablesize; int m_dtablesize;
int m_lresult; int m_lresult;

View File

@ -86,7 +86,7 @@ bool BlockdumpFileReader::Open(const wxString& fileName)
pxAssert( (datalen % (m_blocksize + 4)) == 0); pxAssert( (datalen % (m_blocksize + 4)) == 0);
m_dtablesize = datalen / (m_blocksize + 4); m_dtablesize = datalen / (m_blocksize + 4);
m_dtable = new u32[m_dtablesize]; m_dtable = std::unique_ptr<u32[]>(new u32[m_dtablesize]);
m_file->SeekI(BlockDumpHeaderSize); m_file->SeekI(BlockDumpHeaderSize);
@ -95,14 +95,14 @@ bool BlockdumpFileReader::Open(const wxString& fileName)
u32 has = 0; u32 has = 0;
int i = 0; int i = 0;
ScopedArray<u8> buffer(bs); std::unique_ptr<u8[]> buffer(new u8[bs]);
do { do {
m_file->Read(buffer.GetPtr(), bs); m_file->Read(buffer.get(), bs);
has = m_file->LastRead(); has = m_file->LastRead();
while (i < m_dtablesize && off < has) while (i < m_dtablesize && off < has)
{ {
m_dtable[i++] = *(u32*)(buffer.GetPtr() + off); m_dtable[i++] = *reinterpret_cast<u32*>(buffer.get() + off);
off += 4; off += 4;
off += m_blocksize; off += m_blocksize;
} }

View File

@ -27,11 +27,11 @@
#include <time.h> #include <time.h>
#include <wx/datetime.h> #include <wx/datetime.h>
#include <exception> #include <exception>
#include <memory>
#include "IsoFS/IsoFS.h" #include "IsoFS/IsoFS.h"
#include "IsoFS/IsoFSCDVD.h" #include "IsoFS/IsoFSCDVD.h"
#include "CDVDisoReader.h" #include "CDVDisoReader.h"
#include "Utilities/ScopedPtr.h"
#include "DebugTools/SymbolMap.h" #include "DebugTools/SymbolMap.h"
#include "AppConfig.h" #include "AppConfig.h"
@ -82,14 +82,14 @@ static int CheckDiskTypeFS(int baseType)
int size = file.getLength(); int size = file.getLength();
ScopedArray<char> buffer((int)file.getLength()+1); std::unique_ptr<char[]> buffer(new char[file.getLength() + 1]);
file.read((u8*)(buffer.GetPtr()),size); file.read(buffer.get(),size);
buffer[size]='\0'; buffer[size]='\0';
char* pos = strstr(buffer.GetPtr(), "BOOT2"); char* pos = strstr(buffer.get(), "BOOT2");
if (pos == NULL) if (pos == NULL)
{ {
pos = strstr(buffer.GetPtr(), "BOOT"); pos = strstr(buffer.get(), "BOOT");
if (pos == NULL) return CDVD_TYPE_ILLEGAL; if (pos == NULL) return CDVD_TYPE_ILLEGAL;
return CDVD_TYPE_PSCD; return CDVD_TYPE_PSCD;
} }

View File

@ -19,6 +19,7 @@
#include "wx/wfstream.h" #include "wx/wfstream.h"
#include "AsyncFileReader.h" #include "AsyncFileReader.h"
#include "CompressedFileReader.h" #include "CompressedFileReader.h"
#include <memory>
enum isoType enum isoType
{ {
@ -113,7 +114,7 @@ protected:
u32 m_blocks; u32 m_blocks;
// dtable / dtablesize are used when reading blockdumps // dtable / dtablesize are used when reading blockdumps
ScopedArray<u32> m_dtable; std::unique_ptr<u32[]> m_dtable;
int m_dtablesize; int m_dtablesize;
ScopedPtr<wxFileOutputStream> m_outstream; ScopedPtr<wxFileOutputStream> m_outstream;

View File

@ -114,7 +114,7 @@ void OutputIsoFile::WriteSector(const u8* src, uint lsn)
void OutputIsoFile::Close() void OutputIsoFile::Close()
{ {
m_dtable.Delete(); m_dtable.reset(nullptr);
_init(); _init();
} }

View File

@ -17,6 +17,7 @@
#include "App.h" #include "App.h"
#include <array> #include <array>
#include <memory>
BEGIN_DECLARE_EVENT_TYPES() BEGIN_DECLARE_EVENT_TYPES()
DECLARE_EVENT_TYPE(pxEvt_DockConsole, -1) DECLARE_EVENT_TYPE(pxEvt_DockConsole, -1)
@ -214,12 +215,6 @@ protected:
// a similar effect) // a similar effect)
ScopedPtr<ConsoleTestThread> m_threadlogger; ScopedPtr<ConsoleTestThread> m_threadlogger;
// ----------------------------------------------------------------------------
// Window and Menu Object Handles
// ----------------------------------------------------------------------------
ScopedArray<wxMenuItem*> m_sourceChecks;
public: public:
// ctor & dtor // ctor & dtor
ConsoleLogFrame( MainEmuFrame *pParent, const wxString& szTitle, ConLogConfig& options ); ConsoleLogFrame( MainEmuFrame *pParent, const wxString& szTitle, ConLogConfig& options );

View File

@ -203,7 +203,7 @@ static bool traceLogEnabled( const wxString& ident )
// -------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------
Panels::LogOptionsPanel::LogOptionsPanel(wxWindow* parent ) Panels::LogOptionsPanel::LogOptionsPanel(wxWindow* parent )
: BaseApplicableConfigPanel( parent ) : BaseApplicableConfigPanel( parent )
, m_checks( traceLogCount ) , m_checks( new pxCheckBox*[traceLogCount] )
{ {
wxStaticBoxSizer& s_misc = *new wxStaticBoxSizer( wxHORIZONTAL, this, L"Misc" ); wxStaticBoxSizer& s_misc = *new wxStaticBoxSizer( wxHORIZONTAL, this, L"Misc" );

View File

@ -17,6 +17,7 @@
#include "AppCommon.h" #include "AppCommon.h"
#include "ApplyState.h" #include "ApplyState.h"
#include <memory>
namespace Panels namespace Panels
{ {
@ -79,7 +80,7 @@ namespace Panels
pxCheckBox* m_masterEnabler; pxCheckBox* m_masterEnabler;
ScopedArray<pxCheckBox*> m_checks; std::unique_ptr<pxCheckBox*[]> m_checks;
public: public:
LogOptionsPanel( wxWindow* parent ); LogOptionsPanel( wxWindow* parent );

View File

@ -21,6 +21,7 @@
#include "ps2/BiosTools.h" #include "ps2/BiosTools.h"
#include <memory>
#include <wx/stdpaths.h> #include <wx/stdpaths.h>
using namespace Dialogs; using namespace Dialogs;
@ -107,13 +108,13 @@ Panels::LanguageSelectionPanel::LanguageSelectionPanel( wxWindow* parent, bool s
i18n_EnumeratePackages( m_langs ); i18n_EnumeratePackages( m_langs );
int size = m_langs.size(); int size = m_langs.size();
ScopedArray<wxString> compiled( size ); std::unique_ptr<wxString[]> compiled( new wxString[size] );
for( int i=0; i<size; ++i ) for( int i=0; i<size; ++i )
compiled[i].Printf( L"%s", m_langs[i].englishName.c_str() ); compiled[i].Printf( L"%s", m_langs[i].englishName.c_str() );
m_picker = new wxComboBox( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, m_picker = new wxComboBox( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize,
size, compiled.GetPtr(), wxCB_READONLY); size, compiled.get(), wxCB_READONLY);
*this += 5; *this += 5;
*this += m_picker | pxSizerFlags::StdSpace(); *this += m_picker | pxSizerFlags::StdSpace();