Use unique_ref instead of unique_ptr wherever possible

This commit is contained in:
Sebastian Meßmer 2015-06-18 19:30:52 +02:00
parent 33a20223cd
commit 536cba1459
22 changed files with 128 additions and 92 deletions

View File

@ -3,7 +3,7 @@
#define FSPP_DEVICE_H_
#include <boost/filesystem.hpp>
#include <memory>
#include <messmer/cpp-utils/unique_ref.h>
#include <sys/statvfs.h>
namespace fspp {
@ -14,7 +14,7 @@ public:
virtual ~Device() {}
virtual void statfs(const boost::filesystem::path &path, struct ::statvfs *fsstat) = 0;
virtual std::unique_ptr<Node> Load(const boost::filesystem::path &path) = 0;
virtual boost::optional<cpputils::unique_ref<Node>> Load(const boost::filesystem::path &path) = 0;
};
}

View File

@ -3,7 +3,7 @@
#define FSPP_DIR_H_
#include "Node.h"
#include <memory>
#include <messmer/cpp-utils/unique_ref.h>
#include <string>
namespace fspp {
@ -26,13 +26,13 @@ public:
std::string name;
};
virtual std::unique_ptr<OpenFile> createAndOpenFile(const std::string &name, mode_t mode, uid_t uid, gid_t gid) = 0;
virtual cpputils::unique_ref<OpenFile> createAndOpenFile(const std::string &name, mode_t mode, uid_t uid, gid_t gid) = 0;
virtual void createDir(const std::string &name, mode_t mode, uid_t uid, gid_t gid) = 0;
virtual void createSymlink(const std::string &name, const boost::filesystem::path &target, uid_t uid, gid_t gid) = 0;
//TODO Allow alternative implementation returning only children names without more information
//virtual std::unique_ptr<std::vector<std::string>> children() const = 0;
virtual std::unique_ptr<std::vector<Entry>> children() const = 0;
virtual cpputils::unique_ref<std::vector<Entry>> children() const = 0;
};
}

View File

