OSDN Git Service

DTXMania089リリースに際してのtag付け。
[dtxmania/dtxmania.git] / 110401(DTXMania089) / SlimDXc_Jun2010(VC++2008) / source / auto_array.h
1 /*\r
2 * Copyright (c) 2007-2010 SlimDX Group\r
3\r
4 * Permission is hereby granted, free of charge, to any person obtaining a copy\r
5 * of this software and associated documentation files (the "Software"), to deal\r
6 * in the Software without restriction, including without limitation the rights\r
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
8 * copies of the Software, and to permit persons to whom the Software is\r
9 * furnished to do so, subject to the following conditions:\r
10\r
11 * The above copyright notice and this permission notice shall be included in\r
12 * all copies or substantial portions of the Software.\r
13\r
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r
20 * THE SOFTWARE.\r
21 */\r
22 #pragma once\r
23 \r
24 template<class T>\r
25 struct auto_array_ref\r
26 {\r
27         explicit auto_array_ref(T *right)\r
28                 : ref(right)\r
29         {\r
30         }\r
31 \r
32         T *ref;\r
33 };\r
34 \r
35 template<class T>\r
36 class auto_array\r
37 {\r
38 private:\r
39         T *myptr;\r
40 \r
41 public:\r
42         explicit auto_array(T *ptr = 0) throw()\r
43                 : myptr(ptr)\r
44         {\r
45         }\r
46 \r
47         auto_array(auto_array<T>& right) throw()\r
48                 : myptr(right.release())\r
49         {\r
50         }\r
51 \r
52         auto_array(auto_array_ref<T> right) throw()\r
53         {\r
54                 T *ptr = right.ref;\r
55                 right.ref = NULL;\r
56                 myptr = ptr;\r
57         }\r
58 \r
59         template<class Other>\r
60         operator auto_array<Other>() throw()\r
61         {\r
62                 return auto_array<Other>(*this);\r
63         }\r
64 \r
65         template<class Other>\r
66         operator auto_array_ref<Other>() throw()\r
67         {\r
68                 Other *ptr = myptr;\r
69                 auto_array_ref<Other> ans(ptr);\r
70                 myptr = NULL;\r
71 \r
72                 return ans;\r
73         }\r
74 \r
75         template<class Other>\r
76         auto_array<T>& operator = (auto_array<Other>& right) throw()\r
77         {\r
78                 reset(right.release());\r
79                 return *this;\r
80         }\r
81 \r
82         template<class Other>\r
83         auto_array(auto_array<Other>& right) throw()\r
84                 : myptr(right.release())\r
85         {\r
86         }\r
87 \r
88         auto_array<T>& operator = (auto_array<T>& right) throw()\r
89         {\r
90                 reset(right.release());\r
91                 return *this;\r
92         }\r
93 \r
94         auto_array<T>& operator = (auto_array_ref<T> right) throw()\r
95         {\r
96                 T *ptr = right.ref;\r
97                 right.ref = NULL;\r
98                 reset(ptr);\r
99 \r
100                 return *this;\r
101         }\r
102 \r
103         ~auto_array()\r
104         {\r
105                 delete[] myptr;\r
106         }\r
107 \r
108         T& operator*() const throw()\r
109         {\r
110                 return *get();\r
111         }\r
112 \r
113         T *operator->() const throw()\r
114         {\r
115                 return get();\r
116         }\r
117 \r
118         T *get() const throw()\r
119         {\r
120                 return myptr;\r
121         }\r
122 \r
123         T *release() throw()\r
124         {\r
125                 T *tmp = myptr;\r
126                 myptr = NULL;\r
127 \r
128                 return tmp;\r
129         }\r
130 \r
131         void reset(T *ptr = 0)\r
132         {\r
133                 if (ptr != myptr)\r
134                         delete[] myptr;\r
135                 myptr = ptr;\r
136         }\r
137 };