diff options
author | Jörg Frings-Fürst <jff@merkur> | 2014-05-18 16:08:14 +0200 |
---|---|---|
committer | Jörg Frings-Fürst <jff@merkur> | 2014-05-18 16:08:14 +0200 |
commit | a15cf65c44d5c224169c32ef5495b68c758134b7 (patch) | |
tree | 3419f58fc8e1b315ba8171910ee044c5d467c162 /xsd/libxsd/xsd/cxx/auto-array.hxx |
Imported Upstream version 3.3.0.2upstream/3.3.0.2
Diffstat (limited to 'xsd/libxsd/xsd/cxx/auto-array.hxx')
-rw-r--r-- | xsd/libxsd/xsd/cxx/auto-array.hxx | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/xsd/libxsd/xsd/cxx/auto-array.hxx b/xsd/libxsd/xsd/cxx/auto-array.hxx new file mode 100644 index 0000000..2b4e100 --- /dev/null +++ b/xsd/libxsd/xsd/cxx/auto-array.hxx @@ -0,0 +1,114 @@ +// file : xsd/cxx/auto-array.hxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2005-2010 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#ifndef XSD_CXX_AUTO_ARRAY_HXX +#define XSD_CXX_AUTO_ARRAY_HXX + +#include <cstddef> // std::size_t + +namespace xsd +{ + namespace cxx + { + template <typename T> + struct std_deallocator + { + void + deallocate (T* p) + { + delete[] p; + } + }; + + // Simple automatic array. The second template parameter is + // an optional deallocator type. If not specified, delete[] + // is used. + // + template <typename T, typename D = std_deallocator<T> > + struct auto_array + { + auto_array (T a[]) + : a_ (a), d_ (0) + { + } + + auto_array (T a[], D& d) + : a_ (a), d_ (&d) + { + } + + ~auto_array () + { + if (d_ != 0) + d_->deallocate (a_); + else + delete[] a_; + } + + T& + operator[] (std::size_t index) const + { + return a_[index]; + } + + T* + get () const + { + return a_; + } + + T* + release () + { + T* tmp (a_); + a_ = 0; + return tmp; + } + + void + reset (T a[] = 0) + { + if (a_ != a) + { + if (d_ != 0) + d_->deallocate (a_); + else + delete[] a_; + + a_ = a; + } + } + + typedef void (auto_array::*bool_convertible)(); + + operator bool_convertible () const + { + return a_ ? &auto_array<T, D>::true_ : 0; + } + + private: + auto_array (const auto_array&); + + auto_array& + operator= (const auto_array&); + + private: + void + true_ (); + + private: + T* a_; + D* d_; + }; + + template <typename T, typename D> + void auto_array<T, D>:: + true_ () + { + } + } +} + +#endif // XSD_CXX_AUTO_ARRAY_HXX |