/src/MapServer/src/flatgeobuf/include/flatbuffers/detached_buffer.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2021 Google Inc. All rights reserved. |
3 | | * |
4 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | * you may not use this file except in compliance with the License. |
6 | | * You may obtain a copy of the License at |
7 | | * |
8 | | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | * |
10 | | * Unless required by applicable law or agreed to in writing, software |
11 | | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | * See the License for the specific language governing permissions and |
14 | | * limitations under the License. |
15 | | */ |
16 | | |
17 | | #ifndef FLATBUFFERS_DETACHED_BUFFER_H_ |
18 | | #define FLATBUFFERS_DETACHED_BUFFER_H_ |
19 | | |
20 | | #include "flatbuffers/allocator.h" |
21 | | #include "flatbuffers/base.h" |
22 | | #include "flatbuffers/default_allocator.h" |
23 | | |
24 | | namespace mapserver { |
25 | | namespace flatbuffers { |
26 | | |
27 | | // DetachedBuffer is a finished flatbuffer memory region, detached from its |
28 | | // builder. The original memory region and allocator are also stored so that |
29 | | // the DetachedBuffer can manage the memory lifetime. |
30 | | class DetachedBuffer { |
31 | | public: |
32 | | DetachedBuffer() |
33 | | : allocator_(nullptr), |
34 | | own_allocator_(false), |
35 | | buf_(nullptr), |
36 | | reserved_(0), |
37 | | cur_(nullptr), |
38 | 0 | size_(0) {} |
39 | | |
40 | | DetachedBuffer(Allocator *allocator, bool own_allocator, uint8_t *buf, |
41 | | size_t reserved, uint8_t *cur, size_t sz) |
42 | | : allocator_(allocator), |
43 | | own_allocator_(own_allocator), |
44 | | buf_(buf), |
45 | | reserved_(reserved), |
46 | | cur_(cur), |
47 | 0 | size_(sz) {} |
48 | | |
49 | | DetachedBuffer(DetachedBuffer &&other) |
50 | | : allocator_(other.allocator_), |
51 | | own_allocator_(other.own_allocator_), |
52 | | buf_(other.buf_), |
53 | | reserved_(other.reserved_), |
54 | | cur_(other.cur_), |
55 | 0 | size_(other.size_) { |
56 | 0 | other.reset(); |
57 | 0 | } |
58 | | |
59 | 0 | DetachedBuffer &operator=(DetachedBuffer &&other) { |
60 | 0 | if (this == &other) return *this; |
61 | 0 |
|
62 | 0 | destroy(); |
63 | 0 |
|
64 | 0 | allocator_ = other.allocator_; |
65 | 0 | own_allocator_ = other.own_allocator_; |
66 | 0 | buf_ = other.buf_; |
67 | 0 | reserved_ = other.reserved_; |
68 | 0 | cur_ = other.cur_; |
69 | 0 | size_ = other.size_; |
70 | 0 |
|
71 | 0 | other.reset(); |
72 | 0 |
|
73 | 0 | return *this; |
74 | 0 | } |
75 | | |
76 | 0 | ~DetachedBuffer() { destroy(); } |
77 | | |
78 | 0 | const uint8_t *data() const { return cur_; } |
79 | | |
80 | 0 | uint8_t *data() { return cur_; } |
81 | | |
82 | 0 | size_t size() const { return size_; } |
83 | | |
84 | | // These may change access mode, leave these at end of public section |
85 | | FLATBUFFERS_DELETE_FUNC(DetachedBuffer(const DetachedBuffer &other)); |
86 | | FLATBUFFERS_DELETE_FUNC( |
87 | | DetachedBuffer &operator=(const DetachedBuffer &other)); |
88 | | |
89 | | protected: |
90 | | Allocator *allocator_; |
91 | | bool own_allocator_; |
92 | | uint8_t *buf_; |
93 | | size_t reserved_; |
94 | | uint8_t *cur_; |
95 | | size_t size_; |
96 | | |
97 | 0 | inline void destroy() { |
98 | 0 | if (buf_) Deallocate(allocator_, buf_, reserved_); |
99 | 0 | if (own_allocator_ && allocator_) { delete allocator_; } |
100 | 0 | reset(); |
101 | 0 | } |
102 | | |
103 | 0 | inline void reset() { |
104 | 0 | allocator_ = nullptr; |
105 | 0 | own_allocator_ = false; |
106 | 0 | buf_ = nullptr; |
107 | 0 | reserved_ = 0; |
108 | 0 | cur_ = nullptr; |
109 | 0 | size_ = 0; |
110 | 0 | } |
111 | | }; |
112 | | |
113 | | } // namespace flatbuffers |
114 | | } // namespace mapserver |
115 | | |
116 | | #endif // FLATBUFFERS_DETACHED_BUFFER_H_ |