Program Listing for File graph_manager.h#
↰ Return to documentation for file (include/nodes-graph/graph_manager.h)
#pragma once
#include <filesystem>
#include <map>
#include "nodes-graph/graph.h"
#include "nodes-graph/small_map.h"
namespace ng {
class GraphManager {
public:
enum class Owner {
User,
Group,
Global,
Unknown
};
struct HeadProperties {
Owner owner;
std::filesystem::path path;
bool readable;
bool writable;
HeadProperties(Owner owner=Owner::Unknown, const std::filesystem::path& path="", bool readable=false, bool writable=false)
: owner(owner), path(path), readable(readable), writable(writable) {}
};
using head_id_t = std::size_t;
using library_t = std::map<qualifier_t, head_id_t>;
using heads_t = SmallMap<head_id_t, HeadProperties>;
static bool can_write(const std::filesystem::path& path);
static bool can_read(const std::filesystem::path& path);
private:
heads_t heads_;
library_t library_; // graphs found on the disk, and the head they were found in
head_id_t next_head_ = 1;
graph_id_t next_graph_ = 1;
std::map<graph_id_t, Graph> graphs_; // all graphs, including anonymous ones
std::map<qualifier_t, graph_id_t> loaded_graphs_; // graphs that have been loaded or saved, by qualifier
private:
GraphManager() = default;
void discover_head(head_id_t head_id);
std::filesystem::path find_graph(const qualifier_t& q) const;
public:
GraphManager(const GraphManager&) = delete;
GraphManager& operator=(const GraphManager&) = delete;
~GraphManager() = default;
static GraphManager& instance();
// --- Getters
inline const library_t& get_library() const { return library_; }
inline const heads_t& get_heads() const { return heads_; }
inline const Graph& get_graph(graph_id_t id) const { return graphs_.at(id); }
inline Graph& get_graph(graph_id_t id) { return graphs_.at(id); }
inline bool has_graph(graph_id_t id) const { return graphs_.count(id) > 0; }
// --- Graphs in memory
graph_id_t new_graph(const std::string& name="");
bool close_graph(graph_id_t id);
// --- Heads
head_id_t add_head(const std::filesystem::path& path, Owner owner=Owner::User);
void refresh();
// --- Disk
bool exists(const qualifier_t& q) const;
Status save_graph(graph_id_t id);
Status save_graph(graph_id_t id, head_id_t head_id);
Graph raw_read(const qualifier_t& q) const;
graph_id_t load_graph(const qualifier_t& q, bool force=false);
void reset();
};
} // namespace ng