Made use of smart pointers :)

Former-commit-id: 5380752e0da3f4b958a944e41fcde38722e3c4c2
This commit is contained in:
Lynix 2013-05-23 02:13:45 +02:00
parent c934d8ed6b
commit 738788b4c3
6 changed files with 63 additions and 95 deletions

View File

@ -10,6 +10,7 @@
#include <Nazara/Core/File.hpp> #include <Nazara/Core/File.hpp>
#include <Nazara/Core/InputStream.hpp> #include <Nazara/Core/InputStream.hpp>
#include <Nazara/Core/MemoryStream.hpp> #include <Nazara/Core/MemoryStream.hpp>
#include <memory>
#include <sndfile/sndfile.h> #include <sndfile/sndfile.h>
#include <Nazara/Audio/Debug.hpp> #include <Nazara/Audio/Debug.hpp>
@ -112,29 +113,24 @@ namespace
sf_command(file, SFC_SET_SCALE_FLOAT_INT_READ, nullptr, SF_TRUE); sf_command(file, SFC_SET_SCALE_FLOAT_INT_READ, nullptr, SF_TRUE);
unsigned int sampleCount = infos.frames*infos.channels; unsigned int sampleCount = infos.frames*infos.channels;
nzInt16* samples = new nzInt16[sampleCount]; std::unique_ptr<nzInt16[]> samples(new nzInt16[sampleCount]);
if (sf_read_short(file, samples, sampleCount) != sampleCount)
if (sf_read_short(file, samples.get(), sampleCount) != sampleCount)
{ {
sf_close(file);
NazaraError("Failed to read samples"); NazaraError("Failed to read samples");
delete[] samples;
sf_close(file);
return false; return false;
} }
if (!soundBuffer->Create(format, infos.frames*infos.channels, infos.samplerate, samples)) if (!soundBuffer->Create(format, infos.frames*infos.channels, infos.samplerate, samples.get()))
{ {
sf_close(file);
NazaraError("Failed to create sound buffer"); NazaraError("Failed to create sound buffer");
delete[] samples;
sf_close(file);
return false; return false;
} }
delete[] samples;
return true; return true;
} }
} }

View File

