From f0510d2f90467de8e8f260b47d79a9baaf9bef17 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 17 Sep 2009 07:15:29 +0200 Subject: Start tracking XSD with git --- libxsd/xsd/cxx/tree/buffer.txx | 153 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 153 insertions(+) create mode 100644 libxsd/xsd/cxx/tree/buffer.txx (limited to 'libxsd/xsd/cxx/tree/buffer.txx') diff --git a/libxsd/xsd/cxx/tree/buffer.txx b/libxsd/xsd/cxx/tree/buffer.txx new file mode 100644 index 0000000..6212e20 --- /dev/null +++ b/libxsd/xsd/cxx/tree/buffer.txx @@ -0,0 +1,153 @@ +// file : xsd/cxx/tree/buffer.txx +// author : Boris Kolpackov +// copyright : Copyright (c) 2005-2009 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +namespace xsd +{ + namespace cxx + { + namespace tree + { + template + buffer:: + buffer (size_t size) + { + capacity (size); + size_ = size; + } + + template + buffer:: + buffer (size_t size, size_t capacity) + { + if (size > capacity) + throw bounds (); + + this->capacity (capacity); + size_ = size; + } + + template + buffer:: + buffer (const void* data, size_t size) + { + capacity (size); + size_ = size; + + if (size_) + std::memcpy (data_, data, size_); + } + + template + buffer:: + buffer (const void* data, size_t size, size_t capacity) + { + if (size > capacity) + throw bounds (); + + this->capacity (capacity); + size_ = size; + + if (size_) + std::memcpy (data_, data, size_); + } + + template + buffer:: + buffer (void* data, size_t size, size_t capacity, bool own) + { + if (size > capacity) + throw bounds (); + + if (own) + { + data_ = reinterpret_cast (data); + size_ = size; + capacity_ = capacity; + } + else + { + this->capacity (capacity); + size_ = size; + + if (size_) + std::memcpy (data_, data, size_); + } + } + + template + buffer:: + buffer (const buffer& other) + : buffer_base () + { + capacity (other.capacity_); + size_ = other.size_; + + if (size_) + std::memcpy (data_, other.data_, size_); + } + + template + buffer& buffer:: + operator= (const buffer& other) + { + if (this != &other) + { + capacity (other.capacity_, false); + size_ = other.size_; + + if (size_) + std::memcpy (data_, other.data_, size_); + } + + return *this; + } + + template + void buffer:: + swap (buffer& other) + { + char* tmp_data (data_); + size_t tmp_size (size_); + size_t tmp_capacity (capacity_); + + data_ = other.data_; + size_ = other.size_; + capacity_ = other.capacity_; + + other.data_ = tmp_data; + other.size_ = tmp_size; + other.capacity_ = tmp_capacity; + } + + template + bool buffer:: + capacity (size_t capacity, bool copy) + { + if (size_ > capacity) + throw bounds (); + + if (capacity <= capacity_) + { + return false; // Do nothing if shrinking is requested. + } + else + { + char* data (reinterpret_cast (operator new (capacity))); + + if (copy && size_ > 0) + std::memcpy (data, data_, size_); + + if (data_) + operator delete (data_); + + data_ = data; + capacity_ = capacity; + + return true; + } + } + } + } +} -- cgit v1.1