1 /**
2  * Copyright (c) 2006-2019 LOVE Development Team
3  *
4  * This software is provided 'as-is', without any express or implied
5  * warranty.  In no event will the authors be held liable for any damages
6  * arising from the use of this software.
7  *
8  * Permission is granted to anyone to use this software for any purpose,
9  * including commercial applications, and to alter it and redistribute it
10  * freely, subject to the following restrictions:
11  *
12  * 1. The origin of this software must not be misrepresented; you must not
13  *    claim that you wrote the original software. If you use this software
14  *    in a product, an acknowledgment in the product documentation would be
15  *    appreciated but is not required.
16  * 2. Altered source versions must be plainly marked as such, and must not be
17  *    misrepresented as being the original software.
18  * 3. This notice may not be removed or altered from any source distribution.
19  **/
20 
21 #ifndef LOVE_MODULE_H
22 #define LOVE_MODULE_H
23 
24 // LOVE
25 #include "Exception.h"
26 #include "Object.h"
27 
28 namespace love
29 {
30 /**
31  * Abstract superclass for all modules.
32  **/
33 class Module : public Object
34 {
35 public:
36 
37 	static love::Type type;
38 
39 	enum ModuleType
40 	{
41 		M_AUDIO,
42 		M_DATA,
43 		M_EVENT,
44 		M_FILESYSTEM,
45 		M_FONT,
46 		M_GRAPHICS,
47 		M_IMAGE,
48 		M_JOYSTICK,
49 		M_KEYBOARD,
50 		M_MATH,
51 		M_MOUSE,
52 		M_PHYSICS,
53 		M_SOUND,
54 		M_SYSTEM,
55 		M_THREAD,
56 		M_TIMER,
57 		M_TOUCH,
58 		M_VIDEO,
59 		M_WINDOW,
60 		M_MAX_ENUM
61 	};
62 
63 	Module();
64 	virtual ~Module();
65 
66     /**
67      * Gets the base type of the module.
68      **/
69 	virtual ModuleType getModuleType() const = 0;
70 
71 	/**
72 	 * Gets the name of the module. This is used in case of errors
73 	 * and other messages.
74 	 *
75 	 * @return The full name of the module, eg. love.graphics.opengl.
76 	 **/
77 	virtual const char *getName() const = 0;
78 
79 	/**
80 	 * Add module to internal registry. To be used /only/ in
81 	 * runtime.cpp:luax_register_module()
82 	 * @param instance The module instance.
83 	 */
84 	static void registerInstance(Module *instance);
85 
86 	/**
87 	 * Retrieve module instance from internal registry. May return NULL
88 	 * if module not registered.
89 	 * @param name The full name of the module.
90 	 * @return Module instance or NULL if the module is not registered.
91 	 */
92 	static Module *getInstance(const std::string &name);
93 
94 	/**
95 	 * Retrieve module instance from the internal registry using the base
96 	 * module type. May return null if the module is not registered.
97 	 * @param type The base type of the module.
98 	 **/
99 	template <typename T>
getInstance(ModuleType type)100 	static T *getInstance(ModuleType type)
101 	{
102 		return (T *) instances[type];
103 	}
104 
105 private:
106 
107 	static Module *instances[M_MAX_ENUM];
108 
109 }; // Module
110 
111 } // love
112 
113 #endif // LOVE_MODULE_H
114