~ubuntu-branches/ubuntu/raring/mdds/raring

« back to all changes in this revision

Viewing changes to include/mdds/mixed_type_matrix_element.hpp

  • Committer: Bazaar Package Importer
  • Author(s): Rene Engelhard
  • Date: 2010-12-21 03:00:49 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20101221030049-15awcps1vyzo90s0
Tags: 0.4.0-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*************************************************************************
 
2
 *
 
3
 * Copyright (c) 2010 Kohei Yoshida
 
4
 * 
 
5
 * Permission is hereby granted, free of charge, to any person
 
6
 * obtaining a copy of this software and associated documentation
 
7
 * files (the "Software"), to deal in the Software without
 
8
 * restriction, including without limitation the rights to use,
 
9
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 
10
 * copies of the Software, and to permit persons to whom the
 
11
 * Software is furnished to do so, subject to the following
 
12
 * conditions:
 
13
 * 
 
14
 * The above copyright notice and this permission notice shall be
 
15
 * included in all copies or substantial portions of the Software.
 
16
 * 
 
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
18
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 
19
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
20
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 
21
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 
22
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 
23
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 
24
 * OTHER DEALINGS IN THE SOFTWARE.
 
25
 *
 
26
 ************************************************************************/
 
27
 
 
28
#ifndef __MDDS_MIXED_TYPE_MATRIX_ELEMENT_HPP__
 
29
#define __MDDS_MIXED_TYPE_MATRIX_ELEMENT_HPP__
 
30
 
 
31
namespace mdds {
 
32
 
 
33
enum matrix_element_t
 
34
 
35
    element_empty   = 0, 
 
36
    element_numeric = 1, 
 
37
    element_boolean = 2, 
 
38
    element_string  = 3 
 
39
};
 
40
 
 
41
template<typename _String>
 
42
struct element
 
43
{
 
44
    typedef _String     string_type;
 
45
 
 
46
    matrix_element_t m_type:2;
 
47
 
 
48
    union
 
49
    {
 
50
        double       m_numeric;
 
51
        bool         m_boolean;
 
52
        string_type* mp_string;
 
53
    };
 
54
 
 
55
    element() : m_type(element_empty) {}
 
56
    element(const element& r) : m_type(r.m_type)
 
57
    {
 
58
        switch (m_type)
 
59
        {
 
60
            case element_boolean:
 
61
                m_boolean = r.m_boolean;
 
62
                break;
 
63
            case element_numeric:
 
64
                m_numeric = r.m_numeric;
 
65
                break;
 
66
            case element_string:
 
67
                mp_string = new string_type(*r.mp_string);
 
68
                break;
 
69
            case element_empty:
 
70
            default:
 
71
                ;
 
72
        }
 
73
    }
 
74
 
 
75
    explicit element(double v) : m_type(element_numeric), m_numeric(v) {}
 
76
    explicit element(bool v) : m_type(element_boolean), m_boolean(v) {}
 
77
    explicit element(string_type* p) : m_type(element_string), mp_string(p) {}
 
78
 
 
79
    bool operator== (const element& r) const
 
80
    {
 
81
        if (m_type != r.m_type)
 
82
            return false;
 
83
 
 
84
        switch (m_type)
 
85
        {
 
86
            case element_boolean:
 
87
                return m_boolean == r.m_boolean;
 
88
            case element_numeric:
 
89
                return m_numeric == r.m_numeric;
 
90
            case element_string:
 
91
                return *mp_string == *r.mp_string;
 
92
            case element_empty:
 
93
            default:
 
94
                ;
 
95
        }
 
96
 
 
97
        return true;
 
98
    }
 
99
 
 
100
    element& operator= (const element& r)
 
101
    {
 
102
        if (m_type == element_string)
 
103
            delete mp_string;
 
104
 
 
105
        m_type = r.m_type;
 
106
 
 
107
        switch (m_type)
 
108
        {
 
109
            case element_boolean:
 
110
                m_boolean = r.m_boolean;
 
111
                break;
 
112
            case element_numeric:
 
113
                m_numeric = r.m_numeric;
 
114
                break;
 
115
            case element_string:
 
116
                mp_string = new string_type(*r.mp_string);
 
117
                break;
 
118
            case element_empty:
 
119
            default:
 
120
                ;
 
121
        }   
 
122
        return *this;
 
123
    }
 
124
 
 
125
    ~element()
 
126
    {
 
127
        clear_string();
 
128
    }
 
129
 
 
130
    void clear_string()
 
131
    {
 
132
        if (m_type == element_string)
 
133
            delete mp_string;
 
134
    }
 
135
 
 
136
    void set_empty()
 
137
    {
 
138
        clear_string();
 
139
        m_type = element_empty;
 
140
    }
 
141
 
 
142
    void set_numeric(double val)
 
143
    {
 
144
        clear_string();
 
145
        m_type = element_numeric;
 
146
        m_numeric = val;
 
147
    }
 
148
 
 
149
    void set_boolean(bool val)
 
150
    {
 
151
        clear_string();
 
152
        m_type = element_boolean;
 
153
        m_boolean = val;
 
154
    }
 
155
 
 
156
    void set_string(string_type* str)
 
157
    {
 
158
        clear_string();
 
159
        m_type = element_string;
 
160
        mp_string = str;
 
161
    }
 
162
};
 
163
 
 
164
}
 
165
 
 
166
#endif