2 // Mono.Data.TdsTypes.TdsBoolean
5 // Tim Coleman (tim@timcoleman.com)
7 // (C) Copyright Tim Coleman, 2002
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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
;
33 using System
.Data
.SqlTypes
;
34 using System
.Globalization
;
36 namespace Mono
.Data
.TdsTypes
{
37 public struct TdsBoolean
: INullable
, IComparable
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);
56 public TdsBoolean (bool value)
58 this.value = (byte) (value ? 1 : 0);
62 public TdsBoolean (int value)
64 this.value = (byte) (value != 0 ? 1 : 0);
68 #endregion // Constructors
72 public byte ByteValue
{
75 throw new TdsNullValueException(Locale
.GetText("The property is set to null."));
108 throw new TdsNullValueException(Locale
.GetText("The property is set to null."));
114 #endregion // Properties
116 public static TdsBoolean
And (TdsBoolean x
, TdsBoolean y
)
121 public int CompareTo (object value)
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
)
130 return this.value.CompareTo (((TdsBoolean
)value).ByteValue
);
133 public override bool Equals(object value)
135 if (!(value is TdsByte
))
138 return (bool) (this == (TdsBoolean
)value);
141 public static TdsBoolean
Equals(TdsBoolean x
, TdsBoolean y
)
146 public override int GetHashCode()
151 public static TdsBoolean
NotEquals(TdsBoolean x
, TdsBoolean y
)
156 public static TdsBoolean
OnesComplement(TdsBoolean x
)
161 public static TdsBoolean
Or(TdsBoolean x
, TdsBoolean 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()
218 return new TdsString ("Null");
220 return new TdsString ("True");
222 return new TdsString ("False");
225 public override string ToString()
235 // Bitwise exclusive-OR (XOR)
236 public static TdsBoolean
Xor(TdsBoolean x
, TdsBoolean y
)
241 // **************************************************
243 // **************************************************
246 public static TdsBoolean
operator & (TdsBoolean x
, TdsBoolean y
)
248 return new TdsBoolean (x
.Value
& y
.Value
);
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
;
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
)
280 public static TdsBoolean
operator != (TdsBoolean x
, TdsBoolean y
)
282 if (x
.IsNull
|| y
.IsNull
)
283 return TdsBoolean
.Null
;
285 return new TdsBoolean (x
.Value
!= y
.Value
);
289 public static TdsBoolean
operator ! (TdsBoolean x
)
292 return TdsBoolean
.Null
;
294 return new TdsBoolean (!x
.Value
);
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
)
309 // ****************************************
311 // ****************************************
314 // TdsBoolean to Boolean
315 public static explicit operator bool (TdsBoolean x
)
321 // TdsByte to TdsBoolean
322 public static explicit operator TdsBoolean (TdsByte x
)
327 return new TdsBoolean ((int)x
.Value
);
330 // TdsDecimal to TdsBoolean
331 public static explicit operator TdsBoolean (TdsDecimal x
)
336 return new TdsBoolean ((int)x
.Value
);
339 // TdsDouble to TdsBoolean
340 public static explicit operator TdsBoolean (TdsDouble x
)
345 return new TdsBoolean ((int)x
.Value
);
348 // TdsInt16 to TdsBoolean
349 public static explicit operator TdsBoolean (TdsInt16 x
)
354 return new TdsBoolean ((int)x
.Value
);
357 // TdsInt32 to TdsBoolean
358 public static explicit operator TdsBoolean (TdsInt32 x
)
363 return new TdsBoolean (x
.Value
);
366 // TdsInt64 to TdsBoolean
367 public static explicit operator TdsBoolean (TdsInt64 x
)
372 return new TdsBoolean ((int)x
.Value
);
375 // TdsMoney to TdsBoolean
376 public static explicit operator TdsBoolean (TdsMoney x
)
381 return new TdsBoolean ((int)x
.Value
);
384 // TdsSingle to TdsBoolean
385 public static explicit operator TdsBoolean (TdsSingle x
)
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
);