Skip to content

Instantly share code, notes, and snippets.

@anthonymorast
Last active Apr 5, 2022
Embed
What would you like to do?
#ifndef FILE_MANAGER
#define FILE_MANAGER
#include <vector>
#include <fstream>
#include <string>
#include <map>
#include <memory>
#include <chrono>
#include <pthread.h>
#include <thread>
#include <algorithm>
#include "./PrintableObject.h"
namespace FM
{
// a JobVector is a vector that defines the jobs (i.e. the objects to be written)
// sometimes you get tired of typing std::vector<std::shared_ptr<PrintableObject>> and managing the carats
using JobVector = std::vector<std::shared_ptr<PrintableObject>>;
// Makes it easier to see where runtime errors are coming from
class FileManagerException : public std::runtime_error
{
public:
~FileManagerException() {}
FileManagerException(const std::string msg) : runtime_error(msg) {}
};
class FileManager
{
public:
FileManager(bool append=false, int wait_timeout=120) : _append(append), _max_stop_wait(wait_timeout) {}
void stop(); // wraps up the queues and stops the manager PID
void start(); // starts the manager PID
void initFile(std::string filename); // creates entries for the filename in the maps
void queueJob(std::string filename, JobVector job);
private:
std::map<std::string, std::shared_ptr<std::ofstream>> _filename_to_filehandle;
std::map<std::string, std::vector<JobVector>> _filename_to_jobs; // store jobs for each filename
std::vector<std::string> _filenames;
pthread_t _manager_thread_handle;
bool _append;
int _max_stop_wait; // Not yet implemented
void _manager(); // thread used to manage the queues
};
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment