1 #ifndef bstm_data_base_h 2 #define bstm_data_base_h 3 //: 4 // \file 5 // 6 #include <cstring> 7 #include <iostream> 8 #include <boxm2/basic/boxm2_array_1d.h> 9 #include <bstm/basic/bstm_block_id.h> 10 #include <bstm/bstm_block_metadata.h> 11 #include <bstm/bstm_data_traits.h> 12 #ifdef _MSC_VER 13 # include <vcl_msvc_warnings.h> 14 #endif 15 16 // smart ptr includes 17 #include <vbl/vbl_ref_count.h> 18 #include <vbl/vbl_smart_ptr.h> 19 20 // 21 #include <vsl/vsl_binary_io.h> 22 23 //: Generic, untemplated base class for data blocks 24 class bstm_data_base : public vbl_ref_count { 25 public: 26 //: Constructor - beware that the data_buffer becomes OWNED (and will be 27 // deleted) by this class! 28 bstm_data_base(char *data_buffer, 29 std::size_t length, 30 bstm_block_id id, 31 bool read_only = true) read_only_(read_only)32 : read_only_(read_only) 33 , id_(id) 34 , buffer_length_(length) 35 , data_buffer_(data_buffer) {} 36 37 //: Constructs a buffer of the appropriate size to hold the given 38 // number of elements of the given type. 39 bstm_data_base(std::size_t num_elements, 40 const std::string &data_type, 41 bstm_block_id id, 42 bool read_only = true) read_only_(read_only)43 : read_only_(read_only) 44 , id_(id) 45 , buffer_length_(bstm_data_info::datasize(data_type) * num_elements) 46 , data_buffer_(new char[buffer_length_]()) {} 47 48 //: initializes empty data buffer 49 bstm_data_base(bstm_block_metadata data, 50 const std::string& type, 51 bool read_only = true); 52 53 void set_default_value(const std::string& data_type, bstm_block_metadata data); 54 55 //: This destructor is correct - by our design the original data_buffer 56 // becomes OWNED by the data_base class ~bstm_data_base()57 ~bstm_data_base() override { 58 if (data_buffer_) 59 delete[] data_buffer_; 60 } 61 62 //: accessor for low level byte buffer kept by the data_base data_buffer()63 char *data_buffer() { return data_buffer_; } data_buffer()64 const char *data_buffer() const { return data_buffer_; } buffer_length()65 std::size_t buffer_length() const { return buffer_length_; } block_id()66 bstm_block_id &block_id() { return id_; } 67 //: accessor to a portion of the byte buffer 68 char *cell_buffer(int i, std::size_t cell_size); 69 70 //: setter for swapping out data buffer 71 72 //: by default data is read-only, i.e. cache doesn't save it before destroying 73 // it 74 bool read_only_; enable_write()75 void enable_write() { read_only_ = false; } disable_write()76 void disable_write() { read_only_ = true; } 77 78 protected: 79 //: id for this particular block 80 bstm_block_id id_; 81 82 //: byte buffer and its size 83 std::size_t buffer_length_; 84 char *data_buffer_; 85 }; 86 87 //: Smart_Pointer typedef for bstm_data_base 88 typedef vbl_smart_ptr<bstm_data_base> bstm_data_base_sptr; 89 90 //: Binary write boxm_update_bit_scene_manager scene to stream 91 void vsl_b_write(vsl_b_ostream &os, bstm_data_base const &scene); 92 void vsl_b_write(vsl_b_ostream &os, const bstm_data_base *&p); 93 void vsl_b_write(vsl_b_ostream &os, bstm_data_base_sptr &sptr); 94 void vsl_b_write(vsl_b_ostream &os, bstm_data_base_sptr const &sptr); 95 96 //: Binary load boxm_update_bit_scene_manager scene from stream. 97 void vsl_b_read(vsl_b_istream &is, bstm_data_base &scene); 98 void vsl_b_read(vsl_b_istream &is, bstm_data_base *p); 99 void vsl_b_read(vsl_b_istream &is, bstm_data_base_sptr &sptr); 100 void vsl_b_read(vsl_b_istream &is, bstm_data_base_sptr const &sptr); 101 102 #endif // bstm_data_base_h 103