~ubuntu-branches/ubuntu/hardy/blam/hardy-updates

« back to all changes in this revision

Viewing changes to src/rss/Shared/DBBool.cs

  • Committer: Bazaar Package Importer
  • Author(s): Barry deFreese
  • Date: 2005-09-27 21:38:56 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20050927213856-8no09ppptvhpqqsh
Tags: 1.8.2-2ubuntu6
Change .desktop comment description

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* DBBool.cs
2
 
 * =========
3
 
 * 
4
 
 * RSS.NET (http://rss-net.sf.net/)
5
 
 * Copyright � 2002, 2003 George Tsiokos. All Rights Reserved.
6
 
 * 
7
 
 * RSS 2.0 (http://blogs.law.harvard.edu/tech/rss)
8
 
 * RSS 2.0 is offered by the Berkman Center for Internet & Society at 
9
 
 * Harvard Law School under the terms of the Attribution/Share Alike 
10
 
 * Creative Commons license.
11
 
 * 
12
 
 * Permission is hereby granted, free of charge, to any person obtaining 
13
 
 * a copy of this software and associated documentation files (the "Software"), 
14
 
 * to deal in the Software without restriction, including without limitation 
15
 
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
16
 
 * and/or sell copies of the Software, and to permit persons to whom the 
17
 
 * Software is furnished to do so, subject to the following conditions:
18
 
 * 
19
 
 * The above copyright notice and this permission notice shall be included in all
20
 
 * copies or substantial portions of the Software.
21
 
 * 
22
 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
23
 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
24
 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
25
 
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
26
 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
27
 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
28
 
 * THE SOFTWARE.
29
 
*/
30
 
using System;
31
 
 
32
 
namespace Rss
33
 
{
34
 
        /// <summary>Represents Null, False, and True</summary>
35
 
        /// <remarks>Source: Microsoft c# example</remarks>
36
 
        [Serializable()]
37
 
        public struct DBBool
38
 
        {
39
 
                /// <summary>A DBBool containing 'Null'.</summary>
40
 
                /// <remarks>One of three possible DBBool values.</remarks>
41
 
                public static readonly DBBool Null = new DBBool(0);
42
 
                /// <summary>A DBBool containing 'False'.</summary>
43
 
                /// <remarks>One of three possible DBBool values.</remarks>
44
 
                public static readonly DBBool False = new DBBool(-1);
45
 
                /// <summary>A DBBool containing 'True'.</summary>
46
 
                /// <remarks>One of three possible DBBool values.</remarks>
47
 
                public static readonly DBBool True = new DBBool(1);
48
 
                /// <summary>Private field that stores �1, 0, 1 for False, Null, True.</summary>
49
 
                sbyte value;
50
 
                /// <summary>Private instance constructor. The value parameter must be �1, 0, or 1.</summary>
51
 
                DBBool(int value) 
52
 
                {
53
 
                        this.value = (sbyte)value;
54
 
                }
55
 
                /// <summary>Properties to examine the value of a DBBool.</summary>
56
 
                /// <remarks>Return true if this DBBool has the given value, false otherwise.</remarks>
57
 
                public bool IsNull { get { return value == 0; } }
58
 
                /// <summary>Properties to examine the value of a DBBool.</summary>
59
 
                /// <remarks>Return true if this DBBool has the given value, false otherwise.</remarks>
60
 
                public bool IsFalse { get { return value < 0; } }
61
 
                /// <summary>Properties to examine the value of a DBBool.</summary>
62
 
                /// <remarks>Return true if this DBBool has the given value, false otherwise.</remarks>
63
 
                public bool IsTrue { get { return value > 0; } }
64
 
                /// <summary>Implicit conversion from bool to DBBool. Maps true to DBBool.True and false to DBBool.False.</summary>
65
 
                /// <param name="x">a DBBool</param>
66
 
                public static implicit operator DBBool(bool x) 
67
 
                {
68
 
                        return x? True: False;
69
 
                }
70
 
                /// <summary>Explicit conversion from DBBool to bool.</summary>
71
 
                /// <exception cref="InvalidOperationException">The given DBBool is Null</exception>
72
 
                /// <param name="x">a DBBool</param>
73
 
                /// <returns>true or false</returns>
74
 
                public static explicit operator bool(DBBool x) 
75
 
                {
76
 
                        if (x.value == 0) throw new InvalidOperationException();
77
 
                        return x.value > 0;
78
 
                }
79
 
                /// <summary>Equality operator.</summary>
80
 
                /// <param name="x">a DBBool</param>
81
 
                /// <param name="y">a DBBool</param>
82
 
                /// <returns>Returns Null if either operand is Null, otherwise returns True or False.</returns>
83
 
                public static DBBool operator ==(DBBool x, DBBool y) 
84
 
                {
85
 
                        if (x.value == 0 || y.value == 0) return Null;
86
 
                        return x.value == y.value? True: False;
87
 
                }
88
 
                /// <summary>Inequality operator.</summary>
89
 
                /// <param name="x">a DBBool</param>
90
 
                /// <param name="y">a DBBool</param>
91
 
                /// <returns>Returns Null if either operand is Null, otherwise returns True or False.</returns>
92
 
                public static DBBool operator !=(DBBool x, DBBool y) 
93
 
                {
94
 
                        if (x.value == 0 || y.value == 0) return Null;
95
 
                        return x.value != y.value? True: False;
96
 
                }
97
 
                /// <summary>Logical negation operator.</summary>
98
 
                /// <param name="x">a DBBool</param>
99
 
                /// <returns>Returns True if the operand is False, Null if the operand is Null, or False if the operand is True.</returns>
100
 
                public static DBBool operator !(DBBool x) 
101
 
                {
102
 
                        return new DBBool(-x.value);
103
 
                }
104
 
                /// <summary>Logical AND operator.</summary>
105
 
                /// <param name="x">a DBBool</param>
106
 
                /// <param name="y">a DBBool</param>
107
 
                /// <returns>Returns False if either operand is False, otherwise Null if either operand is Null, otherwise True.</returns>
108
 
                public static DBBool operator &(DBBool x, DBBool y) 
109
 
                {
110
 
                        return new DBBool(x.value < y.value? x.value: y.value);
111
 
                }
112
 
                /// <summary>Logical OR operator.</summary>
113
 
                /// <param name="x">a DBBool</param>
114
 
                /// <param name="y">a DBBool</param>
115
 
                /// <returns>Returns True if either operand is True, otherwise Null if either operand is Null, otherwise False.</returns>
116
 
                public static DBBool operator |(DBBool x, DBBool y) 
117
 
                {
118
 
                        return new DBBool(x.value > y.value? x.value: y.value);
119
 
                }
120
 
                /// <summary>Definitely true operator.</summary>
121
 
                /// <param name="x">a DBBool</param>
122
 
                /// <returns>Returns true if the operand is True, false otherwise.</returns>
123
 
                public static bool operator true(DBBool x) 
124
 
                {
125
 
                        return x.value > 0;
126
 
                }
127
 
                /// <summary>Definitely false operator.</summary>
128
 
                /// <param name="x">a DBBool</param>
129
 
                /// <returns>Returns true if the operand is False, false otherwise.</returns>
130
 
                public static bool operator false(DBBool x) 
131
 
                {
132
 
                        return x.value < 0;
133
 
                }
134
 
                /// <summary>Determines whether two DBBool instances are equal.</summary>
135
 
                /// <param name="o">The object to check.</param>
136
 
                /// <returns>True if the two DBBools are equal.</returns>
137
 
                public override bool Equals(object o) 
138
 
                {
139
 
                        try 
140
 
                        {
141
 
                                return (bool) (this == (DBBool) o);
142
 
                        }
143
 
                        catch 
144
 
                        {
145
 
                                return false;
146
 
                        }
147
 
                }
148
 
                /// <summary>Serves as a hash function for a particular type, suitable for use in hashing algorithms and data structures like a hash table.</summary>
149
 
                /// <returns>A hash code for the current DBBool.</returns>
150
 
                public override int GetHashCode() 
151
 
                {
152
 
                        return value;
153
 
                }
154
 
                /// <summary>Returns a string representation of the current Object.</summary>
155
 
                /// <exception cref="InvalidOperationException">Object has not been initialized.</exception>
156
 
                /// <returns>A string containing DBBool.False, DBBool.Null, or DBBool.True</returns>
157
 
                public override string ToString() 
158
 
                {
159
 
                        switch (value) 
160
 
                        {
161
 
                                case -1:
162
 
                                        return "DBBool.False";
163
 
                                case 0:
164
 
                                        return "DBBool.Null";
165
 
                                case 1:
166
 
                                        return "DBBool.True";
167
 
                                default:
168
 
                                        throw new InvalidOperationException();
169
 
                        }
170
 
                }
171
 
        }
172
 
}