This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mcs / class / Mono.Data.TdsClient / Mono.Data.TdsTypes / TdsByte.cs
1 //
2 // Mono.Data.TdsTypes.TdsByte
3 //
4 // Author:
5 //   Tim Coleman <tim@timcoleman.com>
6 //
7 // (C) Copyright Tim Coleman, 2002
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using Mono.Data.TdsClient;
32 using System;
33 using System.Data.SqlTypes;
34 using System.Globalization;
35
36 namespace Mono.Data.TdsTypes {
37         public struct TdsByte : INullable, IComparable
38         {
39                 #region Fields
40
41                 byte value;
42                 private bool notNull;
43
44                 public static readonly TdsByte MaxValue = new TdsByte (0xff);
45                 public static readonly TdsByte MinValue = new TdsByte (0);
46                 public static readonly TdsByte Null;
47                 public static readonly TdsByte Zero = new TdsByte (0);
48
49                 #endregion
50
51                 #region Constructors
52
53                 public TdsByte (byte value) 
54                 {
55                         this.value = value;
56                         notNull = true;
57                 }
58
59                 #endregion
60
61                 #region Properties
62
63                 public bool IsNull {
64                         get { return !notNull; }
65                 }
66
67                 public byte Value { 
68                         get { 
69                                 if (this.IsNull) 
70                                         throw new TdsNullValueException ();
71                                 else 
72                                         return value; 
73                         }
74                 }
75
76                 #endregion
77
78                 #region Methods
79
80                 public static TdsByte Add (TdsByte x, TdsByte y)
81                 {
82                         return (x + y);
83                 }
84
85                 public static TdsByte BitwiseAnd (TdsByte x, TdsByte y)
86                 {
87                         return (x & y);
88                 }
89
90                 public static TdsByte BitwiseOr (TdsByte x, TdsByte y)
91                 {
92                         return (x | y);
93                 }
94
95                 public int CompareTo (object value)
96                 {
97                         if (value == null)
98                                 return 1;
99                         else if (!(value is TdsByte))
100                                 throw new ArgumentException (Locale.GetText ("Value is not a System.Data.TdsTypes.TdsByte"));
101                         else if (((TdsByte)value).IsNull)
102                                 return 1;
103                         else
104                                 return this.value.CompareTo (((TdsByte)value).Value);
105                 }
106
107                 public static TdsByte Divide (TdsByte x, TdsByte y)
108                 {
109                         return (x / y);
110                 }
111
112                 public override bool Equals (object value)
113                 {
114                         if (!(value is TdsByte))
115                                 return false;
116                         else
117                                 return (bool) (this == (TdsByte)value);
118                 }
119
120                 public static TdsBoolean Equals (TdsByte x, TdsByte y)
121                 {
122                         return (x == y);
123                 }
124
125                 public override int GetHashCode ()
126                 {
127                         return (int)value;
128                 }
129
130                 public static TdsBoolean GreaterThan (TdsByte x, TdsByte y)
131                 {
132                         return (x > y);
133                 }
134
135                 public static TdsBoolean GreaterThanOrEqual (TdsByte x, TdsByte y)
136                 {
137                         return (x >= y);
138                 }
139
140                 public static TdsBoolean LessThan (TdsByte x, TdsByte y)
141                 {
142                         return (x < y);
143                 }
144
145                 public static TdsBoolean LessThanOrEqual (TdsByte x, TdsByte y)
146                 {
147                         return (x <= y);
148                 }
149
150                 public static TdsByte Mod (TdsByte x, TdsByte y)
151                 {
152                         return (x % y);
153                 }
154
155                 public static TdsByte Multiply (TdsByte x, TdsByte y)
156                 {
157                         return (x * y);
158                 }
159
160                 public static TdsBoolean NotEquals (TdsByte x, TdsByte y)
161                 {
162                         return (x != y);
163                 }
164
165                 public static TdsByte OnesComplement (TdsByte x)
166                 {
167                         return ~x;
168                 }
169
170                 public static TdsByte Parse (string s)
171                 {
172                         return new TdsByte (Byte.Parse (s));
173                 }
174
175                 public static TdsByte Subtract (TdsByte x, TdsByte y)
176                 {
177                         return (x - y);
178                 }
179
180                 public TdsBoolean ToTdsBoolean ()
181                 {
182                         return ((TdsBoolean)this);
183                 }
184                 
185                 public TdsDecimal ToTdsDecimal ()
186                 {
187                         return ((TdsDecimal)this);
188                 }
189
190                 public TdsDouble ToTdsDouble ()
191                 {
192                         return ((TdsDouble)this);
193                 }
194
195                 public TdsInt16 ToTdsInt16 ()
196                 {
197                         return ((TdsInt16)this);
198                 }
199
200                 public TdsInt32 ToTdsInt32 ()
201                 {
202                         return ((TdsInt32)this);
203                 }
204
205                 public TdsInt64 ToTdsInt64 ()
206                 {
207                         return ((TdsInt64)this);
208                 }
209
210                 public TdsMoney ToTdsMoney ()
211                 {
212                         return ((TdsMoney)this);
213                 }
214
215                 public TdsSingle ToTdsSingle ()
216                 {
217                         return ((TdsSingle)this);
218                 }
219
220                 public TdsString ToTdsString ()
221                 {
222                         return ((TdsString)this);
223                 }
224
225                 public override string ToString ()
226                 {
227                         if (this.IsNull)
228                                 return "Null";
229                         else
230                                 return value.ToString ();
231                 }
232
233                 public static TdsByte Xor (TdsByte x, TdsByte y)
234                 {
235                         return (x ^ y);
236                 }
237
238                 public static TdsByte operator + (TdsByte x, TdsByte y)
239                 {
240                         return new TdsByte ((byte) (x.Value + y.Value));
241                 }
242
243                 public static TdsByte operator & (TdsByte x, TdsByte y)
244                 {
245                         return new TdsByte ((byte) (x.Value & y.Value));
246                 }
247
248                 public static TdsByte operator | (TdsByte x, TdsByte y)
249                 {
250                         return new TdsByte ((byte) (x.Value | y.Value));
251                 }
252
253                 public static TdsByte operator / (TdsByte x, TdsByte y)
254                 {
255                         return new TdsByte ((byte) (x.Value / y.Value));
256                 }
257
258                 public static TdsBoolean operator == (TdsByte x, TdsByte y)
259                 {
260                         if (x.IsNull || y.IsNull) 
261                                 return TdsBoolean.Null;
262                         else
263                                 return new TdsBoolean (x.Value == y.Value);
264                 }
265
266                 public static TdsByte operator ^ (TdsByte x, TdsByte y)
267                 {
268                         return new TdsByte ((byte) (x.Value ^ y.Value));
269                 }
270
271                 public static TdsBoolean operator > (TdsByte x, TdsByte y)
272                 {
273                         if (x.IsNull || y.IsNull) 
274                                 return TdsBoolean.Null;
275                         else
276                                 return new TdsBoolean (x.Value > y.Value);
277                 }
278
279                 public static TdsBoolean operator >= (TdsByte x, TdsByte y)
280                 {
281                         if (x.IsNull || y.IsNull) 
282                                 return TdsBoolean.Null;
283                         else
284                                 return new TdsBoolean (x.Value >= y.Value);
285                 }
286
287                 public static TdsBoolean operator != (TdsByte x, TdsByte y)
288                 {
289                         if (x.IsNull || y.IsNull) 
290                                 return TdsBoolean.Null;
291                         else
292                                 return new TdsBoolean (!(x.Value == y.Value));
293                 }
294
295                 public static TdsBoolean operator < (TdsByte x, TdsByte y)
296                 {
297                         if (x.IsNull || y.IsNull) 
298                                 return TdsBoolean.Null;
299                         else
300                                 return new TdsBoolean (x.Value < y.Value);
301                 }
302
303                 public static TdsBoolean operator <= (TdsByte x, TdsByte y)
304                 {
305                         if (x.IsNull || y.IsNull) 
306                                 return TdsBoolean.Null;
307                         else
308                                 return new TdsBoolean (x.Value <= y.Value);
309                 }
310
311                 public static TdsByte operator % (TdsByte x, TdsByte y)
312                 {
313                         return new TdsByte ((byte) (x.Value % y.Value));
314                 }
315
316                 public static TdsByte operator * (TdsByte x, TdsByte y)
317                 {
318                         return new TdsByte ((byte) (x.Value * y.Value));
319                 }
320
321                 public static TdsByte operator ~ (TdsByte x)
322                 {
323                         return new TdsByte ((byte) ~x.Value);
324                 }
325
326                 public static TdsByte operator - (TdsByte x, TdsByte y)
327                 {
328                         return new TdsByte ((byte) (x.Value - y.Value));
329                 }
330
331                 public static explicit operator TdsByte (TdsBoolean x)
332                 {
333                         if (x.IsNull)
334                                 return Null;
335                         else
336                                 return new TdsByte (x.ByteValue);
337                 }
338
339                 public static explicit operator byte (TdsByte x)
340                 {
341                         return x.Value;
342                 }
343
344                 public static explicit operator TdsByte (TdsDecimal x)
345                 {
346                         if (x.IsNull)
347                                 return Null;
348                         else
349                                 return new TdsByte ((byte)x.Value);
350                 }
351
352                 public static explicit operator TdsByte (TdsDouble x)
353                 {
354                         if (x.IsNull)
355                                 return Null;
356                         else
357                                 return new TdsByte ((byte)x.Value);
358                 }
359
360                 public static explicit operator TdsByte (TdsInt16 x)
361                 {
362                         if (x.IsNull)
363                                 return Null;
364                         else
365                                 return new TdsByte ((byte)x.Value);
366                 }
367
368                 public static explicit operator TdsByte (TdsInt32 x)
369                 {
370                         if (x.IsNull)
371                                 return Null;
372                         else
373                                 return new TdsByte ((byte)x.Value);
374                 }
375
376                 public static explicit operator TdsByte (TdsInt64 x)
377                 {
378                         if (x.IsNull)
379                                 return Null;
380                         else
381                                 return new TdsByte ((byte)x.Value);
382                 }
383
384                 public static explicit operator TdsByte (TdsMoney x)
385                 {
386                         if (x.IsNull)
387                                 return Null;
388                         else
389                                 return new TdsByte ((byte)x.Value);
390                 }
391
392                 public static explicit operator TdsByte (TdsSingle x)
393                 {
394                         if (x.IsNull)
395                                 return Null;
396                         else
397                                 return new TdsByte ((byte)x.Value);
398                 }
399
400
401                 public static explicit operator TdsByte (TdsString x)
402                 {
403                         return TdsByte.Parse (x.Value);
404                 }
405
406                 public static implicit operator TdsByte (byte x)
407                 {
408                         return new TdsByte (x);
409                 }
410                 
411                 #endregion
412         }
413 }
414