1 #ifndef boxm2_dumb_cache_h_
2 #define boxm2_dumb_cache_h_
3 //:
4 // \file
5 #include <iostream>
6 #include <boxm2/io/boxm2_cache1.h>
7 #ifdef _MSC_VER
8 # include <vcl_msvc_warnings.h>
9 #endif
10
11 //: boxm2_dumb_cache - example realization of abstract cache class
12 class boxm2_dumb_cache : public boxm2_cache1
13 {
14 public:
15 boxm2_dumb_cache(boxm2_scene* scene);
16 ~boxm2_dumb_cache() override;
17
18 //: returns block pointer to block specified by ID
19 boxm2_block* get_block(boxm2_block_id id) override;
20
21 //: returns data_base pointer (THIS IS NECESSARY BECAUSE TEMPLATED FUNCTIONS CANNOT BE VIRTUAL)
22 boxm2_data_base* get_data_base(boxm2_block_id, std::string type, std::size_t num_bytes=0, bool read_only = true) override;
23
24 //: deletes data from dumb cache
25 void remove_data_base(boxm2_block_id, std::string type) override;
26 void replace_data_base(boxm2_block_id id, std::string type, boxm2_data_base* replacement) override;
27
28 //: returns data pointer to data block specified by ID
29 template <boxm2_data_type T>
30 boxm2_data<T>* get_data(boxm2_block_id id);
31
32 //: dumps writeable data onto disk
33 // \todo not yet implemented
write_to_disk()34 void write_to_disk() override { std::cerr << "write_to_disk() not yet implemented!!!\n"; }
35
36 //: disable the write process
37 // \todo not yet implemented
disable_write()38 void disable_write() override { std::cerr << "disable_write() not yet implemented!!!\n"; }
39
40 //: delete all the memory
clear_cache()41 void clear_cache() override { std::cerr << "clear_cache() not yet implemented!!!\n"; }
42 private:
43
44 //: private update cache method (very simple)
45 void update_block_cache(boxm2_block* blk);
46
47 //: private update method (very simple)
48 void update_data_base_cache(boxm2_data_base* dat, const std::string& type);
49
50 //: private update block cache method
51 template <boxm2_data_type T>
52 void update_data_cache(boxm2_data<T>* dat);
53
54 //: dumb cache keeps one cached block, the last one used.
55 boxm2_block* cached_block_;
56
57 //: keeps one copy of each type of cached data
58 std::map<std::string, boxm2_data_base* > cached_data_;
59
60 //: directory where blocks are found
61 std::string scene_dir_;
62 };
63
64
65 //: get data by type and id
66 template<boxm2_data_type T>
get_data(boxm2_block_id id)67 boxm2_data<T>* boxm2_dumb_cache::get_data(boxm2_block_id id)
68 {
69 if ( cached_data_.find(boxm2_data_traits<T>::prefix()) != cached_data_.end() )
70 {
71 if (cached_data_[boxm2_data_traits<T>::prefix()]->block_id() == id)
72 return (boxm2_data<T>*) cached_data_[boxm2_data_traits<T>::prefix()];
73 }
74
75 //otherwise load it from disk
76 boxm2_data<T>* loaded = boxm2_sio_mgr::load_block_data<T>(scene_dir_,id);
77 this->update_data_cache<T>(loaded);
78 return loaded;
79 }
80
81 //: update data cache by type
82 template<boxm2_data_type T>
update_data_cache(boxm2_data<T> * dat)83 void boxm2_dumb_cache::update_data_cache(boxm2_data<T>* dat)
84 {
85 std::map<std::string, boxm2_data_base* >::iterator iter;
86 iter = cached_data_.find(boxm2_data_traits<T>::prefix());
87 if ( iter != cached_data_.end() )
88 {
89 boxm2_data_base* old = (*iter).second;
90 if (old) delete old;
91 }
92 cached_data_[boxm2_data_traits<T>::prefix()] = dat;
93 }
94
95 #endif // boxm2_dumb_cache_h_
96