Revision 111, 1.9 kB
(checked in by mido, 16 years ago)
|
pridana knihovna XSD (a jeji chlebodarkyne XERCES), v ramci Win32 zprovoznen priklad tests/test_xsd_hello.cxx
|
Line | |
---|
1 | // file : xsd/cxx/auto-array.hxx |
---|
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 | #ifndef XSD_CXX_AUTO_ARRAY_HXX |
---|
7 | #define XSD_CXX_AUTO_ARRAY_HXX |
---|
8 | |
---|
9 | #include <cstddef> // std::size_t |
---|
10 | |
---|
11 | namespace xsd |
---|
12 | { |
---|
13 | namespace cxx |
---|
14 | { |
---|
15 | template <typename X> |
---|
16 | struct std_deallocator |
---|
17 | { |
---|
18 | void |
---|
19 | deallocate (X* p) |
---|
20 | { |
---|
21 | delete[] p; |
---|
22 | } |
---|
23 | }; |
---|
24 | |
---|
25 | // Simple automatic array. The second template parameter is |
---|
26 | // an optional deallocator type. If not specified, delete[] |
---|
27 | // is used. |
---|
28 | // |
---|
29 | template <typename X, typename D = std_deallocator<X> > |
---|
30 | struct auto_array |
---|
31 | { |
---|
32 | auto_array (X a[]) |
---|
33 | : a_ (a), d_ (0) |
---|
34 | { |
---|
35 | } |
---|
36 | |
---|
37 | auto_array (X a[], D& d) |
---|
38 | : a_ (a), d_ (&d) |
---|
39 | { |
---|
40 | } |
---|
41 | |
---|
42 | ~auto_array () |
---|
43 | { |
---|
44 | if (d_ != 0) |
---|
45 | d_->deallocate (a_); |
---|
46 | else |
---|
47 | delete[] a_; |
---|
48 | } |
---|
49 | |
---|
50 | X& |
---|
51 | operator[] (std::size_t index) const |
---|
52 | { |
---|
53 | return a_[index]; |
---|
54 | } |
---|
55 | |
---|
56 | X* |
---|
57 | get () const |
---|
58 | { |
---|
59 | return a_; |
---|
60 | } |
---|
61 | |
---|
62 | X* |
---|
63 | release () |
---|
64 | { |
---|
65 | X* tmp (a_); |
---|
66 | a_ = 0; |
---|
67 | return tmp; |
---|
68 | } |
---|
69 | |
---|
70 | void |
---|
71 | reset (X a[] = 0) |
---|
72 | { |
---|
73 | if (a_ != a) |
---|
74 | { |
---|
75 | if (d_ != 0) |
---|
76 | d_->deallocate (a_); |
---|
77 | else |
---|
78 | delete[] a_; |
---|
79 | |
---|
80 | a_ = a; |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | typedef void (auto_array::*bool_convertible)(); |
---|
85 | |
---|
86 | operator bool_convertible () const |
---|
87 | { |
---|
88 | return a_ ? &auto_array<X, D>::true_ : 0; |
---|
89 | } |
---|
90 | |
---|
91 | private: |
---|
92 | auto_array (const auto_array&); |
---|
93 | |
---|
94 | auto_array& |
---|
95 | operator= (const auto_array&); |
---|
96 | |
---|
97 | private: |
---|
98 | void |
---|
99 | true_ (); |
---|
100 | |
---|
101 | private: |
---|
102 | X* a_; |
---|
103 | D* d_; |
---|
104 | }; |
---|
105 | |
---|
106 | template <typename X, typename D> |
---|
107 | void auto_array<X, D>:: |
---|
108 | true_ () |
---|
109 | { |
---|
110 | } |
---|
111 | } |
---|
112 | } |
---|
113 | |
---|
114 | #endif // XSD_CXX_AUTO_ARRAY_HXX |
---|