Revision 111, 2.9 kB
(checked in by mido, 17 years ago)
|
pridana knihovna XSD (a jeji chlebodarkyne XERCES), v ramci Win32 zprovoznen priklad tests/test_xsd_hello.cxx
|
Line | |
---|
1 | // file : xsd/cxx/xml/dom/auto-ptr.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_XML_DOM_AUTO_PTR_HXX |
---|
7 | #define XSD_CXX_XML_DOM_AUTO_PTR_HXX |
---|
8 | |
---|
9 | namespace xsd |
---|
10 | { |
---|
11 | namespace cxx |
---|
12 | { |
---|
13 | namespace xml |
---|
14 | { |
---|
15 | namespace dom |
---|
16 | { |
---|
17 | // Simple auto_ptr version that calls release() instead of delete. |
---|
18 | // |
---|
19 | |
---|
20 | template <typename X> |
---|
21 | struct remove_c |
---|
22 | { |
---|
23 | typedef X r; |
---|
24 | }; |
---|
25 | |
---|
26 | template <typename X> |
---|
27 | struct remove_c<const X> |
---|
28 | { |
---|
29 | typedef X r; |
---|
30 | }; |
---|
31 | |
---|
32 | template <typename X> |
---|
33 | struct auto_ptr_ref |
---|
34 | { |
---|
35 | X* x_; |
---|
36 | |
---|
37 | explicit |
---|
38 | auto_ptr_ref (X* x) |
---|
39 | : x_ (x) |
---|
40 | { |
---|
41 | } |
---|
42 | }; |
---|
43 | |
---|
44 | template <typename X> |
---|
45 | struct auto_ptr |
---|
46 | { |
---|
47 | ~auto_ptr () |
---|
48 | { |
---|
49 | reset (); |
---|
50 | } |
---|
51 | |
---|
52 | explicit |
---|
53 | auto_ptr (X* x = 0) |
---|
54 | : x_ (x) |
---|
55 | { |
---|
56 | } |
---|
57 | |
---|
58 | auto_ptr (auto_ptr& y) |
---|
59 | : x_ (y.release ()) |
---|
60 | { |
---|
61 | } |
---|
62 | |
---|
63 | template <typename Y> |
---|
64 | auto_ptr (auto_ptr<Y>& y) |
---|
65 | : x_ (y.release ()) |
---|
66 | { |
---|
67 | } |
---|
68 | |
---|
69 | auto_ptr (auto_ptr_ref<X> r) |
---|
70 | : x_ (r.x_) |
---|
71 | { |
---|
72 | } |
---|
73 | |
---|
74 | auto_ptr& |
---|
75 | operator= (auto_ptr& y) |
---|
76 | { |
---|
77 | if (x_ != y.x_) |
---|
78 | reset (y.release ()); |
---|
79 | |
---|
80 | return *this; |
---|
81 | } |
---|
82 | |
---|
83 | template <typename Y> |
---|
84 | auto_ptr& |
---|
85 | operator= (auto_ptr<Y>& y) |
---|
86 | { |
---|
87 | if (x_ != y.x_) |
---|
88 | reset (y.release ()); |
---|
89 | |
---|
90 | return *this; |
---|
91 | } |
---|
92 | |
---|
93 | auto_ptr& |
---|
94 | operator= (auto_ptr_ref<X> r) |
---|
95 | { |
---|
96 | if (r.x_ != x_) |
---|
97 | reset (r.x_); |
---|
98 | |
---|
99 | return *this; |
---|
100 | } |
---|
101 | |
---|
102 | template <typename Y> |
---|
103 | operator auto_ptr_ref<Y> () |
---|
104 | { |
---|
105 | return auto_ptr_ref<Y> (release ()); |
---|
106 | } |
---|
107 | |
---|
108 | template <typename Y> |
---|
109 | operator auto_ptr<Y> () |
---|
110 | { |
---|
111 | return auto_ptr<Y> (release ()); |
---|
112 | } |
---|
113 | |
---|
114 | public: |
---|
115 | X& |
---|
116 | operator* () const |
---|
117 | { |
---|
118 | return *x_; |
---|
119 | } |
---|
120 | |
---|
121 | X* |
---|
122 | operator-> () const |
---|
123 | { |
---|
124 | return x_; |
---|
125 | } |
---|
126 | |
---|
127 | X* |
---|
128 | get () const |
---|
129 | { |
---|
130 | return x_; |
---|
131 | } |
---|
132 | |
---|
133 | X* |
---|
134 | release () |
---|
135 | { |
---|
136 | X* x (x_); |
---|
137 | x_ = 0; |
---|
138 | return x; |
---|
139 | } |
---|
140 | |
---|
141 | void |
---|
142 | reset (X* x = 0) |
---|
143 | { |
---|
144 | if (x_) |
---|
145 | const_cast<typename remove_c<X>::r*> (x_)->release (); |
---|
146 | |
---|
147 | x_ = x; |
---|
148 | } |
---|
149 | |
---|
150 | private: |
---|
151 | X* x_; |
---|
152 | }; |
---|
153 | } |
---|
154 | } |
---|
155 | } |
---|
156 | } |
---|
157 | |
---|
158 | #endif // XSD_CXX_XML_DOM_AUTO_PTR_HXX |
---|