aboutsummaryrefslogtreecommitdiffstats
path: root/include/swobject.h
blob: 567405b9b777e71dc44df924cf9d03b32a2e6168 (plain) (blame)
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
#ifndef SWOBJECT_H
#define SWOBJECT_H

#include <utilfuns.h>
#if !defined(__GNUC__) && !defined(_WIN32_WCE)
#else
#include <unixstr.h>
#endif

#include <defs.h>
#include <string.h>

#define SWDYNAMIC_CAST(className, object) (className *)((object)?((object->getClass()->isAssignableFrom(#className))?object:0):0)

/**
* Class used for SWDYNAMIC_CAST to save the inheritance order.
*/
class SWDLLEXPORT SWClass {
private:
	const char **descends;

public:
	SWClass(const char **descends) {
		this->descends = descends;
	}

	bool isAssignableFrom(const char *className) const {
		for (int i = 0; descends[i]; i++) {
			if (!stricmp(descends[i], className))
				return true;
		}
		return false;
	}
};

/** Base class for major Sword classes.
* SWObject is the base class for major Sword classes like SWKey.
* It is used because dynamic_cast is not available on all plattforms supported
* by Sword. Use SWDYNAMIC_CAST(classname, object) instead of dynamic_cast<classname>(object).
*/
class SWObject {
protected:
	SWClass * myclass;
     
public:
	/** Use this to get the class definition and inheritance order.
	* @return The class definition of this object
	*/
	const SWClass *getClass () const {
		return myclass;
	}
};

#endif