@ -4,6 +4,7 @@
#include <Nazara/Core/Win32/DirectoryImpl.hpp> #include <Nazara/Core/Win32/DirectoryImpl.hpp>
#include <Nazara/Core/Error.hpp> #include <Nazara/Core/Error.hpp>
#include <memory>
#include <Nazara/Core/Debug.hpp> #include <Nazara/Core/Debug.hpp>
NzDirectoryImpl::NzDirectoryImpl(const NzDirectory* parent) NzDirectoryImpl::NzDirectoryImpl(const NzDirectory* parent)
@ -60,9 +61,9 @@ bool NzDirectoryImpl::NextResult()
bool NzDirectoryImpl::Open(const NzString& dirPath) bool NzDirectoryImpl::Open(const NzString& dirPath)
{ {
NzString searchPath = dirPath + "\\*"; NzString searchPath = dirPath + "\\*";
wchar_t* path = searchPath.GetWideBuffer();
m_handle = FindFirstFileW(path, &m_result); std::unique_ptr<wchar_t[]> wPath(searchPath.GetWideBuffer());
delete[] path; m_handle = FindFirstFileW(wPath.get(), &m_result);
if (m_handle == INVALID_HANDLE_VALUE) if (m_handle == INVALID_HANDLE_VALUE)
{ {
@ -77,18 +78,15 @@ bool NzDirectoryImpl::Open(const NzString& dirPath)
bool NzDirectoryImpl::Create(const NzString& dirPath) bool NzDirectoryImpl::Create(const NzString& dirPath)
{ {
wchar_t* path = dirPath.GetWideBuffer(); std::unique_ptr<wchar_t[]> wPath(dirPath.GetWideBuffer());
bool success = CreateDirectoryW(path, nullptr) != 0;
delete[] path;
return success || GetLastError() == ERROR_ALREADY_EXISTS; return (CreateDirectoryW(wPath.get(), nullptr) != 0) || GetLastError() == ERROR_ALREADY_EXISTS;
} }
bool NzDirectoryImpl::Exists(const NzString& dirPath) bool NzDirectoryImpl::Exists(const NzString& dirPath)
{ {
wchar_t* path = dirPath.GetWideBuffer(); std::unique_ptr<wchar_t[]> wPath(dirPath.GetWideBuffer());
DWORD attributes = GetFileAttributesW(path); DWORD attributes = GetFileAttributesW(wPath.get());
delete[] path;
if (attributes != INVALID_FILE_ATTRIBUTES) if (attributes != INVALID_FILE_ATTRIBUTES)
return (attributes & FILE_ATTRIBUTE_DIRECTORY) != 0; return (attributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
@ -99,34 +97,29 @@ bool NzDirectoryImpl::Exists(const NzString& dirPath)
NzString NzDirectoryImpl::GetCurrent() NzString NzDirectoryImpl::GetCurrent()
{ {
NzString currentPath; NzString currentPath;
wchar_t* path = new wchar_t[MAX_PATH]; std::unique_ptr<wchar_t[]> path(new wchar_t[MAX_PATH]);
unsigned int size = GetCurrentDirectoryW(MAX_PATH, path); unsigned int size = GetCurrentDirectoryW(MAX_PATH, path.get());
if (size > MAX_PATH) // La taille prends en compte le caractère nul if (size > MAX_PATH) // La taille prends en compte le caractère nul
{ {
delete[] path; path.reset(new wchar_t[size]);
if (GetCurrentDirectoryW(size, path.get()) != 0)
path = new wchar_t[size]; currentPath = NzString::Unicode(path.get());
if (GetCurrentDirectoryW(size, path) == 0)
NazaraError("Unable to get current directory: " + NzGetLastSystemError());
else else
currentPath = NzString::Unicode(path); NazaraError("Unable to get current directory: " + NzGetLastSystemError());
} }
else if (size == 0) else if (size == 0)
NazaraError("Unable to get current directory: " + NzGetLastSystemError()); NazaraError("Unable to get current directory: " + NzGetLastSystemError());
else else
currentPath = NzString::Unicode(path); currentPath = NzString::Unicode(path.get());
delete[] path;
return currentPath; return currentPath;
} }
bool NzDirectoryImpl::Remove(const NzString& dirPath) bool NzDirectoryImpl::Remove(const NzString& dirPath)
{ {
wchar_t* path = dirPath.GetWideBuffer(); std::unique_ptr<wchar_t[]> path(dirPath.GetWideBuffer());
bool success = RemoveDirectoryW(path) != 0; bool success = RemoveDirectoryW(path.get()) != 0;
delete[] path;
DWORD error = GetLastError(); DWORD error = GetLastError();
return success || error == ERROR_FILE_NOT_FOUND || error == ERROR_PATH_NOT_FOUND; return success || error == ERROR_FILE_NOT_FOUND || error == ERROR_PATH_NOT_FOUND;

View File

@ -6,6 +6,7 @@
#include <Nazara/Core/DynLib.hpp> #include <Nazara/Core/DynLib.hpp>
#include <Nazara/Core/Error.hpp> #include <Nazara/Core/Error.hpp>
#include <Nazara/Core/String.hpp> #include <Nazara/Core/String.hpp>
#include <memory>
#include <Nazara/Core/Debug.hpp> #include <Nazara/Core/Debug.hpp>
NzDynLibImpl::NzDynLibImpl(NzDynLib* parent) : NzDynLibImpl::NzDynLibImpl(NzDynLib* parent) :
@ -28,9 +29,8 @@ bool NzDynLibImpl::Load(const NzString& libraryPath)
if (!path.EndsWith(".dll")) if (!path.EndsWith(".dll"))
path += ".dll"; path += ".dll";
wchar_t* pathW = path.GetWideBuffer(); std::unique_ptr<wchar_t[]> wPath(path.GetWideBuffer());
m_handle = LoadLibraryExW(pathW, nullptr, LOAD_WITH_ALTERED_SEARCH_PATH); m_handle = LoadLibraryExW(wPath.get(), nullptr, LOAD_WITH_ALTERED_SEARCH_PATH);
delete[] pathW;
if (m_handle) if (m_handle)
return true; return true;

View File

@ -5,6 +5,7 @@
#include <Nazara/Core/Win32/FileImpl.hpp> #include <Nazara/Core/Win32/FileImpl.hpp>
#include <Nazara/Core/Error.hpp> #include <Nazara/Core/Error.hpp>
#include <Nazara/Core/Win32/Time.hpp> #include <Nazara/Core/Win32/Time.hpp>
#include <memory>
#include <Nazara/Core/Debug.hpp> #include <Nazara/Core/Debug.hpp>
NzFileImpl::NzFileImpl(const NzFile* parent) : NzFileImpl::NzFileImpl(const NzFile* parent) :
@ -91,9 +92,8 @@ bool NzFileImpl::Open(const NzString& filePath, unsigned int mode)
if ((mode & NzFile::Lock) == 0) if ((mode & NzFile::Lock) == 0)
shareMode |= FILE_SHARE_WRITE; shareMode |= FILE_SHARE_WRITE;
wchar_t* path = filePath.GetWideBuffer(); std::unique_ptr<wchar_t[]> path(filePath.GetWideBuffer());
m_handle = CreateFileW(path, access, shareMode, nullptr, openMode, 0, nullptr); m_handle = CreateFileW(path.get(), access, shareMode, nullptr, openMode, 0, nullptr);
delete[] path;
return m_handle != INVALID_HANDLE_VALUE; return m_handle != INVALID_HANDLE_VALUE;
} }
@ -184,13 +184,10 @@ std::size_t NzFileImpl::Write(const void* buffer, std::size_t size)
bool NzFileImpl::Copy(const NzString& sourcePath, const NzString& targetPath) bool NzFileImpl::Copy(const NzString& sourcePath, const NzString& targetPath)
{ {
wchar_t* path = sourcePath.GetWideBuffer(); std::unique_ptr<wchar_t[]> srcPath(sourcePath.GetWideBuffer());
wchar_t* newPath = targetPath.GetWideBuffer(); std::unique_ptr<wchar_t[]> dstPath(targetPath.GetWideBuffer());
bool success = CopyFileW(path, newPath, false);
delete[] path;
delete[] newPath;
if (success) if (CopyFileW(srcPath.get(), dstPath.get(), false))
return true; return true;
else else
{ {
@ -202,11 +199,9 @@ bool NzFileImpl::Copy(const NzString& sourcePath, const NzString& targetPath)
bool NzFileImpl::Delete(const NzString& filePath) bool NzFileImpl::Delete(const NzString& filePath)
{ {
wchar_t* path = filePath.GetWideBuffer(); std::unique_ptr<wchar_t[]> path(filePath.GetWideBuffer());
bool success = DeleteFileW(path);
delete[] path;
if (success) if (DeleteFileW(path.get()))
return true; return true;
else else
{ {
@ -218,9 +213,8 @@ bool NzFileImpl::Delete(const NzString& filePath)
bool NzFileImpl::Exists(const NzString& filePath) bool NzFileImpl::Exists(const NzString& filePath)
{ {
wchar_t* path = filePath.GetWideBuffer(); std::unique_ptr<wchar_t[]> path(filePath.GetWideBuffer());
HANDLE handle = CreateFileW(path, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, 0, nullptr); HANDLE handle = CreateFileW(path.get(), 0, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, 0, nullptr);
delete[] path;
if (handle == INVALID_HANDLE_VALUE) if (handle == INVALID_HANDLE_VALUE)
return false; return false;
@ -232,9 +226,8 @@ bool NzFileImpl::Exists(const NzString& filePath)
time_t NzFileImpl::GetCreationTime(const NzString& filePath) time_t NzFileImpl::GetCreationTime(const NzString& filePath)
{ {
wchar_t* path = filePath.GetWideBuffer(); std::unique_ptr<wchar_t[]> path(filePath.GetWideBuffer());
HANDLE handle = CreateFileW(path, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, 0, nullptr); HANDLE handle = CreateFileW(path.get(), 0, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, 0, nullptr);
delete[] path;
if (handle == INVALID_HANDLE_VALUE) if (handle == INVALID_HANDLE_VALUE)
return 0; return 0;
@ -255,9 +248,8 @@ time_t NzFileImpl::GetCreationTime(const NzString& filePath)
time_t NzFileImpl::GetLastAccessTime(const NzString& filePath) time_t NzFileImpl::GetLastAccessTime(const NzString& filePath)
{ {
wchar_t* path = filePath.GetWideBuffer(); std::unique_ptr<wchar_t[]> path(filePath.GetWideBuffer());
HANDLE handle = CreateFileW(path, 0, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr); HANDLE handle = CreateFileW(path.get(), 0, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
delete[] path;
if (handle == INVALID_HANDLE_VALUE) if (handle == INVALID_HANDLE_VALUE)
return 0; return 0;
@ -278,9 +270,8 @@ time_t NzFileImpl::GetLastAccessTime(const NzString& filePath)
time_t NzFileImpl::GetLastWriteTime(const NzString& filePath) time_t NzFileImpl::GetLastWriteTime(const NzString& filePath)
{ {
wchar_t* path = filePath.GetWideBuffer(); std::unique_ptr<wchar_t[]> path(filePath.GetWideBuffer());
HANDLE handle = CreateFileW(path, 0, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr); HANDLE handle = CreateFileW(path.get(), 0, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
delete[] path;
if (handle == INVALID_HANDLE_VALUE) if (handle == INVALID_HANDLE_VALUE)
return 0; return 0;
@ -301,9 +292,8 @@ time_t NzFileImpl::GetLastWriteTime(const NzString& filePath)
nzUInt64 NzFileImpl::GetSize(const NzString& filePath) nzUInt64 NzFileImpl::GetSize(const NzString& filePath)
{ {
wchar_t* path = filePath.GetWideBuffer(); std::unique_ptr<wchar_t[]> path(filePath.GetWideBuffer());
HANDLE handle = CreateFileW(path, 0, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr); HANDLE handle = CreateFileW(path.get(), 0, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
delete[] path;
if (handle == INVALID_HANDLE_VALUE) if (handle == INVALID_HANDLE_VALUE)
return 0; return 0;
@ -319,15 +309,10 @@ nzUInt64 NzFileImpl::GetSize(const NzString& filePath)
bool NzFileImpl::Rename(const NzString& sourcePath, const NzString& targetPath) bool NzFileImpl::Rename(const NzString& sourcePath, const NzString& targetPath)
{ {
wchar_t* path = sourcePath.GetWideBuffer(); std::unique_ptr<wchar_t[]> srcPath(sourcePath.GetWideBuffer());
wchar_t* newPath = targetPath.GetWideBuffer(); std::unique_ptr<wchar_t[]> dstPath(targetPath.GetWideBuffer());
bool success = MoveFileExW(path, newPath, MOVEFILE_COPY_ALLOWED) != 0; if (MoveFileExW(srcPath.get(), dstPath.get(), MOVEFILE_COPY_ALLOWED))
delete[] path;
delete[] newPath;
if (success)
return true; return true;
else else
{ {

View File

@ -11,6 +11,7 @@
#include <Nazara/Utility/Mesh.hpp> #include <Nazara/Utility/Mesh.hpp>
#include <Nazara/Utility/Skeleton.hpp> #include <Nazara/Utility/Skeleton.hpp>
#include <Nazara/Utility/VertexStruct.hpp> #include <Nazara/Utility/VertexStruct.hpp>
#include <memory>
#include <vector> #include <vector>
#include <Nazara/Utility/Debug.hpp> #include <Nazara/Utility/Debug.hpp>
@ -132,10 +133,10 @@ namespace
struct NzSkeletalMeshImpl struct NzSkeletalMeshImpl
{ {
std::unique_ptr<nzUInt8[]> bindPoseBuffer;
std::vector<NzVertexWeight> vertexWeights; std::vector<NzVertexWeight> vertexWeights;
std::vector<NzWeight> weights; std::vector<NzWeight> weights;
NzCubef aabb; NzCubef aabb;
nzUInt8* bindPoseBuffer;
NzIndexBufferConstRef indexBuffer; NzIndexBufferConstRef indexBuffer;
unsigned int vertexCount; unsigned int vertexCount;
}; };
@ -169,7 +170,7 @@ bool NzSkeletalMesh::Create(unsigned int vertexCount, unsigned int weightCount)
#endif #endif
m_impl = new NzSkeletalMeshImpl; m_impl = new NzSkeletalMeshImpl;
m_impl->bindPoseBuffer = new nzUInt8[vertexCount*sizeof(NzMeshVertex)]; m_impl->bindPoseBuffer.reset(new nzUInt8[vertexCount*sizeof(NzMeshVertex)]);
m_impl->vertexCount = vertexCount; m_impl->vertexCount = vertexCount;
m_impl->vertexWeights.resize(vertexCount); m_impl->vertexWeights.resize(vertexCount);
m_impl->weights.resize(weightCount); m_impl->weights.resize(weightCount);
@ -181,7 +182,6 @@ void NzSkeletalMesh::Destroy()
{ {
if (m_impl) if (m_impl)
{ {
delete[] m_impl->bindPoseBuffer;
delete m_impl; delete m_impl;
m_impl = nullptr; m_impl = nullptr;
} }
@ -217,7 +217,7 @@ void* NzSkeletalMesh::GetBindPoseBuffer()
} }
#endif #endif
return m_impl->bindPoseBuffer; return m_impl->bindPoseBuffer.get();
} }
const void* NzSkeletalMesh::GetBindPoseBuffer() const const void* NzSkeletalMesh::GetBindPoseBuffer() const
@ -230,7 +230,7 @@ const void* NzSkeletalMesh::GetBindPoseBuffer() const
} }
#endif #endif
return m_impl->bindPoseBuffer; return m_impl->bindPoseBuffer.get();
} }
const NzIndexBuffer* NzSkeletalMesh::GetIndexBuffer() const const NzIndexBuffer* NzSkeletalMesh::GetIndexBuffer() const
@ -358,7 +358,7 @@ void NzSkeletalMesh::Skin(NzMeshVertex* outputBuffer, const NzSkeleton* skeleton
#endif #endif
SkinningInfos skinningInfos; SkinningInfos skinningInfos;
skinningInfos.inputVertex = reinterpret_cast<const NzMeshVertex*>(m_impl->bindPoseBuffer); skinningInfos.inputVertex = reinterpret_cast<const NzMeshVertex*>(m_impl->bindPoseBuffer.get());
skinningInfos.outputVertex = outputBuffer; skinningInfos.outputVertex = outputBuffer;
skinningInfos.joints = skeleton->GetJoints(); skinningInfos.joints = skeleton->GetJoints();
skinningInfos.vertexWeights = &m_impl->vertexWeights[0]; skinningInfos.vertexWeights = &m_impl->vertexWeights[0];

View File

@ -18,6 +18,7 @@
#include <Nazara/Utility/Win32/CursorImpl.hpp> #include <Nazara/Utility/Win32/CursorImpl.hpp>
#include <Nazara/Utility/Win32/IconImpl.hpp> #include <Nazara/Utility/Win32/IconImpl.hpp>
#include <cstdio> #include <cstdio>
#include <memory>
#include <windowsx.h> #include <windowsx.h>
#include <Nazara/Utility/Debug.hpp> #include <Nazara/Utility/Debug.hpp>
@ -120,7 +121,7 @@ bool NzWindowImpl::Create(NzVideoMode mode, const NzString& title, nzUInt32 styl
m_callback = 0; m_callback = 0;
wchar_t* wtitle = title.GetWideBuffer(); std::unique_ptr<wchar_t> wtitle(title.GetWideBuffer());
#if NAZARA_UTILITY_THREADED_WINDOW #if NAZARA_UTILITY_THREADED_WINDOW
NzMutex mutex; NzMutex mutex;
@ -129,15 +130,13 @@ bool NzWindowImpl::Create(NzVideoMode mode, const NzString& title, nzUInt32 styl
// On attend que la fenêtre soit créée // On attend que la fenêtre soit créée
mutex.Lock(); mutex.Lock();
m_thread = new NzThread(WindowThread, &m_handle, win32StyleEx, wtitle, win32Style, x, y, width, height, this, &mutex, &condition); m_thread = new NzThread(WindowThread, &m_handle, win32StyleEx, wtitle.get(), win32Style, x, y, width, height, this, &mutex, &condition);
condition.Wait(&mutex); condition.Wait(&mutex);
mutex.Unlock(); mutex.Unlock();
#else #else
m_handle = CreateWindowExW(win32StyleEx, className, wtitle, win32Style, x, y, width, height, nullptr, nullptr, GetModuleHandle(nullptr), this); m_handle = CreateWindowExW(win32StyleEx, className, wtitle.get(), win32Style, x, y, width, height, nullptr, nullptr, GetModuleHandle(nullptr), this);
#endif #endif
delete[] wtitle;
if (fullscreen) if (fullscreen)
{ {
SetForegroundWindow(m_handle); SetForegroundWindow(m_handle);
@ -245,14 +244,10 @@ NzString NzWindowImpl::GetTitle() const
titleSize++; // Caractère nul titleSize++; // Caractère nul
wchar_t* wTitle = new wchar_t[titleSize]; std::unique_ptr<wchar_t[]> wTitle(new wchar_t[titleSize]);
GetWindowTextW(m_handle, wTitle, titleSize); GetWindowTextW(m_handle, wTitle.get(), titleSize);
NzString title = NzString::Unicode(wTitle); return NzString::Unicode(wTitle.get());
delete[] wTitle;
return title;
} }
unsigned int NzWindowImpl::GetWidth() const unsigned int NzWindowImpl::GetWidth() const
@ -463,9 +458,8 @@ void NzWindowImpl::SetStayOnTop(bool stayOnTop)
void NzWindowImpl::SetTitle(const NzString& title) void NzWindowImpl::SetTitle(const NzString& title)
{ {
wchar_t* wtitle = title.GetWideBuffer(); std::unique_ptr<wchar_t[]> wTitle(title.GetWideBuffer());
SetWindowTextW(m_handle, wtitle); SetWindowTextW(m_handle, wTitle.get());
delete[] wtitle;
} }
void NzWindowImpl::SetVisible(bool visible) void NzWindowImpl::SetVisible(bool visible)