Ver código fonte

Updated the new module changes to use more compile/link-time checking rather than runtime checking.

Alex Szpakowski 11 anos atrás
pai
commit
d52bab4221

+ 9 - 17
src/common/Module.cpp

@@ -60,11 +60,6 @@ namespace love
 
 Module *Module::instances[] = {};
 
-Module::Module()
-	: moduleType(M_INVALID)
-{
-}
-
 Module::~Module()
 {
 	ModuleRegistry &registry = registryInstance();
@@ -79,15 +74,14 @@ Module::~Module()
 		}
 	}
 
-	freeEmptyRegistry();
-
-	if (instances[moduleType] == this)
-		instances[moduleType] = nullptr;
-}
+	// Same deal with Module::getModuleType().
+	for (int i = 0; i < (int) M_MAX_ENUM; i++)
+	{
+		if (instances[i] == this)
+			instances[i] = nullptr;
+	}
 
-Module::ModuleType Module::getModuleType() const
-{
-	return moduleType;
+	freeEmptyRegistry();
 }
 
 void Module::registerInstance(Module *instance)
@@ -108,12 +102,10 @@ void Module::registerInstance(Module *instance)
 		throw Exception("Module %s already registered!", instance->getName());
 	}
 
-	ModuleType moduletype = instance->getModuleType();
-	if (moduletype == M_INVALID)
-		throw love::Exception("Module %s has an invalid base module type.", instance->getName());
-
 	registry.insert(make_pair(name, instance));
 
