Woops, forgot to bump versions for the dependencies on ETL and synfig.
[synfig.git] / ETL / tags / stable / ETL / _trivial.h
1 /*! ========================================================================
2 ** Extended Template Library
3 ** Trivializing Template Class Implementation
4 ** $Id$
5 **
6 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
7 **
8 ** This package is free software; you can redistribute it and/or
9 ** modify it under the terms of the GNU General Public License as
10 ** published by the Free Software Foundation; either version 2 of
11 ** the License, or (at your option) any later version.
12 **
13 ** This package is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 ** General Public License for more details.
17 **
18 ** === N O T E S ===========================================================
19 **
20 ** This is an internal header file, included by other ETL headers.
21 ** You should not attempt to use it directly.
22 **
23 ** ========================================================================= */
24
25 /* === S T A R T =========================================================== */
26
27 #ifndef __ETL_TRIVIAL_H
28 #define __ETL_TRIVIAL_H
29
30 /* === H E A D E R S ======================================================= */
31
32 /* === M A C R O S ========================================================= */
33
34 /* === T Y P E D E F S ===================================================== */
35
36 /* === C L A S S E S & S T R U C T S ======================================= */
37
38 _ETL_BEGIN_NAMESPACE
39
40 /*! ========================================================================
41 ** \class       Trivial
42 ** \brief       Trivializes the constructor of a given class
43 **
44 ** This class makes the given type 'trivial',
45 ** effectively disabling the constructor and
46 ** destructor. (This is useful for unions)
47 ** Some extra casting may be necessary to get
48 ** it to work properly.
49 */
50 template <class T>
51 class trivial
52 {
53         typedef T value_type;
54         typedef T& reference;
55         typedef const T& const_reference;
56         typedef T* pointer;
57         typedef const T* const_pointer;
58
59         char data[sizeof(T)];
60 public:
61         operator reference()
62         { return *reinterpret_cast<pointer>(data); }
63
64         // HACK - Rather dangerous
65         //operator reference()const
66         //{ return *reinterpret_cast<pointer>(const_cast<char *>(data)); }
67
68         operator const_reference()const
69         { return *reinterpret_cast<const_pointer>(data); }
70
71         reference get()
72         { return *reinterpret_cast<pointer>(data); }
73
74         const_reference get()const
75         { return *reinterpret_cast<const_pointer>(data); }
76
77         void construct()
78         { new(&get()) value_type(); }
79
80         void destruct()
81         { get().~value_type(); }
82
83         void destroy() { destruct(); }
84
85         template<class U> reference
86         operator=(const U &rhs)
87         { return get()=rhs; }
88
89         template<class U>reference
90         operator=(const trivial<U> &rhs)
91         { return get()=rhs.get(); }
92
93         template<class U> reference
94         operator+=(const U &rhs)
95         { return get()+=rhs; }
96
97         template<class U> reference
98         operator-=(const U &rhs)
99         { return get()-=rhs; }
100
101         template<class U> reference
102         operator*=(const U &rhs)
103         { return get()*=rhs; }
104
105         template<class U> reference
106         operator/=(const U &rhs)
107         { return get()/=rhs; }
108
109         template<class U> reference
110         operator%=(const U &rhs)
111         { return get()%=rhs; }
112
113         template<class U> reference
114         operator^=(const U &rhs)
115         { return get()^=rhs; }
116
117         template<class U> reference
118         operator&=(const U &rhs)
119         { return get()&=rhs; }
120
121         template<class U> reference
122         operator>>=(const U &rhs)
123         { return get()>>=rhs; }
124
125         template<class U> reference
126         operator<<=(const U &rhs)
127         { return get()<<=rhs; }
128
129         operator bool()const
130         { return get(); }
131
132         bool operator!()const
133         { return !get(); }
134 }; // END of template class trivial
135
136 _ETL_END_NAMESPACE
137
138 //#include <iostream>
139
140 /*
141 template<typename T, typename _CharT, class _Traits> std::basic_istream<_CharT, _Traits>&
142 operator>>(std::basic_istream<_CharT, _Traits>& s, etl::trivial<T>& rhs)
143 { return s>>(T)(rhs); }
144
145 template<typename T,typename _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>&
146 operator<<(std::basic_ostream<_CharT, _Traits>& s, const etl::trivial<T>& rhs)
147 { return s<<(T)(rhs); }
148 */
149
150 /*
151 template<typename T> std::istream&
152 operator>>(std::istream& s, etl::trivial<T>& rhs)
153 { return s>>(T)(rhs); }
154
155 template<typename T> std::ostream&
156 operator<<(std::ostream& s, const etl::trivial<T>& rhs)
157 { return s<<(T)(rhs); }
158 */
159
160 /* === E X T E R N S ======================================================= */
161
162 /* === E N D =============================================================== */
163
164 #endif