(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / Mono.Data.TdsClient / Mono.Data.TdsTypes / TdsBoolean.cs
blob3c2dd09fd994bde3f4abec3bbacd1d8c8e6b248f
1 //
2 // Mono.Data.TdsTypes.TdsBoolean
3 //
4 // Author:
5 // Tim Coleman (tim@timcoleman.com)
6 //
7 // (C) Copyright Tim Coleman, 2002
8 //
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.
31 using Mono.Data.TdsClient;
32 using System;
33 using System.Data.SqlTypes;
34 using System.Globalization;
36 namespace Mono.Data.TdsTypes {
37 public struct TdsBoolean : INullable, IComparable
39 #region Fields
41 byte value;
43 // default is false
44 private bool notNull;
46 public static readonly TdsBoolean False = new TdsBoolean (false);
47 public static readonly TdsBoolean Null;
48 public static readonly TdsBoolean One = new TdsBoolean (1);
49 public static readonly TdsBoolean True = new TdsBoolean (true);
50 public static readonly TdsBoolean Zero = new TdsBoolean (0);
52 #endregion // Fields
54 #region Constructors
56 public TdsBoolean (bool value)
58 this.value = (byte) (value ? 1 : 0);
59 notNull = true;
62 public TdsBoolean (int value)
64 this.value = (byte) (value != 0 ? 1 : 0);
65 notNull = true;
68 #endregion // Constructors
70 #region Properties
72 public byte ByteValue {
73 get {
74 if (this.IsNull)
75 throw new TdsNullValueException(Locale.GetText("The property is set to null."));
76 else
77 return value;
81 public bool IsFalse {
82 get {
83 if (this.IsNull)
84 return false;
85 else
86 return (value == 0);
90 public bool IsNull {
91 get {
92 return !notNull;
96 public bool IsTrue {
97 get {
98 if (this.IsNull)
99 return false;
100 else
101 return (value != 0);
105 public bool Value {
106 get {
107 if (this.IsNull)
108 throw new TdsNullValueException(Locale.GetText("The property is set to null."));
109 else
110 return this.IsTrue;
114 #endregion // Properties
116 public static TdsBoolean And (TdsBoolean x, TdsBoolean y)
118 return (x & y);
121 public int CompareTo (object value)
123 if (value == null)
124 return 1;
125 else if (!(value is TdsBoolean))
126 throw new ArgumentException (Locale.GetText ("Value is not a System.Data.TdsTypes.TdsBoolean"));
127 else if (((TdsBoolean)value).IsNull)
128 return 1;
129 else
130 return this.value.CompareTo (((TdsBoolean)value).ByteValue);
133 public override bool Equals(object value)
135 if (!(value is TdsByte))
136 return false;
137 else
138 return (bool) (this == (TdsBoolean)value);
141 public static TdsBoolean Equals(TdsBoolean x, TdsBoolean y)
143 return (x == y);
146 public override int GetHashCode()
148 return (int)value;
151 public static TdsBoolean NotEquals(TdsBoolean x, TdsBoolean y)
153 return (x != y);
156 public static TdsBoolean OnesComplement(TdsBoolean x)
158 return ~x;
161 public static TdsBoolean Or(TdsBoolean x, TdsBoolean y)
163 return (x | y);
166 public static TdsBoolean Parse(string s)
168 return new TdsBoolean (Boolean.Parse (s));
171 public TdsByte ToTdsByte()
173 return new TdsByte (value);
176 // **************************************************
177 // Conversion from TdsBoolean to other TdsTypes
178 // **************************************************
180 public TdsDecimal ToTdsDecimal()
182 return ((TdsDecimal)this);
185 public TdsDouble ToTdsDouble()
187 return ((TdsDouble)this);
190 public TdsInt16 ToTdsInt16()
192 return ((TdsInt16)this);
195 public TdsInt32 ToTdsInt32()
197 return ((TdsInt32)this);
200 public TdsInt64 ToTdsInt64()
202 return ((TdsInt64)this);
205 public TdsMoney ToTdsMoney()
207 return ((TdsMoney)this);
210 public TdsSingle ToTdsSingle()
212 return ((TdsSingle)this);
215 public TdsString ToTdsString()
217 if (this.IsNull)
218 return new TdsString ("Null");
219 if (this.IsTrue)
220 return new TdsString ("True");
221 else
222 return new TdsString ("False");
225 public override string ToString()
227 if (this.IsNull)
228 return "Null";
229 if (this.IsTrue)
230 return "True";
231 else
232 return "False";
235 // Bitwise exclusive-OR (XOR)
236 public static TdsBoolean Xor(TdsBoolean x, TdsBoolean y)
238 return (x ^ y);
241 // **************************************************
242 // Public Operators
243 // **************************************************
245 // Bitwise AND
246 public static TdsBoolean operator & (TdsBoolean x, TdsBoolean y)
248 return new TdsBoolean (x.Value & y.Value);
251 // Bitwise OR
252 public static TdsBoolean operator | (TdsBoolean x, TdsBoolean y)
254 return new TdsBoolean (x.Value | y.Value);
258 // Compares two instances for equality
259 public static TdsBoolean operator == (TdsBoolean x, TdsBoolean y)
261 if (x.IsNull || y.IsNull)
262 return TdsBoolean.Null;
263 else
264 return new TdsBoolean (x.Value == y.Value);
267 // Bitwize exclusive-OR (XOR)
268 public static TdsBoolean operator ^ (TdsBoolean x, TdsBoolean y)
270 return new TdsBoolean (x.Value ^ y.Value);
273 // test Value of TdsBoolean to determine it is false.
274 public static bool operator false (TdsBoolean x)
276 return x.IsFalse;
279 // in-equality
280 public static TdsBoolean operator != (TdsBoolean x, TdsBoolean y)
282 if (x.IsNull || y.IsNull)
283 return TdsBoolean.Null;
284 else
285 return new TdsBoolean (x.Value != y.Value);
288 // Logical NOT
289 public static TdsBoolean operator ! (TdsBoolean x)
291 if (x.IsNull)
292 return TdsBoolean.Null;
293 else
294 return new TdsBoolean (!x.Value);
297 // One's Complement
298 public static TdsBoolean operator ~ (TdsBoolean x)
300 return new TdsBoolean (~x.ByteValue);
303 // test to see if value is true
304 public static bool operator true (TdsBoolean x)
306 return x.IsTrue;
309 // ****************************************
310 // Type Conversion
311 // ****************************************
314 // TdsBoolean to Boolean
315 public static explicit operator bool (TdsBoolean x)
317 return x.Value;
321 // TdsByte to TdsBoolean
322 public static explicit operator TdsBoolean (TdsByte x)
324 if (x.IsNull)
325 return Null;
326 else
327 return new TdsBoolean ((int)x.Value);
330 // TdsDecimal to TdsBoolean
331 public static explicit operator TdsBoolean (TdsDecimal x)
333 if (x.IsNull)
334 return Null;
335 else
336 return new TdsBoolean ((int)x.Value);
339 // TdsDouble to TdsBoolean
340 public static explicit operator TdsBoolean (TdsDouble x)
342 if (x.IsNull)
343 return Null;
344 else
345 return new TdsBoolean ((int)x.Value);
348 // TdsInt16 to TdsBoolean
349 public static explicit operator TdsBoolean (TdsInt16 x)
351 if (x.IsNull)
352 return Null;
353 else
354 return new TdsBoolean ((int)x.Value);
357 // TdsInt32 to TdsBoolean
358 public static explicit operator TdsBoolean (TdsInt32 x)
360 if (x.IsNull)
361 return Null;
362 else
363 return new TdsBoolean (x.Value);
366 // TdsInt64 to TdsBoolean
367 public static explicit operator TdsBoolean (TdsInt64 x)
369 if (x.IsNull)
370 return Null;
371 else
372 return new TdsBoolean ((int)x.Value);
375 // TdsMoney to TdsBoolean
376 public static explicit operator TdsBoolean (TdsMoney x)
378 if (x.IsNull)
379 return Null;
380 else
381 return new TdsBoolean ((int)x.Value);
384 // TdsSingle to TdsBoolean
385 public static explicit operator TdsBoolean (TdsSingle x)
387 if (x.IsNull)
388 return Null;
389 else
390 return new TdsBoolean ((int)x.Value);
393 // TdsString to TdsBoolean
394 public static explicit operator TdsBoolean (TdsString x)
396 return TdsBoolean.Parse (x.Value);
399 // Boolean to TdsBoolean
400 public static implicit operator TdsBoolean (bool x)
402 return new TdsBoolean (x);