1 // Copyright 2016 The Draco Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 #ifndef DRACO_CORE_DATA_BUFFER_H_
16 #define DRACO_CORE_DATA_BUFFER_H_
17 
18 #include <cstring>
19 #include <ostream>
20 #include <vector>
21 
22 #include "draco/core/draco_types.h"
23 
24 namespace draco {
25 
26 // Buffer descriptor servers as a unique identifier of a buffer.
27 struct DataBufferDescriptor {
DataBufferDescriptorDataBufferDescriptor28   DataBufferDescriptor() : buffer_id(0), buffer_update_count(0) {}
29   // Id of the data buffer.
30   int64_t buffer_id;
31   // The number of times the buffer content was updated.
32   int64_t buffer_update_count;
33 };
34 
35 // Class used for storing raw buffer data.
36 class DataBuffer {
37  public:
38   DataBuffer();
39   bool Update(const void *data, int64_t size);
40   bool Update(const void *data, int64_t size, int64_t offset);
41 
42   // Reallocate the buffer storage to a new size keeping the data unchanged.
43   void Resize(int64_t new_size);
44   void WriteDataToStream(std::ostream &stream);
45   // Reads data from the buffer. Potentially unsafe, called needs to ensure
46   // the accessed memory is valid.
Read(int64_t byte_pos,void * out_data,size_t data_size)47   void Read(int64_t byte_pos, void *out_data, size_t data_size) const {
48     memcpy(out_data, data() + byte_pos, data_size);
49   }
50 
51   // Writes data to the buffer. Unsafe, caller must ensure the accessed memory
52   // is valid.
Write(int64_t byte_pos,const void * in_data,size_t data_size)53   void Write(int64_t byte_pos, const void *in_data, size_t data_size) {
54     memcpy(const_cast<uint8_t *>(data()) + byte_pos, in_data, data_size);
55   }
56 
57   // Copies data from another buffer to this buffer.
Copy(int64_t dst_offset,const DataBuffer * src_buf,int64_t src_offset,int64_t size)58   void Copy(int64_t dst_offset, const DataBuffer *src_buf, int64_t src_offset,
59             int64_t size) {
60     memcpy(const_cast<uint8_t *>(data()) + dst_offset,
61            src_buf->data() + src_offset, size);
62   }
63 
set_update_count(int64_t buffer_update_count)64   void set_update_count(int64_t buffer_update_count) {
65     descriptor_.buffer_update_count = buffer_update_count;
66   }
update_count()67   int64_t update_count() const { return descriptor_.buffer_update_count; }
data_size()68   size_t data_size() const { return data_.size(); }
data()69   const uint8_t *data() const { return data_.data(); }
data()70   uint8_t *data() { return &data_[0]; }
buffer_id()71   int64_t buffer_id() const { return descriptor_.buffer_id; }
set_buffer_id(int64_t buffer_id)72   void set_buffer_id(int64_t buffer_id) { descriptor_.buffer_id = buffer_id; }
73 
74  private:
75   std::vector<uint8_t> data_;
76   // Counter incremented by Update() calls.
77   DataBufferDescriptor descriptor_;
78 };
79 
80 }  // namespace draco
81 
82 #endif  // DRACO_CORE_DATA_BUFFER_H_
83