summaryrefslogtreecommitdiff
path: root/glcdskin/type.h
blob: 4d54de81eff89b134fe8c736b412e6de6c74a50b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
 * GraphLCD skin library
 *
 * type.h  -  skin type class
 *
 * This file is released under the GNU General Public License. Refer
 * to the COPYING file distributed with this package.
 *
 * based on text2skin
 *
 */

#ifndef _GLCDSKIN_TYPE_H_
#define _GLCDSKIN_TYPE_H_

#include <string>
#include <stdio.h>

#include <stdint.h>
#include <stdlib.h>

namespace GLCD
{

class cType
{
public:
    enum eType
    {
        string,
        number,
        boolean
    };

    //friend class cxFunction;

private:
    eType       mType;
    std::string mString;
    int         mNumber;
    uint32_t    mUpdateIn;

public:
    cType(void): mType(boolean), mNumber(0), mUpdateIn(0) {}
    cType(const char *String): mType(string), mString(String ?: ""), mUpdateIn(0) {}
    cType(std::string String): mType(string), mString(String), mUpdateIn(0) {}
    cType(int Number): mType(number), mNumber(Number), mUpdateIn(0) {}
    cType(time_t Number): mType(number), mNumber(Number), mUpdateIn(0) {}
    cType(bool Value): mType(boolean), mNumber(Value ? 1 : 0), mUpdateIn(0) {}

    std::string String(void) const;
    int Number(void) const { return mType == number ? mNumber : atoi(mString.c_str()); }

    void SetUpdate(uint32_t UpdateIn) { mUpdateIn = UpdateIn; }
    uint32_t UpdateIn(void) const { return mUpdateIn; }

    bool IsString(void)  const { return (mType == string); }
    bool IsNumber(void)  const { return (mType == number); }
    bool IsBoolean(void) const { return (mType == boolean); }

    operator std::string () const { return String(); }
    operator int         () const { return Number(); }
    operator bool        () const;

    friend bool operator== (const cType &a, const cType &b);
    friend bool operator!= (const cType &a, const cType &b);
    friend bool operator<  (const cType &a, const cType &b);
    friend bool operator>  (const cType &a, const cType &b);
    friend bool operator<= (const cType &a, const cType &b);
    friend bool operator>= (const cType &a, const cType &b);
};

inline std::string cType::String(void) const
{
    char str[16];

    switch (mType)
    {
        case number:
            sprintf(str, "%d", mNumber);
            return str;
        case boolean:
            return mNumber != 0 ? "1" : "";
        default:
            return mString;
    }
}

inline cType::operator bool () const
{
    switch (mType)
    {
        case string:
            return mString != "";
        default:
            return mNumber != 0;
    }
}

inline bool operator== (const cType &a, const cType &b)
{
    if (a.mType == cType::string || b.mType == cType::string)
        return a.String() == b.String();
    return a.mNumber == b.mNumber;
}

inline bool operator!= (const cType &a, const cType &b)
{
    if (a.mType == cType::string || b.mType == cType::string)
        return a.String() != b.String();
    return a.mNumber != b.mNumber;
}

inline bool operator< (const cType &a, const cType &b)
{
    if (a.mType == cType::string || b.mType == cType::string)
        return a.String() < b.String();
    return a.mNumber < b.mNumber;
}

inline bool operator> (const cType &a, const cType &b)
{
    if (a.mType == cType::string || b.mType == cType::string)
        return a.String() > b.String();
    return a.mNumber > b.mNumber;
}

inline bool operator<= (const cType &a, const cType &b)
{
    if (a.mType == cType::string || b.mType == cType::string)
        return a.String() <= b.String();
    return a.mNumber <= b.mNumber;
}

inline bool operator>= (const cType &a, const cType &b)
{
    if (a.mType == cType::string || b.mType == cType::string)
        return a.String() >= b.String();
    return a.mNumber >= b.mNumber;
}

} // end of namespace

#endif