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_ #define FSPP_DEVICE_H_
#include <boost/filesystem.hpp> #include <boost/filesystem.hpp>
#include <memory> #include <messmer/cpp-utils/unique_ref.h>
#include <sys/statvfs.h> #include <sys/statvfs.h>
namespace fspp { namespace fspp {
@ -14,7 +14,7 @@ public:
virtual ~Device() {} virtual ~Device() {}
virtual void statfs(const boost::filesystem::path &path, struct ::statvfs *fsstat) = 0; 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_ #define FSPP_DIR_H_
#include "Node.h" #include "Node.h"
#include <memory> #include <messmer/cpp-utils/unique_ref.h>
#include <string> #include <string>
namespace fspp { namespace fspp {
@ -26,13 +26,13 @@ public:
std::string name; 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 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; 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 //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<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_ #define FSPP_FILE_H_
#include "Node.h" #include "Node.h"
#include <memory> #include <messmer/cpp-utils/unique_ref.h>
namespace fspp { namespace fspp {
class Device; class Device;
@ -13,7 +13,7 @@ class File: public virtual Node {
public: public:
virtual ~File() {} 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; virtual void truncate(off_t size) const = 0;
}; };

View File

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

View File

@ -2,10 +2,10 @@
#define MESSMER_FSPP_FSTEST_TESTUTILS_FILESYSTEMTEST_H_ #define MESSMER_FSPP_FSTEST_TESTUTILS_FILESYSTEMTEST_H_
#include <google/gtest/gtest.h> #include <google/gtest/gtest.h>
#include <memory>
#include <type_traits> #include <type_traits>
#include <boost/static_assert.hpp> #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/Device.h"
#include "../../fs_interface/Dir.h" #include "../../fs_interface/Dir.h"
@ -14,7 +14,7 @@
class FileSystemTestFixture { class FileSystemTestFixture {
public: public:
virtual std::unique_ptr<fspp::Device> createDevice() = 0; virtual cpputils::unique_ref<fspp::Device> createDevice() = 0;
}; };
template<class ConcreteFileSystemTestFixture> template<class ConcreteFileSystemTestFixture>
@ -28,22 +28,24 @@ public:
FileSystemTest(): fixture(), device(fixture.createDevice()) {} FileSystemTest(): fixture(), device(fixture.createDevice()) {}
ConcreteFileSystemTestFixture fixture; 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; 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 loaded = device->Load(path);
auto dir = cpputils::dynamic_pointer_move<fspp::Dir>(loaded); EXPECT_NE(boost::none, loaded);
EXPECT_NE(nullptr, dir.get()); auto dir = cpputils::dynamic_pointer_move<fspp::Dir>(*loaded);
return dir; 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 loaded = device->Load(path);
auto file = cpputils::dynamic_pointer_move<fspp::File>(loaded); EXPECT_NE(boost::none, loaded);
EXPECT_NE(nullptr, file.get()); auto file = cpputils::dynamic_pointer_move<fspp::File>(*loaded);
return file; EXPECT_NE(boost::none, file);
return std::move(*file);
} }
}; };

View File

@ -3,19 +3,18 @@
#include "FileSystemTest.h" #include "FileSystemTest.h"
#include <messmer/cpp-utils/data/Data.h> #include <messmer/cpp-utils/data/Data.h>
#include <messmer/cpp-utils/unique_ref.h>
#include <memory>
template<class ConcreteFileSystemTestFixture> template<class ConcreteFileSystemTestFixture>
class FileTest: public FileSystemTest<ConcreteFileSystemTestFixture> { class FileTest: public FileSystemTest<ConcreteFileSystemTestFixture> {
public: public:
FileTest() { FileTest() {
this->LoadDir("/")->createAndOpenFile("myfile", this->MODE_PUBLIC, 0, 0); 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("/")->createDir("mydir", this->MODE_PUBLIC, 0, 0);
this->LoadDir("/mydir")->createAndOpenFile("mynestedfile", 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_root;
std::unique_ptr<fspp::File> file_nested; std::unique_ptr<fspp::File> file_nested;

View File

@ -3,7 +3,7 @@
#define FSPP_IMPL_FILESYSTEM_H_ #define FSPP_IMPL_FILESYSTEM_H_
#include <boost/filesystem.hpp> #include <boost/filesystem.hpp>
#include <memory> #include <messmer/cpp-utils/unique_ref.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/statvfs.h> #include <sys/statvfs.h>
#include "../fs_interface/Dir.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 utimens(const boost::filesystem::path &path, const timespec times[2]) = 0;
virtual void statfs(const boost::filesystem::path &path, struct statvfs *fsstat) = 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 //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 //TODO Test createSymlink
virtual void createSymlink(const boost::filesystem::path &to, const boost::filesystem::path &from, uid_t uid, gid_t gid) = 0; virtual void createSymlink(const boost::filesystem::path &to, const boost::filesystem::path &from, uid_t uid, gid_t gid) = 0;
//TODO Test readSymlink //TODO Test readSymlink

View File

@ -1,6 +1,5 @@
#include "FilesystemImpl.h" #include "FilesystemImpl.h"
#include <memory>
#include <fcntl.h> #include <fcntl.h>
#include "../fs_interface/Device.h" #include "../fs_interface/Device.h"
#include "../fs_interface/Dir.h" #include "../fs_interface/Dir.h"
@ -10,15 +9,15 @@
#include "../fs_interface/File.h" #include "../fs_interface/File.h"
#include "messmer/cpp-utils/pointer.h" #include <messmer/cpp-utils/unique_ref.h>
using namespace fspp; using namespace fspp;
using cpputils::dynamic_pointer_move; using cpputils::dynamic_pointer_move;
using cpputils::unique_ref;
using std::unique_ptr; using cpputils::make_unique_ref;
using std::make_unique;
using std::vector; using std::vector;
using std::string; using std::string;
using boost::none;
namespace bf = boost::filesystem; namespace bf = boost::filesystem;
@ -29,31 +28,40 @@ FilesystemImpl::FilesystemImpl(Device *device)
FilesystemImpl::~FilesystemImpl() { 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 node = _device->Load(path);
auto file = dynamic_pointer_move<File>(node); if (node == none) {
if (!file) { throw fuse::FuseErrnoException(EIO);
}
auto file = dynamic_pointer_move<File>(*node);
if (file == none) {
throw fuse::FuseErrnoException(EISDIR); 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 node = _device->Load(path);
auto dir = dynamic_pointer_move<Dir>(node); if (node == none) {
if (!dir) { throw fuse::FuseErrnoException(EIO);
}
auto dir = dynamic_pointer_move<Dir>(*node);
if (dir == none) {
throw fuse::FuseErrnoException(ENOTDIR); 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 node = _device->Load(path);
auto dir = dynamic_pointer_move<Symlink>(node); if (node == none) {
if (!dir) { throw fuse::FuseErrnoException(EIO);
}
auto lnk = dynamic_pointer_move<Symlink>(*node);
if (lnk == none) {
throw fuse::FuseErrnoException(ENOTDIR); throw fuse::FuseErrnoException(ENOTDIR);
} }
return dir; return std::move(*lnk);
} }
int FilesystemImpl::openFile(const bf::path &path, int flags) { 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) { 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) { 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) { 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) { 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) { 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) { 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) { 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) { void FilesystemImpl::rename(const bf::path &from, const bf::path &to) {
auto node = _device->Load(from); 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); auto dir = LoadDir(path);
return dir->children(); return dir->children();
} }
void FilesystemImpl::utimens(const bf::path &path, const timespec times[2]) { void FilesystemImpl::utimens(const bf::path &path, const timespec times[2]) {
auto node = _device->Load(path); 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) { void FilesystemImpl::statfs(const bf::path &path, struct statvfs *fsstat) {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -10,7 +10,7 @@ class FuseReadDirTest: public FuseTest {
public: public:
const char *DIRNAME = "/mydir"; 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); int ReadDirReturnError(const char *dirname);
static ::testing::Action<std::vector<fspp::Dir::Entry>*(const char*)> ReturnDirEntries(std::vector<std::string> entries); 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; std::unique_ptr<WriteableInMemoryFile> testFile;
TestData testData; TestData testData;
FuseWriteDataTest(): testFile(nullptr), testData(GetParam()) { FuseWriteDataTest()
: testFile(nullptr),
testData(GetParam()) {
testFile = make_unique<WriteableInMemoryFile>(DataFixture::generate(testData.fileSize(), 1)); testFile = make_unique<WriteableInMemoryFile>(DataFixture::generate(testData.fileSize(), 1));
ReturnIsFileOnLstatWithSize(FILENAME, testData.fileSize()); ReturnIsFileOnLstatWithSize(FILENAME, testData.fileSize());
OnOpenReturnFileDescriptor(FILENAME, 0); OnOpenReturnFileDescriptor(FILENAME, 0);

View File

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

View File

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

View File

@ -7,8 +7,8 @@ using ::testing::Throw;
using ::testing::Action; using ::testing::Action;
using ::testing::Invoke; using ::testing::Invoke;
using std::unique_ptr; using cpputils::unique_ref;
using std::make_unique; using cpputils::make_unique_ref;
namespace bf = boost::filesystem; namespace bf = boost::filesystem;
@ -44,8 +44,8 @@ FuseTest::FuseTest(): fsimpl() {
ON_CALL(fsimpl, readSymlink(_,_,_)).WillByDefault(defaultAction); ON_CALL(fsimpl, readSymlink(_,_,_)).WillByDefault(defaultAction);
} }
unique_ptr<FuseTest::TempTestFS> FuseTest::TestFS() { unique_ref<FuseTest::TempTestFS> FuseTest::TestFS() {
return make_unique<TempTestFS>(&fsimpl); return make_unique_ref<TempTestFS>(&fsimpl);
} }
FuseTest::TempTestFS::TempTestFS(MockFilesystem *fsimpl): _mountDir(), _fuse(fsimpl), _fuse_thread(&_fuse) { 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()); return rename(from.c_str(), to.c_str());
} }
MOCK_METHOD2(rename, void(const char*, const char*)); MOCK_METHOD2(rename, void(const char*, const char*));
std::unique_ptr<std::vector<fspp::Dir::Entry>> readDir(const boost::filesystem::path &path) { cpputils::unique_ref<std::vector<fspp::Dir::Entry>> readDir(const boost::filesystem::path &path) {
return std::unique_ptr<std::vector<fspp::Dir::Entry>>(readDir(path.c_str())); 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*)); MOCK_METHOD1(readDir, std::vector<fspp::Dir::Entry>*(const char*));
void utimens(const boost::filesystem::path &path, const timespec ts[2]) override { void utimens(const boost::filesystem::path &path, const timespec ts[2]) override {
@ -100,7 +100,7 @@ public:
FuseThread _fuse_thread; FuseThread _fuse_thread;
}; };
std::unique_ptr<TempTestFS> TestFS(); cpputils::unique_ref<TempTestFS> TestFS();
MockFilesystem fsimpl; MockFilesystem fsimpl;