1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2009 Oracle.  All rights reserved.
5 *
6 * $Id$
7 */
8
9#ifndef _DB_STL_PTYPE_H
10#define _DB_STL_PTYPE_H
11
12#include "dbstl_common.h"
13#include "dbstl_element_ref.h"
14#include <iostream>
15
16using std::istream;
17using std::ostream;
18using namespace dbstl;
19//////////////////////////////////////////////////////////////////////
20//////////////////////////////////////////////////////////////////////
21//
22// ptype class template definition
23//
24// ptype<> is a primitive types wrapper, must use this wrapper to
25// store/retrieve primitive types to/from db via db stl interface
26//
27template <Typename T>
28class ptype
29{
30public:
31	T v;
32	ptype(){v = 0;}
33	ptype(T vv){v = vv;}
34	typedef ptype<T> tptype;
35	typedef db_vector_iterator<tptype> dvit;
36
37	operator T()
38	{
39		return v;
40	}
41
42	ptype(const ElementRef<ptype<T> >&rval)
43	{
44		if ( rval._DB_STL_GetData(*this) != INVALID_KEY_DATA) {
45
46		} else {
47			throw new InvalidDbtException();
48		}
49	}
50
51	ptype(const ptype<T>&p2)
52	{
53		v = p2.v;
54	}
55
56	template <Typename T2>
57	ptype(const ptype<T2>&p2)
58	{
59		v = p2.v;
60	}
61	template <Typename T2>
62	const ptype<T>& operator=(const ptype<T2>&p2)
63	{
64		v = p2.v;
65		return *this;
66	}
67	template <Typename T2>
68	const ptype<T> operator +(const ptype<T2> &p2) const
69	{
70		return ptype<T>(v + p2.v );
71	}
72	template <Typename T2>
73	const ptype<T> operator -(const ptype<T2> &p2) const
74	{
75		return ptype<T>(v - p2.v );
76	}
77	template <Typename T2>
78	const ptype<T> operator *(const ptype<T2> &p2) const
79	{
80		return ptype<T>(v * p2.v );
81	}
82	template <Typename T2>
83	const ptype<T> operator /(const ptype<T2> &p2) const
84	{
85		return ptype<T>(v / p2.v );
86	}
87	template <Typename T2>
88	const ptype<T> operator %(const ptype<T2> &p2) const
89	{
90		return ptype<T>(v % p2.v );
91	}
92
93	template <Typename T2>
94	const ptype<T>& operator +=(const ptype<T2> &p2) const
95	{
96		v += p2.v;
97		return *this;
98	}
99
100	template <Typename T2>
101	const ptype<T>& operator -=(const ptype<T2> &p2) const
102	{
103		v -= p2.v;
104		return *this;
105	}
106	template <Typename T2>
107	const ptype<T>& operator *=(const ptype<T2> &p2) const
108	{
109		v *= p2.v;
110		return *this;
111	}
112	template <Typename T2>
113	const ptype<T>& operator /=(const ptype<T2> &p2) const
114	{
115		v /= p2.v;
116		return *this;
117	}
118	template <Typename T2>
119	const ptype<T>& operator %=(const ptype<T2> &p2) const
120	{
121		v %= p2.v;
122		return *this;
123	}
124
125	template <Typename T2>
126	bool operator==(const ptype<T2>&p2) const
127	{
128		return v == p2.v;
129	}
130	template <Typename T2>
131	bool operator!=(const ptype<T2>&p2)const
132	{
133		return v != p2.v;
134	}
135
136	template <Typename T2>
137	bool operator<(const ptype<T2>&p2) const
138	{
139		return v < p2.v;
140	}
141
142	template <Typename T2>
143	bool operator>(const ptype<T2>&p2) const
144	{
145		return v > p2.v;
146	}
147
148	template <Typename T2>
149	bool operator<=(const ptype<T2>&p2) const
150	{
151		return v <= p2.v;
152	}
153	template <Typename T2>
154	bool operator>=(const ptype<T2>&p2) const
155	{
156		return v >= p2.v;
157	}
158
159	const ptype<T>& operator=(const ptype<T>&p2)
160	{
161		v = p2.v;
162		return p2;
163	}
164
165	bool operator>(const ptype<T>&p2) const
166	{
167		return v > p2.v;
168	}
169	bool operator<(const ptype<T>&p2) const
170	{
171		return v < p2.v;
172	}
173
174	bool operator>=(const ptype<T>&p2) const
175	{
176		return v >= p2.v;
177	}
178	bool operator<=(const ptype<T>&p2) const
179	{
180		return v <= p2.v;
181	}
182	const ptype<T> operator-() const
183	{
184		return ptype<T>(-v);
185	}
186	// althought the definitionos of following arithmetic operators are quite
187	// alike, we can't define them using macros, seems macro's parameter can only
188	// be symbols or literals, not operators
189	const ptype<T> operator +(const ptype<T> &p2) const
190	{
191		return ptype<T>(v + p2.v );
192	}
193	const ptype<T> operator -(const ptype<T> &p2) const
194	{
195		return ptype<T>(v - p2.v );
196	}
197	const ptype<T> operator *(const ptype<T> &p2) const
198	{
199		return ptype<T>(v * p2.v );
200	}
201	const ptype<T> operator /(const ptype<T> &p2) const
202	{
203		return ptype<T>(v / p2.v );
204	}
205	const ptype<T> operator %(const ptype<T> &p2) const
206	{
207		return ptype<T>(v % p2.v );
208	}
209	const ptype<T>& operator %=(const ptype<T> &p2) const
210	{
211		v %= p2.v;
212		return *this;
213	}
214
215	const ptype<T>& operator +=(const ptype<T> &p2) const
216	{
217		v += p2.v;
218		return *this;
219	}
220	const ptype<T>& operator -=(const ptype<T> &p2) const
221	{
222		v -= p2.v;
223		return *this;
224	}
225	const ptype<T>& operator /=(const ptype<T> &p2) const
226	{
227		v /= p2.v;
228		return *this;
229	}
230	const ptype<T>& operator *=(const ptype<T> &p2) const
231	{
232		v *= p2.v;
233		return *this;
234	}
235}; // ptype
236
237// the following two functions help io ptype objs to/from any streams
238template<Typename _CharT, Typename _Traits, Typename T>
239basic_istream<_CharT,_Traits>&
240operator>>( basic_istream<_CharT,_Traits> & in, ptype<T>&p)
241{
242	in>>p.v;
243	return in;
244}
245
246template<Typename _CharT, Typename _Traits, Typename T>
247basic_ostream<_CharT,_Traits>&
248operator<<( basic_ostream<_CharT,_Traits> & out, const ptype<T>&p)
249{
250	out<<p.v;
251	return out;
252}
253
254template <Typename T>
255T operator * (T t, const ptype<T> &pt)
256{
257	return t * pt.v;
258}
259
260#endif // !_DB_STL_PTYPE_H
261