AutoDeleteArray -> std::unique_ptr

Also, switch to C++14 for std::make_unique.
This commit is contained in:
Eelco Dolstra 2017-01-16 22:24:29 +01:00
parent 40dfac968a
commit 2b9d0a99cb
5 changed files with 10 additions and 24 deletions

View file

@ -27,7 +27,7 @@ makefiles = \
tests/local.mk tests/local.mk
#src/download-via-ssh/local.mk \ #src/download-via-ssh/local.mk \
GLOBAL_CXXFLAGS += -std=c++11 -g -Wall GLOBAL_CXXFLAGS += -std=c++14 -g -Wall
-include Makefile.config -include Makefile.config

View file

@ -599,9 +599,8 @@ void RemoteStore::Connection::processStderr(Sink * sink, Source * source)
else if (msg == STDERR_READ) { else if (msg == STDERR_READ) {
if (!source) throw Error("no source"); if (!source) throw Error("no source");
size_t len = readInt(from); size_t len = readInt(from);
unsigned char * buf = new unsigned char[len]; auto buf = std::make_unique<unsigned char[]>(len);
AutoDeleteArray<unsigned char> d(buf); writeString(buf.get(), source->read(buf.get(), len), to);
writeString(buf, source->read(buf, len), to);
to.flush(); to.flush();
} }
else else

View file

@ -3,6 +3,7 @@
#include <cstring> #include <cstring>
#include <cerrno> #include <cerrno>
#include <memory>
namespace nix { namespace nix {
@ -236,11 +237,10 @@ size_t readString(unsigned char * buf, size_t max, Source & source)
string readString(Source & source) string readString(Source & source)
{ {
size_t len = readInt(source); size_t len = readInt(source);
unsigned char * buf = new unsigned char[len]; auto buf = std::make_unique<unsigned char[]>(len);
AutoDeleteArray<unsigned char> d(buf); source(buf.get(), len);
source(buf, len);
readPadding(len, source); readPadding(len, source);
return string((char *) buf, len); return string((char *) buf.get(), len);
} }
Source & operator >> (Source & in, string & s) Source & operator >> (Source & in, string & s)

View file

@ -272,11 +272,10 @@ string readFile(int fd)
if (fstat(fd, &st) == -1) if (fstat(fd, &st) == -1)
throw SysError("statting file"); throw SysError("statting file");
unsigned char * buf = new unsigned char[st.st_size]; auto buf = std::make_unique<unsigned char[]>(st.st_size);
AutoDeleteArray<unsigned char> d(buf); readFull(fd, buf.get(), st.st_size);
readFull(fd, buf, st.st_size);
return string((char *) buf, st.st_size); return string((char *) buf.get(), st.st_size);
} }

View file

@ -139,18 +139,6 @@ string drainFD(int fd);
/* Automatic cleanup of resources. */ /* Automatic cleanup of resources. */
template <class T>
struct AutoDeleteArray
{
T * p;
AutoDeleteArray(T * p) : p(p) { }
~AutoDeleteArray()
{
delete [] p;
}
};
class AutoDelete class AutoDelete
{ {
Path path; Path path;