Some code cleanups.

Added updated C++14 make_unique templates, which now allow to also
use unique_ptr for arrays.

Updated SoundSDL class to use unique_ptr, eliminating another new/delete
pair.


git-svn-id: svn://svn.code.sf.net/p/stella/code/trunk@3171 8b62c5a3-ac7e-4cc8-8f21-d9a121418aba
This commit is contained in:
stephena 2015-06-13 23:58:37 +00:00
parent a39630aca4
commit c7ef1607be
19 changed files with 100 additions and 79 deletions

View File

@ -22,8 +22,6 @@
class OSystem;
#include <regex>
#include "bspf.hxx"
class Cheat

View File

@ -17,8 +17,6 @@
// $Id$
//============================================================================
#include <sstream>
#include "bspf.hxx"
#include "Cheat.hxx"

View File

@ -40,12 +40,6 @@ void EventHandlerSDL2::enableTextEvents(bool enable)
SDL_StopTextInput();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
const char* EventHandlerSDL2::nameForKey(StellaKey key) const
{
return SDL_GetScancodeName(SDL_Scancode(key));
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void EventHandlerSDL2::pollEvent()
{

View File

@ -53,7 +53,9 @@ class EventHandlerSDL2 : public EventHandler
/**
Returns the human-readable name for a StellaKey.
*/
const char* nameForKey(StellaKey key) const;
const char* nameForKey(StellaKey key) const {
return SDL_GetScancodeName(SDL_Scancode(key));
}
/**
Collects and dispatches any pending SDL2 events.

View File

@ -107,8 +107,7 @@ Int32 FrameBufferSDL2::getCurrentDisplayIndex()
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool FrameBufferSDL2::setVideoMode(const string& title, const VideoMode& mode,
bool /*fullscreen_toggle*/)
bool FrameBufferSDL2::setVideoMode(const string& title, const VideoMode& mode)
{
// If not initialized by this point, then immediately fail
if(SDL_WasInit(SDL_INIT_VIDEO) == 0)

View File

@ -129,8 +129,7 @@ class FrameBufferSDL2 : public FrameBuffer
@return False on any errors, else true
*/
bool setVideoMode(const string& title, const VideoMode& mode,
bool fullscreen_toggle);
bool setVideoMode(const string& title, const VideoMode& mode);
/**
This method is called to invalidate the contents of the entire

View File

@ -34,13 +34,6 @@ PNGLibrary::PNGLibrary(const FrameBuffer& fb)
{
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
PNGLibrary::~PNGLibrary()
{
delete[] ReadInfo.buffer;
delete[] ReadInfo.row_pointers;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void PNGLibrary::loadImage(const string& filename, FBSurface& surface)
{
@ -311,7 +304,7 @@ void PNGLibrary::writeComments(png_structp png_ptr, png_infop info_ptr,
if(numComments == 0)
return;
png_text* text_ptr = new png_text[numComments];
png_text text_ptr[numComments];
for(uInt32 i = 0; i < numComments; ++i)
{
text_ptr[i].key = (char*) comments[i].first.c_str();
@ -320,7 +313,6 @@ void PNGLibrary::writeComments(png_structp png_ptr, png_infop info_ptr,
text_ptr[i].text_length = 0;
}
png_set_text(png_ptr, info_ptr, text_ptr, numComments);
delete[] text_ptr;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

View File

@ -41,7 +41,6 @@ class PNGLibrary
{
public:
PNGLibrary(const FrameBuffer& fb);
~PNGLibrary();
/**
Read a PNG image from the specified file into a FBSurface structure,
@ -93,6 +92,8 @@ class PNGLibrary
// The following data remains between invocations of allocateStorage,
// and is only changed when absolutely necessary.
struct ReadInfoType {
~ReadInfoType() { delete[] buffer; delete[] row_pointers; }
uInt8* buffer;
png_bytep* row_pointers;
png_uint_32 width, height, pitch;

View File

@ -450,19 +450,13 @@ bool SoundSDL2::load(Serializer& in)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SoundSDL2::RegWriteQueue::RegWriteQueue(uInt32 capacity)
: myCapacity(capacity),
myBuffer(nullptr),
: myBuffer(nullptr),
myCapacity(capacity),
mySize(0),
myHead(0),
myTail(0)
{
myBuffer = new RegWrite[myCapacity];
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SoundSDL2::RegWriteQueue::~RegWriteQueue()
{
delete[] myBuffer;
myBuffer = make_ptr<RegWrite[]>(myCapacity);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -521,16 +515,15 @@ uInt32 SoundSDL2::RegWriteQueue::size() const
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void SoundSDL2::RegWriteQueue::grow()
{
RegWrite* buffer = new RegWrite[myCapacity * 2];
unique_ptr<RegWrite[]> buffer = make_ptr<RegWrite[]>(myCapacity*2);
for(uInt32 i = 0; i < mySize; ++i)
{
buffer[i] = myBuffer[(myHead + i) % myCapacity];
}
myHead = 0;
myTail = mySize;
myCapacity = myCapacity * 2;
delete[] myBuffer;
myBuffer = buffer;
myCapacity *= 2;
myBuffer = std::move(buffer);
}
#endif // SOUND_SUPPORT

View File

@ -193,11 +193,6 @@ class SoundSDL2 : public Sound
*/
RegWriteQueue(uInt32 capacity = 512);
/**
Destroy this queue instance.
*/
virtual ~RegWriteQueue();
public:
/**
Clear any items stored in the queue.
@ -238,8 +233,8 @@ class SoundSDL2 : public Sound
void grow();
private:
unique_ptr<RegWrite[]> myBuffer;
uInt32 myCapacity;
RegWrite* myBuffer;
uInt32 mySize;
uInt32 myHead;
uInt32 myTail;

View File

@ -38,7 +38,7 @@ class StringParser
*/
StringParser(const string& str)
{
stringstream buf(str);
istringstream buf(str);
string line;
while(std::getline(buf, line, '\n'))
@ -54,7 +54,7 @@ class StringParser
*/
StringParser(const string& str, uInt16 maxlen)
{
stringstream buf(str);
istringstream buf(str);
string line;
while(std::getline(buf, line, '\n'))

67
src/common/UniquePtr.hxx Normal file
View File

@ -0,0 +1,67 @@
//============================================================================
//
// SSSS tt lll lll
// SS SS tt ll ll
// SS tttttt eeee ll ll aaaa
// SSSS tt ee ee ll ll aa
// SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator"
// SS SS tt ee ll ll aa aa
// SSSS ttt eeeee llll llll aaaaa
//
// Copyright (c) 1995-2015 by Bradford W. Mott, Stephen Anthony
// and the Stella Team
//
// See the file "License.txt" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//
// $Id$
//============================================================================
#ifndef UNIQUE_PTR_HXX
#define UNIQUE_PTR_HXX
#include <cstddef>
#include <memory>
#include <type_traits>
#include <utility>
/**
The following templates implement 'make_unique' from C++14, which is
named 'make_ptr' here. Eventually, when compilers are updated, this code
can be removed.
@author Stephen Anthony, based on a C++14 proposal by Stephan T. Lavavej.
*/
namespace std {
template<class T> struct _Unique_if {
typedef unique_ptr<T> _Single_object;
};
template<class T> struct _Unique_if<T[]> {
typedef unique_ptr<T[]> _Unknown_bound;
};
template<class T, size_t N> struct _Unique_if<T[N]> {
typedef void _Known_bound;
};
template<class T, class... Args>
typename _Unique_if<T>::_Single_object
make_ptr(Args&&... args) {
return unique_ptr<T>(new T(std::forward<Args>(args)...));
}
template<class T>
typename _Unique_if<T>::_Unknown_bound
make_ptr(size_t n) {
typedef typename remove_extent<T>::type U;
return unique_ptr<T>(new U[n]());
}
template<class T, class... Args>
typename _Unique_if<T>::_Known_bound
make_ptr(Args&&...) = delete;
}
#endif

View File

@ -55,6 +55,7 @@ using uInt64 = uint64_t;
#include <cstdio>
#include <utility>
#include <vector>
#include "UniquePtr.hxx" // only until C++14 compilers are more common
using namespace std;
// Common array types
@ -99,17 +100,6 @@ static const string EmptyString("");
//////////////////////////////////////////////////////////////////////
// Some convenience functions
// Initialize C++11 unique_ptr, at least until std::make_unique()
// becomes part of the standard (C++14)
template <typename Value, typename ... Arguments>
std::unique_ptr<Value> make_ptr(Arguments && ... arguments_for_constructor)
{
return std::unique_ptr<Value>(
new Value(std::forward<Arguments>(arguments_for_constructor)...)
);
}
template<typename T> inline void BSPF_swap(T& a, T& b) { std::swap(a, b); }
template<typename T> inline T BSPF_abs (T x) { return (x>=0) ? x : -x; }
template<typename T> inline T BSPF_min (T a, T b) { return (a<b) ? a : b; }

View File

@ -38,7 +38,7 @@ class Device : public Serializable
/**
Create a new device
*/
Device() : mySystem(0) { }
Device() : mySystem(nullptr) { }
/**
Destructor

View File

@ -530,7 +530,7 @@ void FrameBuffer::stateChanged(EventHandler::State state)
void FrameBuffer::setFullscreen(bool enable)
{
const VideoMode& mode = getSavedVidMode(enable);
if(setVideoMode(myScreenTitle, mode, true))
if(setVideoMode(myScreenTitle, mode))
{
myImageRect = mode.image;
myScreenSize = mode.screen;

View File

@ -357,15 +357,9 @@ class FrameBuffer
@param title The title for the created window
@param mode The video mode to use
@param fullscreen_toggle Indicate whether this video mode change is
due to a fullscreen/windowed toggle or not; some backends
can use this information to perform a more optimized mode
change
@return False on any errors, else true
*/
virtual bool setVideoMode(const string& title, const VideoMode& mode,
bool fullscreen_toggle = false) = 0;
virtual bool setVideoMode(const string& title, const VideoMode& mode) = 0;
/**
This method is called to invalidate the contents of the entire

View File

@ -293,16 +293,16 @@ class System : public Serializable
// Constructors
PageAccess()
: directPeekBase(0),
directPokeBase(0),
codeAccessBase(0),
device(0),
: directPeekBase(nullptr),
directPokeBase(nullptr),
codeAccessBase(nullptr),
device(nullptr),
type(System::PA_READ) { }
PageAccess(Device* dev, PageAccessType access)
: directPeekBase(0),
directPokeBase(0),
codeAccessBase(0),
: directPeekBase(nullptr),
directPokeBase(nullptr),
codeAccessBase(nullptr),
device(dev),
type(access) { }
};

View File

@ -8,8 +8,8 @@
using namespace std;
typedef unsigned char uInt8;
typedef unsigned int uInt32;
using uInt8 = unsigned char;
using uInt32 = unsigned int;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
int searchForBytes(const uInt8* image, uInt32 imagesize,

View File

@ -26,8 +26,7 @@ int main(int ac, char* av[])
int values_per_line = ac >= 3 ? atoi(av[2]) : 8;
int offset = ac >= 4 ? atoi(av[3]) : 0;
ifstream in;
in.open(av[1]);
ifstream in(av[1]);
if(in.is_open())
{
in.seekg(0, ios::end);