~ubuntu-branches/ubuntu/karmic/firebird2.1/karmic

« back to all changes in this revision

Viewing changes to src/common/classes/TriState.h

  • Committer: Bazaar Package Importer
  • Author(s): Damyan Ivanov
  • Date: 2008-05-26 23:59:25 UTC
  • Revision ID: james.westby@ubuntu.com-20080526235925-2pnqj6nxpppoeaer
Tags: upstream-2.1.0.17798-0.ds2
ImportĀ upstreamĀ versionĀ 2.1.0.17798-0.ds2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *      PROGRAM:                Firebird aux classes.
 
3
 *      MODULE:                 TriState.h
 
4
 *      DESCRIPTION:    Firebird's SQL tri-state emulation class.
 
5
 *
 
6
 *  The contents of this file are subject to the Initial
 
7
 *  Developer's Public License Version 1.0 (the "License");
 
8
 *  you may not use this file except in compliance with the
 
9
 *  License. You may obtain a copy of the License at
 
10
 *  http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
 
11
 *
 
12
 *  Software distributed under the License is distributed AS IS,
 
13
 *  WITHOUT WARRANTY OF ANY KIND, either express or implied.
 
14
 *  See the License for the specific language governing rights
 
15
 *  and limitations under the License.
 
16
 *
 
17
 *  The Original Code was created by Claudio Valderrama on 28-Aug-2007
 
18
 *  for the Firebird Open Source RDBMS project.
 
19
 *
 
20
 *  Copyright (c) 2007 Claudio Valderrama
 
21
 *  and all contributors signed below.
 
22
 *
 
23
 *  All Rights Reserved.
 
24
 *  Contributor(s): ______________________________________.
 
25
 *
 
26
 *
 
27
 */
 
28
 
 
29
#ifndef CLASSES_TRISTATE_H
 
30
#define CLASSES_TRISTATE_H
 
31
 
 
32
#include "firebird.h"
 
33
 
 
34
 
 
35
class TriState
 
36
{
 
37
public:
 
38
        TriState();
 
39
        explicit TriState(bool input);
 
40
 
 
41
        void operator=(bool input);
 
42
 
 
43
        bool asBool() const;
 
44
        void reset();
 
45
        bool assignOnce(bool input);
 
46
        bool isUnknown() const;
 
47
        bool isAssigned() const;
 
48
        bool toggle();
 
49
 
 
50
private:
 
51
        bool m_init, m_val;
 
52
};
 
53
 
 
54
// The var is left uninitialized.
 
55
inline TriState::TriState()
 
56
        : m_init(false), m_val(false)
 
57
{
 
58
}
 
59
 
 
60
// The var is initialized to the explicit value.
 
61
inline TriState::TriState(bool input)
 
62
        : m_init(true), m_val(input)
 
63
{
 
64
}
 
65
 
 
66
// The var receives a T/F value.
 
67
inline void TriState::operator=(bool input)
 
68
{
 
69
        m_init = true;
 
70
        m_val = input;
 
71
}
 
72
 
 
73
// The var is coerced to a T/F value as result.
 
74
inline bool TriState::asBool() const
 
75
{
 
76
        return m_init && m_val;
 
77
}
 
78
 
 
79
// The var is returned to its uninitialized state.
 
80
inline void TriState::reset()
 
81
{
 
82
        m_init = m_val = false;
 
83
}
 
84
 
 
85
// The assignment succeeds only if the var is uninitialized.
 
86
inline bool TriState::assignOnce(bool input)
 
87
{
 
88
        if (m_init)
 
89
                return false;
 
90
 
 
91
        m_init = true;
 
92
        m_val = input;
 
93
        return true;
 
94
}
 
95
 
 
96
// Tests whether the var is uninitialized.
 
97
inline bool TriState::isUnknown() const
 
98
{
 
99
        return !m_init;
 
100
}
 
101
 
 
102
// Tests whether the var is initialized.
 
103
inline bool TriState::isAssigned() const
 
104
{
 
105
        return m_init;
 
106
}
 
107
 
 
108
// The var is toggled between T and F only if it's already initialized.
 
109
inline bool TriState::toggle()
 
110
{
 
111
        if (!m_init)
 
112
                return false;
 
113
                
 
114
        m_val = !m_val;
 
115
        return true;
 
116
}
 
117
 
 
118
 
 
119
#endif // CLASSES_TRISTATE_H