+	ModuleType moduletype = instance->getModuleType();
+
 	if (instances[moduletype] != nullptr)
 	{
 		printf("Warning: overwriting module instance %s with new instance %s\n",

+ 4 - 7
src/common/Module.h

@@ -36,7 +36,6 @@ public:
 
 	enum ModuleType
 	{
-		M_INVALID = 0,
 		M_AUDIO,
 		M_EVENT,
 		M_FILESYSTEM,
@@ -56,10 +55,12 @@ public:
 		M_MAX_ENUM
 	};
 
-	Module();
 	virtual ~Module();
 
-	ModuleType getModuleType() const;
+    /**
+     * Gets the base type of the module.
+     **/
+	virtual ModuleType getModuleType() const = 0;
 
 	/**
 	 * Gets the name of the module. This is used in case of errors
@@ -95,10 +96,6 @@ public:
 		return (T *) instances[type];
 	}
 
-protected:
-
-	ModuleType moduleType;
-
 private:
 
 	static Module *instances[M_MAX_ENUM];

+ 0 - 5
src/modules/audio/Audio.cpp

@@ -25,11 +25,6 @@ namespace love
 namespace audio
 {
 
-Audio::Audio()
-{
-	moduleType = M_AUDIO;
-}
-
 StringMap<Audio::DistanceModel, Audio::DISTANCE_MAX_ENUM>::Entry Audio::distanceModelEntries[] =
 {
 	{"none", Audio::DISTANCE_NONE},

+ 3 - 1
src/modules/audio/Audio.h

@@ -64,9 +64,11 @@ public:
 	static bool getConstant(const char *in, DistanceModel &out);
 	static bool getConstant(DistanceModel in, const char  *&out);
 
-	Audio();
 	virtual ~Audio() {}
 
+	// Implements Module.
+	virtual ModuleType getModuleType() const { return M_AUDIO; }
+
 	virtual Source *newSource(love::sound::Decoder *decoder) = 0;
 	virtual Source *newSource(love::sound::SoundData *soundData) = 0;
 

+ 0 - 1
src/modules/event/Event.cpp

@@ -82,7 +82,6 @@ Message *Message::fromLua(lua_State *L, int n)
 
 Event::Event()
 {
-	moduleType = M_EVENT;
 	mutex = thread::newMutex();
 }
 

+ 3 - 0
src/modules/event/Event.h

@@ -58,6 +58,9 @@ public:
 	Event();
 	virtual ~Event();
 
+	// Implements Module.
+	virtual ModuleType getModuleType() const { return M_EVENT; }
+
 	void push(Message *msg);
 	bool poll(Message *&msg);
 	virtual void clear();

+ 0 - 1
src/modules/filesystem/physfs/Filesystem.cpp

@@ -77,7 +77,6 @@ Filesystem::Filesystem()
 	, fused(false)
 	, fusedSet(false)
 {
-	moduleType = M_FILESYSTEM;
 }
 
 Filesystem::~Filesystem()

+ 2 - 0
src/modules/filesystem/physfs/Filesystem.h

@@ -80,6 +80,8 @@ public:
 	Filesystem();
 	virtual ~Filesystem();
 
+	// Implements Module.
+	virtual ModuleType getModuleType() const { return M_FILESYSTEM; }
 	const char *getName() const;
 
 	void init(const char *arg0);

+ 3 - 1
src/modules/font/Font.h

@@ -40,9 +40,11 @@ class Font : public Module
 
 public:
 
-	Font() { moduleType = M_FONT; }
 	virtual ~Font() {}
 
+	// Implements Module.
+	virtual ModuleType getModuleType() const { return M_FONT; }
+
 	virtual Rasterizer *newRasterizer(Data *data, int size) = 0;
 	virtual Rasterizer *newRasterizer(love::image::ImageData *data, const std::string &glyphs) = 0;
 	virtual Rasterizer *newRasterizer(love::image::ImageData *data, uint32 *glyphs, int length) = 0;

+ 0 - 5
src/modules/graphics/Graphics.cpp

@@ -25,11 +25,6 @@ namespace love
 namespace graphics
 {
 
-Graphics::Graphics()
-{
-	moduleType = M_GRAPHICS;
-}
-
 Graphics::~Graphics()
 {
 }

+ 3 - 1
src/modules/graphics/Graphics.h

@@ -120,9 +120,11 @@ public:
 		std::string device;
 	};
 
-	Graphics();
 	virtual ~Graphics();
 
+	// Implements Module.
+	virtual ModuleType getModuleType() const { return M_GRAPHICS; }
+
 	/**
 	 * Sets the current graphics display viewport dimensions.
 	 **/

+ 3 - 1
src/modules/image/Image.h

@@ -44,9 +44,11 @@ class Image : public Module
 {
 public:
 
-	Image() { moduleType = M_IMAGE; }
 	virtual ~Image() {}
 
+	// Implements Module.
+	virtual ModuleType getModuleType() const { return M_IMAGE; }
+
 	/**
 	 * Creates new ImageData from FileData.
 	 * @param data The FileData containing the encoded image data.

+ 3 - 1
src/modules/joystick/JoystickModule.h

@@ -34,9 +34,11 @@ class JoystickModule : public Module
 {
 public:
 
-	JoystickModule() { moduleType = M_JOYSTICK; }
 	virtual ~JoystickModule() {}
 
+	// Implements Module.
+	virtual ModuleType getModuleType() const { return M_JOYSTICK; }
+
 	/**
 	 * Adds a connected Joystick device and opens it for use.
 	 * Returns NULL if the Joystick could not be added.

+ 0 - 5
src/modules/keyboard/Keyboard.cpp

@@ -27,11 +27,6 @@ namespace love
 namespace keyboard
 {
 
-Keyboard::Keyboard()
-{
-	moduleType = M_KEYBOARD;
-}
-
 bool Keyboard::getConstant(const char *in, Keyboard::Key &out)
 {
 	return keys.find(in, out);

+ 3 - 1
src/modules/keyboard/Keyboard.h

@@ -244,9 +244,11 @@ public:
 		KEY_MAX_ENUM = 512
 	};
 
-	Keyboard();
 	virtual ~Keyboard() {}
 
+	// Implements Module.
+	virtual ModuleType getModuleType() const { return M_KEYBOARD; }
+
 	/**
 	 * Sets whether repeat keypress events should be sent if a key is held down.
 	 * Does not affect text input events.

+ 0 - 2
src/modules/math/MathModule.cpp

@@ -87,8 +87,6 @@ Math Math::instance;
 Math::Math()
 	: rng()
 {
-	moduleType = M_MATH;
-
 	// prevent the runtime from free()-ing this
 	retain();
 }

+ 6 - 0
src/modules/math/MathModule.h

@@ -115,6 +115,12 @@ public:
 	 **/
 	BezierCurve *newBezierCurve(const std::vector<Vector> &points);
 
+	// Implements Module.
+	virtual ModuleType getModuleType() const
+	{
+		return M_MATH;
+	}
+
 	virtual const char *getName() const
 	{
 		return "love.math";

+ 0 - 5
src/modules/mouse/Mouse.cpp

@@ -25,11 +25,6 @@ namespace love
 namespace mouse
 {
 
-Mouse::Mouse()
-{
-	moduleType = M_MOUSE;
-}
-
 bool Mouse::getConstant(const char *in, Button &out)
 {
 	return buttons.find(in, out);

+ 3 - 1
src/modules/mouse/Mouse.h

@@ -49,9 +49,11 @@ public:
 		BUTTON_MAX_ENUM
 	};
 
-	Mouse();
 	virtual ~Mouse() {}
 
+	// Implements Module.
+	virtual ModuleType getModuleType() const { return M_MOUSE; }
+
 	virtual Cursor *newCursor(love::image::ImageData *data, int hotx, int hoty) = 0;
 	virtual Cursor *getSystemCursor(Cursor::SystemCursor cursortype) = 0;
 

+ 0 - 5
src/modules/physics/box2d/Physics.cpp

@@ -33,11 +33,6 @@ namespace box2d
 
 int Physics::meter = Physics::DEFAULT_METER;
 
-Physics::Physics()
-{
-	moduleType = M_PHYSICS;
-}
-
 const char *Physics::getName() const
 {
 	return "love.physics.box2d";

+ 1 - 2
src/modules/physics/box2d/Physics.h

@@ -61,10 +61,9 @@ public:
 	 **/
 	static const int DEFAULT_METER = 30;
 
-	Physics();
-
 	// Implements Module.
 	const char *getName() const;
+	virtual ModuleType getModuleType() const { return M_PHYSICS; }
 
 	/**
 	 * Creates a new World.

+ 0 - 5
src/modules/sound/Sound.cpp

@@ -25,11 +25,6 @@ namespace love
 namespace sound
 {
 
-Sound::Sound()
-{
-	moduleType = M_SOUND;
-}
-
 Sound::~Sound()
 {
 }

+ 3 - 1
src/modules/sound/Sound.h

@@ -42,9 +42,11 @@ class Sound : public Module
 
 public:
 
-	Sound();
 	virtual ~Sound();
 
+	// Implements Module.
+	virtual ModuleType getModuleType() const { return M_SOUND; }
+
 	/**
 	 * Creates new SoundData from a decoder. Fully expands the
 	 * encoded sound data into raw sound data. Not recommended

+ 0 - 5
src/modules/system/System.cpp

@@ -41,11 +41,6 @@ namespace love
 namespace system
 {
 
-System::System()
-{
-	moduleType = M_SYSTEM;
-}
-
 std::string System::getOS() const
 {
 #if defined(LOVE_MACOSX)

+ 3 - 1
src/modules/system/System.h

@@ -48,9 +48,11 @@ public:
 		POWER_MAX_ENUM
 	};
 
-	System();
 	virtual ~System() {}
 
+	// Implements Module.
+	virtual ModuleType getModuleType() const { return M_SYSTEM; }
+
 	/**
 	 * Gets the current operating system.
 	 **/

+ 0 - 5
src/modules/thread/ThreadModule.cpp

@@ -25,11 +25,6 @@ namespace love
 namespace thread
 {
 
-ThreadModule::ThreadModule()
-{
-	moduleType = M_THREAD;
-}
-
 LuaThread *ThreadModule::newThread(const std::string &name, love::Data *data)
 {
 	LuaThread *lt = new LuaThread(name, data);

+ 1 - 1
src/modules/thread/ThreadModule.h

@@ -42,7 +42,6 @@ class ThreadModule : public love::Module
 {
 public:
 
-	ThreadModule();
 	virtual ~ThreadModule() {}
 	virtual LuaThread *newThread(const std::string &name, love::Data *data);
 	virtual Channel *newChannel();
@@ -50,6 +49,7 @@ public:
 
 	// Implements Module.
 	virtual const char *getName() const;
+	virtual ModuleType getModuleType() const { return M_THREAD; }
 
 }; // ThreadModule
 

+ 3 - 1
src/modules/timer/Timer.h

@@ -33,9 +33,11 @@ class Timer : public Module
 {
 public:
 
-	Timer() { moduleType = M_TIMER; }
 	virtual ~Timer() {}
 
+	// Implements Module.
+	virtual ModuleType getModuleType() const { return M_TIMER; }
+
 	/**
 	 * Measures the time between this call and the previous call,
 	 * and updates internal values accordinly.

+ 0 - 5
src/modules/window/Window.cpp

@@ -28,11 +28,6 @@ namespace window
 
 Window *Window::singleton = nullptr;
 
-Window::Window()
-{
-	moduleType = M_WINDOW;
-}
-
 Window::~Window()
 {
 	if (singleton == this)

+ 3 - 1
src/modules/window/Window.h

@@ -75,9 +75,11 @@ public:
 		int height;
 	};
 
-	Window();
 	virtual ~Window();
 
+	// Implements Module.
+	virtual ModuleType getModuleType() const { return M_WINDOW; }
+
 	virtual bool setWindow(int width = 800, int height = 600, WindowSettings *settings = nullptr) = 0;
 	virtual void getWindow(int &width, int &height, WindowSettings &settings) = 0;