File: RootObject.h

package info (click to toggle)
storm-lang 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,004 kB
  • sloc: ansic: 261,462; cpp: 140,405; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (91 lines) | stat: -rw-r--r-- 1,955 bytes parent folder | download | duplicates (3)
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
#pragma once
#include "Runtime.h"
#include "OS/PtrThrowable.h"

namespace storm {
	STORM_PKG(core.lang);

	class Engine;
	class Str;
	class StrBuf;
	struct RootCast;

	/**
	 * The shared parts between Object and TObject. Not exposed to Storm.
	 */
	class RootObject : public os::PtrThrowable {
		STORM_ROOT_CLASS;
	public:
		// Default constructor.
		RootObject();

		// Default copy-ctor.
		RootObject(const RootObject &o);

		// Make sure destructors are virtual.
		virtual ~RootObject();

		// Get the engine somehow.
		inline Engine &engine() const {
			return runtime::allocEngine(this);
		}

		// Get our type somehow.
		inline Type *type() const {
			return runtime::typeOf(this);
		}

		// Used to allow the as<Foo> using our custom (fast) type-checking.
		inline bool isA(const Type *o) const {
			return runtime::isA(this, o);
		}

		// Exception magic. More or less only used to produce nice error messages in some cases.
		virtual const wchar *toCStr() const;

		// To string.
		virtual Str *STORM_FN toS() const;
		virtual void STORM_FN toS(StrBuf *to) const;

		// Custom casting using as<>.
		typedef RootCast DynamicCast;
	};


	// Output an object.
	wostream &operator <<(wostream &to, const RootObject *o);
	wostream &operator <<(wostream &to, const RootObject &o);


	/**
	 * Custom casting.
	 */
	struct RootCast {
		template <class To>
		static To *cast(RootObject *from) {
			if (from == null)
				return null;
			if (from->isA(To::stormType(from->engine())))
				return static_cast<To *>(from);
			return null;
		}

		template <class To>
		static To *cast(const RootObject *from) {
			if (from == null)
				return null;
			if (from->isA(To::stormType(from->engine())))
				return static_cast<To *>(from);
			return null;
		}
	};


	/**
	 * Check if two objects have the same type. Useful in operator ==.
	 */
	inline Bool sameType(const RootObject *a, const RootObject *b) {
		return runtime::typeOf(a) == runtime::typeOf(b);
	}

}