Generalized IdList
This commit is contained in:
parent
9db97ff258
commit
abc8811e17
@ -3,11 +3,11 @@
|
||||
#define CRYFS_LIB_CRYDEVICE_H_
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <cryfs_lib/CryOpenFileList.h>
|
||||
#include <memory>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "utils/macros.h"
|
||||
#include "CryOpenFileList.h"
|
||||
|
||||
namespace cryfs {
|
||||
class CryNode;
|
||||
|
@ -4,11 +4,11 @@
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "CryDevice.h"
|
||||
#include "CryNode.h"
|
||||
#include "utils/macros.h"
|
||||
|
||||
namespace cryfs {
|
||||
class CryDevice;
|
||||
class CryOpenFile;
|
||||
|
||||
class CryFile: public CryNode {
|
||||
|
@ -25,6 +25,7 @@ protected:
|
||||
bf::path base_path() const;
|
||||
const bf::path &path() const;
|
||||
CryDevice *device();
|
||||
const CryDevice *device() const;
|
||||
|
||||
private:
|
||||
CryDevice *const _device;
|
||||
@ -42,6 +43,10 @@ inline const bf::path &CryNode::path() const {
|
||||
}
|
||||
|
||||
inline CryDevice *CryNode::device() {
|
||||
return const_cast<CryDevice*>(const_cast<const CryNode*>(this)->device());
|
||||
}
|
||||
|
||||
inline const CryDevice *CryNode::device() const {
|
||||
return _device;
|
||||
}
|
||||
|
||||
|
@ -4,25 +4,22 @@
|
||||
|
||||
using namespace cryfs;
|
||||
|
||||
CryOpenFileList::~CryOpenFileList() {
|
||||
}
|
||||
|
||||
CryOpenFileList::CryOpenFileList()
|
||||
:_open_files() {
|
||||
}
|
||||
|
||||
CryOpenFileList::~CryOpenFileList() {
|
||||
}
|
||||
|
||||
int CryOpenFileList::open(const CryFile &file, int flags) {
|
||||
//TODO Reuse descriptors
|
||||
int desc = _open_files.size();
|
||||
_open_files[desc] = file.open(flags);
|
||||
return desc;
|
||||
return _open_files.add(file.open(flags));
|
||||
}
|
||||
|
||||
CryOpenFile *CryOpenFileList::get(int descriptor) {
|
||||
return _open_files.at(descriptor).get();
|
||||
return _open_files.get(descriptor);
|
||||
}
|
||||
|
||||
void CryOpenFileList::close(int descriptor) {
|
||||
//The destructor of the stored CryFile::OpenFile closes the file
|
||||
_open_files.erase(descriptor);
|
||||
_open_files.remove(descriptor);
|
||||
}
|
||||
|
@ -4,10 +4,11 @@
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include "utils/macros.h"
|
||||
#include "IdList.h"
|
||||
|
||||
namespace cryfs {
|
||||
class CryFile;
|
||||
class CryOpenFile;
|
||||
class CryFile;
|
||||
|
||||
class CryOpenFileList {
|
||||
public:
|
||||
@ -19,7 +20,7 @@ public:
|
||||
void close(int descriptor);
|
||||
|
||||
private:
|
||||
std::map<int, std::unique_ptr<CryOpenFile>> _open_files;
|
||||
IdList<CryOpenFile> _open_files;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(CryOpenFileList);
|
||||
};
|
||||
|
5
src/cryfs_lib/IdList.cpp
Normal file
5
src/cryfs_lib/IdList.cpp
Normal file
@ -0,0 +1,5 @@
|
||||
#include <cryfs_lib/IdList.h>
|
||||
|
||||
namespace cryfs {
|
||||
|
||||
} /* namespace cryfs */
|
60
src/cryfs_lib/IdList.h
Normal file
60
src/cryfs_lib/IdList.h
Normal file
@ -0,0 +1,60 @@
|
||||
#ifndef CRYFS_LIB_IDLIST_H_
|
||||
#define CRYFS_LIB_IDLIST_H_
|
||||
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include "utils/macros.h"
|
||||
|
||||
namespace cryfs {
|
||||
|
||||
template<class Entry>
|
||||
class IdList {
|
||||
public:
|
||||
IdList();
|
||||
virtual ~IdList();
|
||||
|
||||
int add(std::unique_ptr<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;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(IdList<Entry>)
|
||||
};
|
||||
|
||||
template<class Entry>
|
||||
IdList<Entry>::IdList()
|
||||
: _entries() {
|
||||
}
|
||||
|
||||
template<class Entry>
|
||||
IdList<Entry>::~IdList() {
|
||||
}
|
||||
|
||||
template<class Entry>
|
||||
int IdList<Entry>::add(std::unique_ptr<Entry> entry) {
|
||||
//TODO Reuse IDs (ids = descriptors)
|
||||
int new_id = _entries.size();
|
||||
_entries[new_id] = std::move(entry);
|
||||
return new_id;
|
||||
}
|
||||
|
||||
template<class Entry>
|
||||
Entry *IdList<Entry>::get(int id) {
|
||||
return const_cast<Entry*>(const_cast<const IdList<Entry>*>(this)->get(id));
|
||||
}
|
||||
|
||||
template<class Entry>
|
||||
const Entry *IdList<Entry>::get(int id) const {
|
||||
return _entries.at(id).get();
|
||||
}
|
||||
|
||||
template<class Entry>
|
||||
void IdList<Entry>::remove(int id) {
|
||||
_entries.erase(id);
|
||||
}
|
||||
|
||||
} /* namespace cryfs */
|
||||
|
||||
#endif /* CRYFS_LIB_IDLIST_H_ */
|
Loading…
Reference in New Issue
Block a user