ConfigFileSystem.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "../fs.hpp"
4 
6 #include <ivorium_config.hpp>
7 
8 #include <functional>
9 #include <string>
10 
11 namespace iv
12 {
13 
15 {
16 public:
18 
19  ConfigStream( Instance * inst, std::string const & name );
20  ~ConfigStream();
21 
22  Instance * instance() const;
23 
24  virtual void config_stream_changed() = 0;
25 
26  bool stream_exists();
27  void stream_read( std::function< void( std::istream & ) > const & );
28  void stream_write( std::function< void( std::ostream & ) > const & );
29 
33  std::string get_filepath();
34 
35 private:
36  Instance * inst;
37  std::string name;
38 };
39 
43 class ConfigFileSystem : public System
44 {
45 public:
46  //
47  ConfigFileSystem( SystemContainer * sc, std::string const & base_dir );
48  virtual std::string debug_name() const override { return "ConfigFileSystem"; }
49  virtual bool flushSystem() override;
50  virtual void status( TextDebugView * view ) override;
51 
52  //
53  void stream_add_listener( ConfigStream * listener, std::string const & name );
54  void stream_remove_listener( ConfigStream * listener );
55 
56  bool stream_exists( std::string const & name );
57  void stream_read( std::string const & name, std::function< void( std::istream & ) > const & );
58  void stream_write( std::string const & name, std::function< void( std::ostream & ) > const & );
59  std::string get_filepath( std::string const & name );
60 
61 #if IV_CONFIG_FS_ENABLED
62 private:
63  struct StreamListener
64  {
65  ConfigStream * listener;
66  std::string name;
67  fs::file_time_type timestamp;
68 
69  StreamListener( ConfigStream * listener, std::string const & name, fs::file_time_type timestamp ) : listener( listener ), name( name ), timestamp( timestamp ){}
70  };
71 
72 private:
73  fs::path stream_filepath( std::string const & name );
74  fs::file_time_type stream_timestamp( std::string const & name );
75 
76 private:
77  fs::path base_dir;
78 
79  std::vector< StreamListener > stream_listeners;
80 
81  unsigned frame_id;
82 #endif
83 };
84 
85 }