@ -3,7 +3,7 @@
#define FSPP_FILE_H_
#include "Node.h"
#include <memory>
#include <messmer/cpp-utils/unique_ref.h>
namespace fspp {
class Device;
@ -13,7 +13,7 @@ class File: public virtual Node {
public:
virtual ~File() {}
virtual std::unique_ptr<OpenFile> open(int flags) const = 0;
virtual cpputils::unique_ref<OpenFile> open(int flags) const = 0;
virtual void truncate(off_t size) const = 0;
};

View File

@ -3,7 +3,7 @@
#define FSPP_SYMLINK_H_
#include "Node.h"
#include <memory>
#include <messmer/cpp-utils/unique_ref.h>
#include <string>
namespace fspp {

View File

@ -2,10 +2,10 @@
#define MESSMER_FSPP_FSTEST_TESTUTILS_FILESYSTEMTEST_H_
#include <google/gtest/gtest.h>
#include <memory>
#include <type_traits>
#include <boost/static_assert.hpp>
#include <messmer/cpp-utils/pointer.h>
#include <messmer/cpp-utils/unique_ref.h>
#include <messmer/cpp-utils/unique_ref_boost_optional_gtest_workaround.h>
#include "../../fs_interface/Device.h"
#include "../../fs_interface/Dir.h"
@ -14,7 +14,7 @@
class FileSystemTestFixture {
public:
virtual std::unique_ptr<fspp::Device> createDevice() = 0;
virtual cpputils::unique_ref<fspp::Device> createDevice() = 0;
};
template<class ConcreteFileSystemTestFixture>
@ -28,22 +28,24 @@ public:
FileSystemTest(): fixture(), device(fixture.createDevice()) {}
ConcreteFileSystemTestFixture fixture;
std::unique_ptr<fspp::Device> device;
cpputils::unique_ref<fspp::Device> device;
static constexpr mode_t MODE_PUBLIC = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH;
std::unique_ptr<fspp::Dir> LoadDir(const boost::filesystem::path &path) {
cpputils::unique_ref<fspp::Dir> LoadDir(const boost::filesystem::path &path) {
auto loaded = device->Load(path);
auto dir = cpputils::dynamic_pointer_move<fspp::Dir>(loaded);
EXPECT_NE(nullptr, dir.get());
return dir;
EXPECT_NE(boost::none, loaded);
auto dir = cpputils::dynamic_pointer_move<fspp::Dir>(*loaded);
EXPECT_NE(boost::none, dir);
return std::move(*dir);
}
std::unique_ptr<fspp::File> LoadFile(const boost::filesystem::path &path) {
cpputils::unique_ref<fspp::File> LoadFile(const boost::filesystem::path &path) {
auto loaded = device->Load(path);
auto file = cpputils::dynamic_pointer_move<fspp::File>(loaded);
EXPECT_NE(nullptr, file.get());
return file;
EXPECT_NE(boost::none, loaded);
auto file = cpputils::dynamic_pointer_move<fspp::File>(*loaded);
EXPECT_NE(boost::none, file);
return std::move(*file);
}
};

View File

@ -3,19 +3,18 @@
#include "FileSystemTest.h"
#include <messmer/cpp-utils/data/Data.h>
#include <memory>
#include <messmer/cpp-utils/unique_ref.h>
template<class ConcreteFileSystemTestFixture>
class FileTest: public FileSystemTest<ConcreteFileSystemTestFixture> {
public:
FileTest() {
this->LoadDir("/")->createAndOpenFile("myfile", this->MODE_PUBLIC, 0, 0);
file_root = this->LoadFile("/myfile");
file_root = cpputils::to_unique_ptr(this->LoadFile("/myfile"));
this->LoadDir("/")->createDir("mydir", this->MODE_PUBLIC, 0, 0);
this->LoadDir("/mydir")->createAndOpenFile("mynestedfile", this->MODE_PUBLIC, 0, 0);
file_nested = this->LoadFile("/mydir/mynestedfile");
file_nested = cpputils::to_unique_ptr(this->LoadFile("/mydir/mynestedfile"));
}
std::unique_ptr<fspp::File> file_root;
std::unique_ptr<fspp::File> file_nested;

View File

@ -3,7 +3,7 @@
#define FSPP_IMPL_FILESYSTEM_H_
#include <boost/filesystem.hpp>
#include <memory>
#include <messmer/cpp-utils/unique_ref.h>
#include <sys/stat.h>
#include <sys/statvfs.h>
#include "../fs_interface/Dir.h"
@ -40,7 +40,7 @@ public:
virtual void utimens(const boost::filesystem::path &path, const timespec times[2]) = 0;
virtual void statfs(const boost::filesystem::path &path, struct statvfs *fsstat) = 0;
//TODO We shouldn't use Dir::Entry here, that's in another layer
virtual std::unique_ptr<std::vector<Dir::Entry>> readDir(const boost::filesystem::path &path) = 0;
virtual cpputils::unique_ref<std::vector<Dir::Entry>> readDir(const boost::filesystem::path &path) = 0;
//TODO Test createSymlink
virtual void createSymlink(const boost::filesystem::path &to, const boost::filesystem::path &from, uid_t uid, gid_t gid) = 0;
//TODO Test readSymlink

View File

@ -1,6 +1,5 @@
#include "FilesystemImpl.h"
#include <memory>
#include <fcntl.h>
#include "../fs_interface/Device.h"
#include "../fs_interface/Dir.h"
@ -10,15 +9,15 @@
#include "../fs_interface/File.h"
#include "messmer/cpp-utils/pointer.h"
#include <messmer/cpp-utils/unique_ref.h>
using namespace fspp;
using cpputils::dynamic_pointer_move;
using std::unique_ptr;
using std::make_unique;
using cpputils::unique_ref;
using cpputils::make_unique_ref;
using std::vector;
using std::string;
using boost::none;
namespace bf = boost::filesystem;
@ -29,31 +28,40 @@ FilesystemImpl::FilesystemImpl(Device *device)
FilesystemImpl::~FilesystemImpl() {
}
unique_ptr<File> FilesystemImpl::LoadFile(const bf::path &path) {
unique_ref<File> FilesystemImpl::LoadFile(const bf::path &path) {
auto node = _device->Load(path);
auto file = dynamic_pointer_move<File>(node);
if (!file) {
if (node == none) {
throw fuse::FuseErrnoException(EIO);
}
auto file = dynamic_pointer_move<File>(*node);
if (file == none) {
throw fuse::FuseErrnoException(EISDIR);
}
return file;
return std::move(*file);
}
unique_ptr<Dir> FilesystemImpl::LoadDir(const bf::path &path) {
unique_ref<Dir> FilesystemImpl::LoadDir(const bf::path &path) {
auto node = _device->Load(path);
auto dir = dynamic_pointer_move<Dir>(node);
if (!dir) {
if (node == none) {
throw fuse::FuseErrnoException(EIO);
}
auto dir = dynamic_pointer_move<Dir>(*node);
if (dir == none) {
throw fuse::FuseErrnoException(ENOTDIR);
}
return dir;
return std::move(*dir);
}
unique_ptr<Symlink> FilesystemImpl::LoadSymlink(const bf::path &path) {
unique_ref<Symlink> FilesystemImpl::LoadSymlink(const bf::path &path) {
auto node = _device->Load(path);
auto dir = dynamic_pointer_move<Symlink>(node);
if (!dir) {
if (node == none) {
throw fuse::FuseErrnoException(EIO);
}
auto lnk = dynamic_pointer_move<Symlink>(*node);
if (lnk == none) {
throw fuse::FuseErrnoException(ENOTDIR);
}
return dir;
return std::move(*lnk);
}
int FilesystemImpl::openFile(const bf::path &path, int flags) {
@ -74,7 +82,12 @@ void FilesystemImpl::closeFile(int descriptor) {
}
void FilesystemImpl::lstat(const bf::path &path, struct ::stat *stbuf) {
_device->Load(path)->stat(stbuf);
auto node = _device->Load(path);
if(node == none) {
throw fuse::FuseErrnoException(ENOENT);
} else {
(*node)->stat(stbuf);
}
}
void FilesystemImpl::fstat(int descriptor, struct ::stat *stbuf) {
@ -82,11 +95,21 @@ void FilesystemImpl::fstat(int descriptor, struct ::stat *stbuf) {
}
void FilesystemImpl::chmod(const boost::filesystem::path &path, mode_t mode) {
_device->Load(path)->chmod(mode);
auto node = _device->Load(path);
if(node == none) {
throw fuse::FuseErrnoException(ENOENT);
} else {
(*node)->chmod(mode);
}
}
void FilesystemImpl::chown(const boost::filesystem::path &path, uid_t uid, gid_t gid) {
_device->Load(path)->chown(uid, gid);
auto node = _device->Load(path);
if(node == none) {
throw fuse::FuseErrnoException(ENOENT);
} else {
(*node)->chown(uid, gid);
}
}
void FilesystemImpl::truncate(const bf::path &path, off_t size) {
@ -114,7 +137,12 @@ void FilesystemImpl::fdatasync(int descriptor) {
}
void FilesystemImpl::access(const bf::path &path, int mask) {
_device->Load(path)->access(mask);
auto node = _device->Load(path);
if(node == none) {
throw fuse::FuseErrnoException(ENOENT);
} else {
(*node)->access(mask);
}
}
int FilesystemImpl::createAndOpenFile(const bf::path &path, mode_t mode, uid_t uid, gid_t gid) {
@ -142,17 +170,25 @@ void FilesystemImpl::unlink(const bf::path &path) {
void FilesystemImpl::rename(const bf::path &from, const bf::path &to) {
auto node = _device->Load(from);
node->rename(to);
if(node == none) {
throw fuse::FuseErrnoException(ENOENT);
} else {
(*node)->rename(to);
}
}
unique_ptr<vector<Dir::Entry>> FilesystemImpl::readDir(const bf::path &path) {
unique_ref<vector<Dir::Entry>> FilesystemImpl::readDir(const bf::path &path) {
auto dir = LoadDir(path);
return dir->children();
}
void FilesystemImpl::utimens(const bf::path &path, const timespec times[2]) {
auto node = _device->Load(path);
node->utimens(times);
if(node == none) {
throw fuse::FuseErrnoException(ENOENT);
} else {
(*node)->utimens(times);
}
}
void FilesystemImpl::statfs(const bf::path &path, struct statvfs *fsstat) {

View File

@ -5,7 +5,7 @@
#include "FuseOpenFileList.h"
#include "../fuse/Filesystem.h"
#include "messmer/cpp-utils/macros.h"
#include <messmer/cpp-utils/unique_ref.h>
namespace fspp {
class Node;
@ -37,16 +37,16 @@ public:
void rmdir(const boost::filesystem::path &path) override;
void unlink(const boost::filesystem::path &path) override;
void rename(const boost::filesystem::path &from, const boost::filesystem::path &to) override;
std::unique_ptr<std::vector<Dir::Entry>> readDir(const boost::filesystem::path &path) override;
cpputils::unique_ref<std::vector<Dir::Entry>> readDir(const boost::filesystem::path &path) override;
void utimens(const boost::filesystem::path &path, const timespec times[2]) override;
void statfs(const boost::filesystem::path &path, struct statvfs *fsstat) override;
void createSymlink(const boost::filesystem::path &to, const boost::filesystem::path &from, uid_t uid, gid_t gid) override;
void readSymlink(const boost::filesystem::path &path, char *buf, size_t size) override;
private:
std::unique_ptr<File> LoadFile(const boost::filesystem::path &path);
std::unique_ptr<Dir> LoadDir(const boost::filesystem::path &path);
std::unique_ptr<Symlink> LoadSymlink(const boost::filesystem::path &path);
cpputils::unique_ref<File> LoadFile(const boost::filesystem::path &path);
cpputils::unique_ref<Dir> LoadDir(const boost::filesystem::path &path);
cpputils::unique_ref<Symlink> LoadSymlink(const boost::filesystem::path &path);
int openFile(const File &file, int flags);
Device *_device;

View File

@ -14,7 +14,7 @@ public:
FuseOpenFileList();
virtual ~FuseOpenFileList();
int open(std::unique_ptr<OpenFile> file);
int open(cpputils::unique_ref<OpenFile> file);
OpenFile *get(int descriptor);
void close(int descriptor);
@ -31,7 +31,7 @@ inline FuseOpenFileList::FuseOpenFileList()
inline FuseOpenFileList::~FuseOpenFileList() {
}
inline int FuseOpenFileList::open(std::unique_ptr<OpenFile> file) {
inline int FuseOpenFileList::open(cpputils::unique_ref<OpenFile> file) {
return _open_files.add(std::move(file));
}

View File

@ -3,10 +3,9 @@
#define FSPP_FUSE_IDLIST_H_
#include <map>
#include <memory>
#include <mutex>
#include <stdexcept>
#include "messmer/cpp-utils/macros.h"
#include <messmer/cpp-utils/unique_ref.h>
namespace fspp {
@ -16,12 +15,12 @@ public:
IdList();
virtual ~IdList();
int add(std::unique_ptr<Entry> entry);
int add(cpputils::unique_ref<Entry> entry);
Entry *get(int id);
const Entry *get(int id) const;
void remove(int id);
private:
std::map<int, std::unique_ptr<Entry>> _entries;
std::map<int, cpputils::unique_ref<Entry>> _entries;
int _id_counter;
mutable std::mutex _mutex;
@ -38,11 +37,11 @@ IdList<Entry>::~IdList() {
}
template<class Entry>
int IdList<Entry>::add(std::unique_ptr<Entry> entry) {
int IdList<Entry>::add(cpputils::unique_ref<Entry> entry) {
std::lock_guard<std::mutex> lock(_mutex);
//TODO Reuse IDs (ids = descriptors)
int new_id = ++_id_counter;
_entries[new_id] = std::move(entry);
_entries.insert(std::make_pair(new_id, std::move(entry)));
return new_id;
}

View File

@ -2,6 +2,7 @@
#include <messmer/cpp-utils/data/Data.h>
#include "../../testutils/InMemoryFile.h"
#include "testutils/FuseReadTest.h"
#include <messmer/cpp-utils/unique_ref.h>
#include "../../../fuse/FuseErrnoException.h"
@ -51,7 +52,9 @@ public:
std::unique_ptr<InMemoryFile> testFile;
TestData testData;
FuseReadReturnedDataTest(): testFile(nullptr), testData(GetParam()) {
FuseReadReturnedDataTest()
: testFile(nullptr),
testData(GetParam()) {
testFile = make_unique<InMemoryFile>(DataFixture::generate(testData.fileSize()));
ReturnIsFileOnLstatWithSize(FILENAME, testData.fileSize());
OnOpenReturnFileDescriptor(FILENAME, 0);

View File

@ -4,8 +4,6 @@ using ::testing::_;
using ::testing::StrEq;
using ::testing::Return;
using std::make_unique;
using std::unique_ptr;
using std::vector;
using std::string;

View File

@ -8,8 +8,6 @@ using ::testing::Throw;
using ::testing::WithParamInterface;
using ::testing::Values;
using std::make_unique;
using std::unique_ptr;
using std::vector;
using std::string;

View File

@ -1,5 +1,5 @@
#include "testutils/FuseReadDirTest.h"
#include <messmer/cpp-utils/unique_ref.h>
#include "../../../fuse/FuseErrnoException.h"
using ::testing::_;
@ -8,16 +8,16 @@ using ::testing::Throw;
using ::testing::WithParamInterface;
using ::testing::Values;
using std::make_unique;
using std::unique_ptr;
using cpputils::unique_ref;
using cpputils::make_unique_ref;
using std::vector;
using std::string;
using namespace fspp::fuse;
using fspp::Dir;
unique_ptr<vector<string>> LARGE_DIR(int num_entries) {
auto result = make_unique<vector<string>>();
unique_ref<vector<string>> LARGE_DIR(int num_entries) {
auto result = make_unique_ref<vector<string>>();
result->reserve(num_entries);
for(int i=0; i<num_entries; ++i) {
result->push_back("File "+std::to_string(i)+" file");

View File

@ -1,7 +1,7 @@
#include "FuseReadDirTest.h"
using std::unique_ptr;
using std::make_unique;
using cpputils::unique_ref;
using cpputils::make_unique_ref;
using std::vector;
using std::string;
using std::initializer_list;
@ -9,12 +9,12 @@ using std::initializer_list;
using ::testing::Action;
using ::testing::Return;
unique_ptr<vector<string>> FuseReadDirTest::ReadDir(const char *dirname) {
unique_ref<vector<string>> FuseReadDirTest::ReadDir(const char *dirname) {
auto fs = TestFS();
DIR *dir = openDir(fs.get(), dirname);
auto result = make_unique<vector<string>>();
auto result = make_unique_ref<vector<string>>();
readDirEntries(dir, result.get());
closeDir(dir);
return result;
@ -30,7 +30,7 @@ int FuseReadDirTest::ReadDirReturnError(const char *dirname) {
return errno;
}
auto result = make_unique<vector<string>>();
auto result = make_unique_ref<vector<string>>();
int error = readDirEntriesAllowError(dir, result.get());
closeDir(dir);
return error;

View File

@ -10,7 +10,7 @@ class FuseReadDirTest: public FuseTest {
public:
const char *DIRNAME = "/mydir";
std::unique_ptr<std::vector<std::string>> ReadDir(const char *dirname);
cpputils::unique_ref<std::vector<std::string>> ReadDir(const char *dirname);
int ReadDirReturnError(const char *dirname);
static ::testing::Action<std::vector<fspp::Dir::Entry>*(const char*)> ReturnDirEntries(std::vector<std::string> entries);

View File

@ -49,7 +49,9 @@ public:
std::unique_ptr<WriteableInMemoryFile> testFile;
TestData testData;
FuseWriteDataTest(): testFile(nullptr), testData(GetParam()) {
FuseWriteDataTest()
: testFile(nullptr),
testData(GetParam()) {
testFile = make_unique<WriteableInMemoryFile>(DataFixture::generate(testData.fileSize(), 1));
ReturnIsFileOnLstatWithSize(FILENAME, testData.fileSize());
OnOpenReturnFileDescriptor(FILENAME, 0);

View File

@ -5,8 +5,7 @@
#include <stdexcept>
using std::unique_ptr;
using std::make_unique;
using cpputils::make_unique_ref;
using namespace fspp;
@ -37,7 +36,7 @@ struct FuseOpenFileListTest: public ::testing::Test {
FuseOpenFileList list;
int open(int fileid, int flags) {
return list.open(make_unique<MockOpenFile>(fileid, flags));
return list.open(make_unique_ref<MockOpenFile>(fileid, flags));
}
int open() {
return open(FILEID1, FILEID2);

View File

@ -3,7 +3,7 @@
#include "../../impl/IdList.h"
#include <stdexcept>
using std::make_unique;
using cpputils::make_unique_ref;
using namespace fspp;
@ -20,7 +20,7 @@ struct IdListTest: public ::testing::Test {
IdList<MyObj> list;
int add(int num) {
return list.add(make_unique<MyObj>(num));
return list.add(make_unique_ref<MyObj>(num));
}
int add() {
return add(OBJ1);

View File

@ -7,8 +7,8 @@ using ::testing::Throw;
using ::testing::Action;
using ::testing::Invoke;
using std::unique_ptr;
using std::make_unique;
using cpputils::unique_ref;
using cpputils::make_unique_ref;
namespace bf = boost::filesystem;
@ -44,8 +44,8 @@ FuseTest::FuseTest(): fsimpl() {
ON_CALL(fsimpl, readSymlink(_,_,_)).WillByDefault(defaultAction);
}
unique_ptr<FuseTest::TempTestFS> FuseTest::TestFS() {
return make_unique<TempTestFS>(&fsimpl);
unique_ref<FuseTest::TempTestFS> FuseTest::TestFS() {
return make_unique_ref<TempTestFS>(&fsimpl);
}
FuseTest::TempTestFS::TempTestFS(MockFilesystem *fsimpl): _mountDir(), _fuse(fsimpl), _fuse_thread(&_fuse) {

View File

@ -64,8 +64,8 @@ public:
return rename(from.c_str(), to.c_str());
}
MOCK_METHOD2(rename, void(const char*, const char*));
std::unique_ptr<std::vector<fspp::Dir::Entry>> readDir(const boost::filesystem::path &path) {
return std::unique_ptr<std::vector<fspp::Dir::Entry>>(readDir(path.c_str()));
cpputils::unique_ref<std::vector<fspp::Dir::Entry>> readDir(const boost::filesystem::path &path) {
return std::move(cpputils::nullcheck(std::unique_ptr<std::vector<fspp::Dir::Entry>>(readDir(path.c_str()))).get());
}
MOCK_METHOD1(readDir, std::vector<fspp::Dir::Entry>*(const char*));
void utimens(const boost::filesystem::path &path, const timespec ts[2]) override {
@ -100,7 +100,7 @@ public:
FuseThread _fuse_thread;
};
std::unique_ptr<TempTestFS> TestFS();
cpputils::unique_ref<TempTestFS> TestFS();
MockFilesystem fsimpl;