mirror of
https://github.com/slsdetectorgroup/aare.git
synced 2025-06-12 07:17:13 +02:00
Merge pull request #27 from slsdetectorgroup/feature/restructure-FileHandler
add contextManager to hanldle files
This commit is contained in:
34
file_io/include/aare/ContextManager.hpp
Normal file
34
file_io/include/aare/ContextManager.hpp
Normal file
@ -0,0 +1,34 @@
|
||||
#include "aare/File.hpp"
|
||||
#include "aare/FileFactory.hpp"
|
||||
#include <filesystem>
|
||||
#include <memory>
|
||||
|
||||
|
||||
/**
|
||||
* @brief A class to manage the context of the files and network connections
|
||||
* closes the files and network connections when the context is destroyed
|
||||
*/
|
||||
class ContextManager {
|
||||
public:
|
||||
ContextManager() = default;
|
||||
|
||||
File *get_file(std::filesystem::path fname) {
|
||||
auto tmp_file = FileFactory::load_file(fname);
|
||||
this->files.push_back(tmp_file);
|
||||
return tmp_file;
|
||||
}
|
||||
|
||||
// prevent default copying, it can delete the file twice
|
||||
ContextManager(const ContextManager &) = delete;
|
||||
ContextManager &operator=(const ContextManager &) = delete;
|
||||
|
||||
~ContextManager() {
|
||||
for (auto f : files) {
|
||||
delete f;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<File *> files;
|
||||
// std::vector<NetworkConnection*> connections;
|
||||
};
|
@ -1,24 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include "aare/defs.hpp"
|
||||
#include "aare/Frame.hpp"
|
||||
#include "SubFile.hpp"
|
||||
#include "aare/Frame.hpp"
|
||||
#include "aare/defs.hpp"
|
||||
#include <filesystem>
|
||||
#include <fmt/core.h>
|
||||
#include <iostream>
|
||||
|
||||
|
||||
class File {
|
||||
|
||||
public:
|
||||
virtual Frame* get_frame(size_t frame_number) = 0;
|
||||
virtual Frame get_frame(size_t frame_number) =0;
|
||||
|
||||
private:
|
||||
//comment
|
||||
|
||||
// comment
|
||||
|
||||
public:
|
||||
virtual ~File() = default;
|
||||
virtual ~File() = default;
|
||||
std::filesystem::path fname;
|
||||
std::filesystem::path base_path;
|
||||
std::string base_name, ext;
|
||||
@ -35,11 +33,9 @@ class File {
|
||||
ssize_t cols{};
|
||||
ssize_t bitdepth{};
|
||||
// File();
|
||||
|
||||
|
||||
inline size_t bytes_per_frame() const { return rows * cols * bitdepth / 8; }
|
||||
inline size_t pixels() const { return rows * cols; }
|
||||
|
||||
|
||||
// size_t total_frames();
|
||||
};
|
@ -9,6 +9,12 @@ class FileFactory{
|
||||
public:
|
||||
static FileFactory* get_factory(std::filesystem::path);
|
||||
// virtual int deleteFile() = 0;
|
||||
static File* load_file(std::filesystem::path p){
|
||||
auto factory = get_factory(p);
|
||||
File* tmp= factory->load_file();
|
||||
delete factory;
|
||||
return tmp;
|
||||
};
|
||||
virtual File* load_file()=0;//TODO: add option to load all file to memory or keep it on disk
|
||||
virtual void parse_metadata(File*)=0;
|
||||
virtual void parse_fname(File*)=0;
|
||||
|
@ -1,28 +0,0 @@
|
||||
#include <filesystem>
|
||||
#include "aare/FileFactory.hpp"
|
||||
#include "aare/File.hpp"
|
||||
|
||||
class FileHandler{
|
||||
private:
|
||||
std::filesystem::path fpath;
|
||||
FileFactory* fileFactory;
|
||||
File* f;
|
||||
|
||||
public:
|
||||
FileHandler(std::filesystem::path fname){
|
||||
this->fpath = fname;
|
||||
this->fileFactory= FileFactory::get_factory(fname);
|
||||
this->f= fileFactory->load_file();
|
||||
delete fileFactory;
|
||||
}
|
||||
|
||||
Frame* get_frame(int index){
|
||||
return f->get_frame(index);
|
||||
}
|
||||
|
||||
|
||||
|
||||
~FileHandler(){
|
||||
delete f;
|
||||
}
|
||||
};
|
@ -9,7 +9,7 @@ class JsonFile : public File {
|
||||
using config = RawFileConfig;
|
||||
public:
|
||||
|
||||
Frame *get_frame(size_t frame_number);
|
||||
Frame get_frame(size_t frame_number);
|
||||
int n_subfiles;
|
||||
std::vector<SubFile *> subfiles;
|
||||
int subfile_rows, subfile_cols;
|
||||
|
@ -1,11 +1,12 @@
|
||||
#include "aare/FileFactory.hpp"
|
||||
#include "aare/JsonFile.hpp"
|
||||
class JsonFileFactory: public FileFactory
|
||||
{
|
||||
private:
|
||||
/* data */
|
||||
public:
|
||||
JsonFileFactory(std::filesystem::path fpath);
|
||||
File* load_file() override;
|
||||
virtual JsonFile* load_file() override;
|
||||
void parse_metadata(File*) override;
|
||||
void parse_fname(File*) override;
|
||||
void open_subfiles(File*);
|
||||
|
@ -11,7 +11,7 @@ class NumpyFile : public File {
|
||||
|
||||
public:
|
||||
NumpyFile(std::filesystem::path fname);
|
||||
Frame *get_frame(size_t frame_number) override;
|
||||
Frame get_frame(size_t frame_number) override;
|
||||
header_t header{};
|
||||
static constexpr std::array<char, 6> magic_str{'\x93', 'N', 'U', 'M', 'P', 'Y'};
|
||||
uint8_t major_ver_{};
|
||||
@ -24,4 +24,10 @@ class NumpyFile : public File {
|
||||
return std::accumulate(header.shape.begin() + 1, header.shape.end(), 1, std::multiplies<uint64_t>());
|
||||
};
|
||||
inline ssize_t bytes_per_frame() { return header.dtype.itemsize * pixels_per_frame(); };
|
||||
~NumpyFile(){
|
||||
if (fp != nullptr) {
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
}
|
||||
};
|
@ -14,7 +14,7 @@ class NumpyFileFactory : public FileFactory {
|
||||
public:
|
||||
NumpyFileFactory(std::filesystem::path fpath);
|
||||
void parse_metadata(File *_file) override;
|
||||
File *load_file() override;
|
||||
NumpyFile* load_file() override;
|
||||
void parse_fname(File*){};
|
||||
|
||||
};
|
Reference in New Issue
Block a user