summaryrefslogtreecommitdiff
path: root/xml/type.h
blob: e37d99d443364643ef77e8c5ebf3c96111dfec12 (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
/*
 *  $Id: type.h,v 1.7 2005/01/31 14:39:23 lordjaxom Exp $
 */

#ifndef VDR_TEXT2SKIN_XML_TYPE_H
#define VDR_TEXT2SKIN_XML_TYPE_H

#include <string>
#include <vdr/tools.h>
#include <stdio.h>

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

	friend class cxFunction;

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

public:
	cxType(void): mType(boolean), mNumber(0), mUpdateIn(0) {}
	cxType(const char *String): mType(string), mString(String ?: ""), mUpdateIn(0) {}
	cxType(std::string String): mType(string), mString(String), mUpdateIn(0) {}
	cxType(int Number): mType(number), mNumber(Number), mUpdateIn(0) {}
	cxType(time_t Number): mType(number), mNumber(Number), mUpdateIn(0) {}
	cxType(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(uint UpdateIn) { mUpdateIn = UpdateIn; }
	uint UpdateIn(void) const { return mUpdateIn; }

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

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

inline std::string cxType::String(void) const {
	switch (mType) {
	case number:  return (const char*)itoa(mNumber);
	case boolean: return mNumber != 0 ? "1" : "";
	default:      return mString;
	}
}

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

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

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

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

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

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

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

#endif // VDR_TEXT2SKIN_XML_TYPE_H