mirror of
https://github.com/love2d/love.git
synced 2026-08-12 08:30:56 +02:00
Updated the new module changes to use more compile/link-time checking rather than runtime checking.
This commit is contained in:
+9
-17
@@ -60,11 +60,6 @@ namespace love
|
||||
|
||||
Module *Module::instances[] = {};
|
||||
|
||||
Module::Module()
|
||||
: moduleType(M_INVALID)
|
||||
{
|
||||
}
|
||||
|
||||
Module::~Module()
|
||||
{
|
||||
ModuleRegistry ®istry = registryInstance();
|
||||
@@ -79,15 +74,14 @@ Module::~Module()
|
||||
}
|
||||
}
|
||||
|
||||
// Same deal with Module::getModuleType().
|
||||
for (int i = 0; i < (int) M_MAX_ENUM; i++)
|
||||
{
|
||||
if (instances[i] == this)
|
||||
instances[i] = nullptr;
|
||||
}
|
||||
|
||||
freeEmptyRegistry();
|
||||
|
||||
if (instances[moduleType] == this)
|
||||
instances[moduleType] = nullptr;
|
||||
}
|
||||
|
||||
Module::ModuleType Module::getModuleType() const
|
||||
{
|
||||
return moduleType;
|
||||
}
|
||||
|
||||
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
@@ -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];
|
||||
|
||||
@@ -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},
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -82,7 +82,6 @@ Message *Message::fromLua(lua_State *L, int n)
|
||||
|
||||
Event::Event()
|
||||
{
|
||||
moduleType = M_EVENT;
|
||||
mutex = thread::newMutex();
|
||||
}
|
||||
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -77,7 +77,6 @@ Filesystem::Filesystem()
|
||||
, fused(false)
|
||||
, fusedSet(false)
|
||||
{
|
||||
moduleType = M_FILESYSTEM;
|
||||
}
|
||||
|
||||
Filesystem::~Filesystem()
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -25,11 +25,6 @@ namespace love
|
||||
namespace graphics
|
||||
{
|
||||
|
||||
Graphics::Graphics()
|
||||
{
|
||||
moduleType = M_GRAPHICS;
|
||||
}
|
||||
|
||||
Graphics::~Graphics()
|
||||
{
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
**/
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -87,8 +87,6 @@ Math Math::instance;
|
||||
Math::Math()
|
||||
: rng()
|
||||
{
|
||||
moduleType = M_MATH;
|
||||
|
||||
// prevent the runtime from free()-ing this
|
||||
retain();
|
||||
}
|
||||
|
||||
@@ -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";
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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";
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -25,11 +25,6 @@ namespace love
|
||||
namespace sound
|
||||
{
|
||||
|
||||
Sound::Sound()
|
||||
{
|
||||
moduleType = M_SOUND;
|
||||
}
|
||||
|
||||
Sound::~Sound()
|
||||
{
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -41,11 +41,6 @@ namespace love
|
||||
namespace system
|
||||
{
|
||||
|
||||
System::System()
|
||||
{
|
||||
moduleType = M_SYSTEM;
|
||||
}
|
||||
|
||||
std::string System::getOS() const
|
||||
{
|
||||
#if defined(LOVE_MACOSX)
|
||||
|
||||
@@ -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.
|
||||
**/
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -28,11 +28,6 @@ namespace window
|
||||
|
||||
Window *Window::singleton = nullptr;
|
||||
|
||||
Window::Window()
|
||||
{
|
||||
moduleType = M_WINDOW;
|
||||
}
|
||||
|
||||
Window::~Window()
|
||||
{
|
||||
if (singleton == this)
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user