1 #ifndef bstm_cache_h_ 2 #define bstm_cache_h_ 3 //: 4 // \file 5 #include <bstm/bstm_scene.h> 6 #include <bstm/bstm_block.h> 7 #include <bstm/bstm_time_block.h> 8 #include <bstm/basic/bstm_block_id.h> 9 #include <bstm/bstm_data_base.h> 10 11 #include <vbl/vbl_ref_count.h> 12 #include <vbl/vbl_smart_ptr.h> 13 //: top level storage (abstract) class 14 // - handles all block io, from both the cache/marshaller and the disk 15 16 class bstm_cache; 17 typedef vbl_smart_ptr<bstm_cache> bstm_cache_sptr; 18 19 class bstm_cache_destroyer; 20 21 class bstm_cache: public vbl_ref_count 22 { 23 public: 24 25 //: Use this instead of constructor 26 static bstm_cache_sptr instance(); exists()27 static bool exists() { return bstm_cache::instance_!=nullptr; } 28 29 //: the destructor instance to make sure memory is deallocated when the program exits 30 static bstm_cache_destroyer destroyer_; // it's not a pointer so C++ will make sure that its destructor will be called 31 friend class bstm_cache_destroyer; 32 33 //: returns block pointer to block specified by ID 34 virtual bstm_block* get_block(bstm_block_id id) = 0; 35 36 //: returns pointer to time_block specified by ID 37 virtual bstm_time_block* get_time_block(bstm_block_id id) = 0; 38 39 virtual void replace_time_block(bstm_block_id id, bstm_time_block* replacement)=0; 40 41 //: returns data_base pointer (THIS IS NECESSARY BECAUSE TEMPLATED FUNCTIONS CANNOT BE VIRTUAL) 42 virtual bstm_data_base* get_data_base(bstm_block_id id, std::string type, std::size_t num_bytes=0, bool read_only = true) = 0; 43 44 //: returns a data_base pointer which is initialized to the default value of the type. 45 // If a block for this type exists on the cache, it is removed and replaced with the new one. 46 // This method does not check whether a block of this type already exists on the disk nor writes it to the disk 47 virtual bstm_data_base* get_data_base_new(bstm_block_id id, std::string type=nullptr, std::size_t num_bytes=0, bool read_only = true) = 0; 48 49 //: removes data from this cache (may or may not write to disk first) 50 // Note that this function does not delete the memory, just removes it from the cache 51 // and puts it in the garbage vector 52 virtual void remove_data_base(bstm_block_id id, std::string type)=0; 53 54 virtual void replace_data_base(bstm_block_id id, std::string type, bstm_data_base* replacement)=0; 55 56 //: dumps writeable data onto disk 57 // -- pure virtual method; see specialisations 58 virtual void write_to_disk() = 0; 59 60 //: delete all the memory 61 virtual void clear_cache() = 0; 62 63 //: return scene sptr get_scene()64 virtual bstm_scene_sptr get_scene() { return scene_; } 65 66 protected: 67 68 //: hidden constructor bstm_cache(bstm_scene_sptr scene)69 bstm_cache(bstm_scene_sptr scene) : scene_(scene) {} 70 71 //: hidden destructor (protected so it cannot be called -- forces the class to be singleton) 72 ~bstm_cache() override = default; 73 74 //: singleton instance of bstm_cache 75 static bstm_cache_sptr instance_; 76 77 //: bstm_scene needs to be around to initialized uninitialized blocks 78 bstm_scene_sptr scene_; 79 80 }; 81 82 //: Binary write bstm_cache to stream 83 void vsl_b_write(vsl_b_ostream& os, bstm_cache const& scene); 84 //: Binary write bstm_cache to stream 85 void vsl_b_write(vsl_b_ostream& os, const bstm_cache* &p); 86 //: Binary write bstm_cache smart pointer to stream 87 void vsl_b_write(vsl_b_ostream& os, bstm_cache_sptr& sptr); 88 //: Binary write bstm_cache smart pointer to stream 89 void vsl_b_write(vsl_b_ostream& os, bstm_cache_sptr const& sptr); 90 91 //: Binary load bstm_cache from stream. 92 void vsl_b_read(vsl_b_istream& is, bstm_cache &scene); 93 //: Binary load bstm_cache from stream. 94 void vsl_b_read(vsl_b_istream& is, bstm_cache* p); 95 //: Binary load bstm_cache smart pointer from stream. 96 void vsl_b_read(vsl_b_istream& is, bstm_cache_sptr& sptr); 97 //: Binary load bstm_cache smart pointer from stream. 98 void vsl_b_read(vsl_b_istream& is, bstm_cache_sptr const& sptr); 99 100 101 //: create another class whose sole purpose is to destroy the singleton instance 102 class bstm_cache_destroyer 103 { 104 public: 105 bstm_cache_destroyer(const bstm_cache_sptr& s = nullptr); 106 ~bstm_cache_destroyer(); 107 108 void set_singleton(const bstm_cache_sptr& s); 109 private: 110 bstm_cache_sptr s_; 111 }; 112 113 #endif // bstm_cache_h_ 114