[111] | 1 | // file : xsd/cxx/tree/buffer.txx |
---|
| 2 | // author : Boris Kolpackov <boris@codesynthesis.com> |
---|
| 3 | // copyright : Copyright (c) 2005-2008 Code Synthesis Tools CC |
---|
| 4 | // license : GNU GPL v2 + exceptions; see accompanying LICENSE file |
---|
| 5 | |
---|
| 6 | namespace xsd |
---|
| 7 | { |
---|
| 8 | namespace cxx |
---|
| 9 | { |
---|
| 10 | namespace tree |
---|
| 11 | { |
---|
| 12 | template <typename C> |
---|
| 13 | buffer<C>:: |
---|
| 14 | buffer (size_t size) |
---|
| 15 | { |
---|
| 16 | capacity (size); |
---|
| 17 | size_ = size; |
---|
| 18 | } |
---|
| 19 | |
---|
| 20 | template <typename C> |
---|
| 21 | buffer<C>:: |
---|
| 22 | buffer (size_t size, size_t capacity) |
---|
| 23 | { |
---|
| 24 | if (size > capacity) |
---|
| 25 | throw bounds<C> (); |
---|
| 26 | |
---|
| 27 | this->capacity (capacity); |
---|
| 28 | size_ = size; |
---|
| 29 | } |
---|
| 30 | |
---|
| 31 | template <typename C> |
---|
| 32 | buffer<C>:: |
---|
| 33 | buffer (const void* data, size_t size) |
---|
| 34 | { |
---|
| 35 | capacity (size); |
---|
| 36 | size_ = size; |
---|
| 37 | |
---|
| 38 | if (size_) |
---|
| 39 | std::memcpy (data_, data, size_); |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | template <typename C> |
---|
| 43 | buffer<C>:: |
---|
| 44 | buffer (const void* data, size_t size, size_t capacity) |
---|
| 45 | { |
---|
| 46 | if (size > capacity) |
---|
| 47 | throw bounds<C> (); |
---|
| 48 | |
---|
| 49 | this->capacity (capacity); |
---|
| 50 | size_ = size; |
---|
| 51 | |
---|
| 52 | if (size_) |
---|
| 53 | std::memcpy (data_, data, size_); |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | template <typename C> |
---|
| 57 | buffer<C>:: |
---|
| 58 | buffer (void* data, size_t size, size_t capacity, bool own) |
---|
| 59 | { |
---|
| 60 | if (size > capacity) |
---|
| 61 | throw bounds<C> (); |
---|
| 62 | |
---|
| 63 | if (own) |
---|
| 64 | { |
---|
| 65 | data_ = reinterpret_cast<char*> (data); |
---|
| 66 | size_ = size; |
---|
| 67 | capacity_ = capacity; |
---|
| 68 | } |
---|
| 69 | else |
---|
| 70 | { |
---|
| 71 | this->capacity (capacity); |
---|
| 72 | size_ = size; |
---|
| 73 | |
---|
| 74 | if (size_) |
---|
| 75 | std::memcpy (data_, data, size_); |
---|
| 76 | } |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | template <typename C> |
---|
| 80 | buffer<C>:: |
---|
| 81 | buffer (const buffer& other) |
---|
| 82 | : buffer_base () |
---|
| 83 | { |
---|
| 84 | capacity (other.capacity_); |
---|
| 85 | size_ = other.size_; |
---|
| 86 | |
---|
| 87 | if (size_) |
---|
| 88 | std::memcpy (data_, other.data_, size_); |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | template <typename C> |
---|
| 92 | buffer<C>& buffer<C>:: |
---|
| 93 | operator= (const buffer& other) |
---|
| 94 | { |
---|
| 95 | if (this != &other) |
---|
| 96 | { |
---|
| 97 | capacity (other.capacity_, false); |
---|
| 98 | size_ = other.size_; |
---|
| 99 | |
---|
| 100 | if (size_) |
---|
| 101 | std::memcpy (data_, other.data_, size_); |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | return *this; |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | template <typename C> |
---|
| 108 | void buffer<C>:: |
---|
| 109 | swap (buffer& other) |
---|
| 110 | { |
---|
| 111 | char* tmp_data (data_); |
---|
| 112 | size_t tmp_size (size_); |
---|
| 113 | size_t tmp_capacity (capacity_); |
---|
| 114 | |
---|
| 115 | data_ = other.data_; |
---|
| 116 | size_ = other.size_; |
---|
| 117 | capacity_ = other.capacity_; |
---|
| 118 | |
---|
| 119 | other.data_ = tmp_data; |
---|
| 120 | other.size_ = tmp_size; |
---|
| 121 | other.capacity_ = tmp_capacity; |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | template <typename C> |
---|
| 125 | bool buffer<C>:: |
---|
| 126 | capacity (size_t capacity, bool copy) |
---|
| 127 | { |
---|
| 128 | if (size_ > capacity) |
---|
| 129 | throw bounds<C> (); |
---|
| 130 | |
---|
| 131 | if (capacity <= capacity_) |
---|
| 132 | { |
---|
| 133 | return false; // Do nothing if shrinking is requested. |
---|
| 134 | } |
---|
| 135 | else |
---|
| 136 | { |
---|
| 137 | char* data (reinterpret_cast<char*> (operator new (capacity))); |
---|
| 138 | |
---|
| 139 | if (copy && size_ > 0) |
---|
| 140 | std::memcpy (data, data_, size_); |
---|
| 141 | |
---|
| 142 | char* tmp (data_); |
---|
| 143 | data_ = data; |
---|
| 144 | capacity_ = capacity; |
---|
| 145 | |
---|
| 146 | operator delete (tmp); |
---|
| 147 | |
---|
| 148 | return true; |
---|
| 149 | } |
---|
| 150 | } |
---|
| 151 | } |
---|
| 152 | } |
---|
| 153 | } |
---|