summaryrefslogtreecommitdiff
path: root/project2/variables.h
blob: 6dbbf2b7f52e9789b19ad75562392441c9a324ad (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
#ifndef VARIABLES_H
#define VARIABLES_H

#include <boost/intrusive_ptr.hpp>
#include <boost/optional.hpp>
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include <stdint.h>
#include <glibmm/ustring.h>
#include <libxml++/nodes/element.h>
#include <libxml++/attribute.h>
#include "intrusivePtrBase.h"
#include "xmlObjectLoader.h"
#include <boost/variant.hpp>
#include <boost/shared_ptr.hpp>

class Null {
	public:
		bool operator<(const Null &) const;
};
typedef boost::variant<
	Null,
	// Strings
	Glib::ustring,
	// Numbers
	long long unsigned int,
	long unsigned int,
	unsigned int,
	short unsigned int,
	long long int,
	long int,
	int,
	short int,
	double,
	float,
	// DateTimes
	boost::posix_time::ptime
	> _VT;

class VariableType : public _VT {
	public:
		typedef void(*Freer)(const void*);
		template<typename T>
		VariableType(const T & t) : _VT(t), convertCache(NULL), freer(NULL) { }
		VariableType();
		VariableType(const VariableType &);
		~VariableType();
		void operator=(const VariableType &);
		bool operator<(const VariableType &) const;

		operator const Glib::ustring &() const;
		operator const std::string &() const;
		operator const char *() const;
		operator const unsigned char *() const;
		operator int64_t() const;
		operator int32_t() const;
		operator double() const;
		operator const boost::posix_time::ptime &() const;
		template <typename T> const T * get() const { return boost::get<T>(this); }

	private:
		template <typename T> friend const T * set(const VariableType * var, const T * t, Freer);
		mutable const void * convertCache;
		mutable Freer freer;
};

/// Base class for Project2 variable accessors
class VariableImpl : public IntrusivePtrBase {
	public:
		virtual VariableType value() const = 0;

	protected:
		virtual ~VariableImpl() = 0;
};

class Variable {
	public:
		typedef boost::intrusive_ptr<VariableImpl> VariableImplPtr;

		Variable(const xmlpp::Element *, const Glib::ustring & n, bool required = true, VariableType def = VariableType());
		Variable(const xmlpp::Element *, const boost::optional<Glib::ustring> &);
		Variable(VariableType def);

		static Variable makeParent(const Glib::ustring & name, bool attr, unsigned int depth);

		operator VariableType () const { return var->value(); }
		VariableType operator()() const { return var->value(); }

	private:
		Variable(VariableImpl *);
		friend class VariableParse;
		VariableImplPtr var;
};

/// Base class for variables whose content is dynamic
class VariableImplDyn : public VariableImpl {
	public:
		VariableImplDyn(const xmlpp::Element * e);
		virtual VariableType value() const = 0;

	protected:
		boost::optional<Variable> defaultValue;
};

/// Base class to create variables
class VariableLoader : public ComponentLoader {
	public:
		virtual VariableImpl * create(const xmlpp::Element *) const = 0;
};
/// Helper implementation of VariableLoader for specific variable types
template <class VarType>
class VariableLoaderImpl : public VariableLoader {
	public:
		virtual VariableImpl * create(const xmlpp::Element * e) const
		{
			return new VarType(e);
		}
};

#endif