Use X macros here and there

Specifically for lists of element and tool numbers, and clipboard implementations. These higher-level macros allow lower-level macros meant to be applied to each item in a list to be defined at the application site of the higher-level macro, slightly increasing readability. See https://en.wikipedia.org/wiki/X_macro
This commit is contained in:
Tamás Bálint Misius
2025-06-05 00:03:17 +02:00
parent ebb3638413
commit e7cc43e689
11 changed files with 53 additions and 70 deletions

View File

@@ -1,10 +1,5 @@
#ifdef CLIPBOARD_IMPLS_DECLARE #pragma once
# define IMPL_DEFINE(subsystem, factory) std::unique_ptr<ClipboardImpl> factory();
#endif
#ifdef CLIPBOARD_IMPLS_DEFINE
# define IMPL_DEFINE(subsystem, factory) { subsystem, factory },
#endif
@impl_defs@ #define CLIPBOARD_IMPLS(X) \
@clipboard_impls@
#undef IMPL_DEFINE // last line of the macro, do not remove

View File

@@ -3,13 +3,14 @@
#include "client/GameSave.h" #include "client/GameSave.h"
#include "prefs/GlobalPrefs.h" #include "prefs/GlobalPrefs.h"
#include "PowderToySDL.h" #include "PowderToySDL.h"
#include "ClipboardImpls.h"
#include <SDL_syswm.h> #include <SDL_syswm.h>
#include <iostream> #include <iostream>
namespace Clipboard namespace Clipboard
{ {
#define CLIPBOARD_IMPLS_DECLARE #define CLIPBOARD_IMPLS_DECLARE(subsystem, factory) std::unique_ptr<ClipboardImpl> factory();
#include "ClipboardImpls.h" CLIPBOARD_IMPLS(CLIPBOARD_IMPLS_DECLARE)
#undef CLIPBOARD_IMPLS_DECLARE #undef CLIPBOARD_IMPLS_DECLARE
struct ClipboardImplEntry struct ClipboardImplEntry
@@ -17,8 +18,8 @@ namespace Clipboard
SDL_SYSWM_TYPE subsystem; SDL_SYSWM_TYPE subsystem;
std::unique_ptr<ClipboardImpl> (*factory)(); std::unique_ptr<ClipboardImpl> (*factory)();
} clipboardImpls[] = { } clipboardImpls[] = {
#define CLIPBOARD_IMPLS_DEFINE #define CLIPBOARD_IMPLS_DEFINE(subsystem, factory) { subsystem, factory },
#include "ClipboardImpls.h" CLIPBOARD_IMPLS(CLIPBOARD_IMPLS_DEFINE)
#undef CLIPBOARD_IMPLS_DEFINE #undef CLIPBOARD_IMPLS_DEFINE
{ SDL_SYSWM_UNKNOWN, nullptr }, { SDL_SYSWM_UNKNOWN, nullptr },
}; };

View File

@@ -203,42 +203,42 @@ subdir('resampler')
subdir('simulation') subdir('simulation')
subdir('tasks') subdir('tasks')
simulation_elem_defs = [] element_numbers_x = []
foreach elem_name_id : simulation_elem_ids foreach elem_name_id : simulation_elem_ids
simulation_elem_defs += 'ELEMENT_DEFINE(' + elem_name_id[0] + ', ' + elem_name_id[1].to_string() + ');' element_numbers_x += 'X(@0@, @1@) \\'.format(elem_name_id[0], elem_name_id[1])
endforeach endforeach
elements_conf_data = configuration_data()
elements_conf_data.set('element_defs', '\n'.join(simulation_elem_defs))
configure_file( configure_file(
input: 'simulation/ElementNumbers.template.h', input: 'simulation/ElementNumbers.template.h',
output: 'ElementNumbers.h', output: 'ElementNumbers.h',
configuration: elements_conf_data configuration: configuration_data({
'element_numbers': '\n\t'.join(element_numbers_x),
}),
) )
if platform_clipboard if platform_clipboard
clipboard_impl_defs = [] clipboard_impls_x = []
foreach impl_subsys_factory : clipboard_impl_factories foreach impl_subsys_factory : clipboard_impl_factories
clipboard_impl_defs += 'IMPL_DEFINE(' + impl_subsys_factory[0] + ', ' + impl_subsys_factory[1] + ')' clipboard_impls_x += 'X(@0@, @1@) \\'.format(impl_subsys_factory[0], impl_subsys_factory[1])
endforeach endforeach
clipboard_impls_data = configuration_data()
clipboard_impls_data.set('impl_defs', '\n'.join(clipboard_impl_defs))
configure_file( configure_file(
input: 'common/clipboard/ClipboardImpls.template.h', input: 'common/clipboard/ClipboardImpls.template.h',
output: 'ClipboardImpls.h', output: 'ClipboardImpls.h',
configuration: clipboard_impls_data configuration: configuration_data({
'clipboard_impls': '\n\t'.join(clipboard_impls_x),
}),
) )
endif endif
simulation_tool_defs = [] tool_numbers_x = []
foreach tool_name_id : simulation_tool_ids foreach tool_name_id : simulation_tool_ids
simulation_tool_defs += 'TOOL_DEFINE(' + tool_name_id[0] + ', ' + tool_name_id[1].to_string() + ');' tool_numbers_x += 'X(@0@, @1@) \\'.format(tool_name_id[0], tool_name_id[1])
endforeach endforeach
tools_conf_data = configuration_data()
tools_conf_data.set('tool_defs', '\n'.join(simulation_tool_defs))
configure_file( configure_file(
input: 'simulation/ToolNumbers.template.h', input: 'simulation/ToolNumbers.template.h',
output: 'ToolNumbers.h', output: 'ToolNumbers.h',
configuration: tools_conf_data configuration: configuration_data({
'tool_numbers': '\n\t'.join(tool_numbers_x),
}),
) )
configure_file( configure_file(

View File

@@ -1,10 +1,11 @@
#pragma once #pragma once
#include <memory>
#include "common/Vec2.h" #include "common/Vec2.h"
#include "graphics/Pixel.h" #include "graphics/Pixel.h"
#include "ElementDefs.h" #include "ElementDefs.h"
#include "Particle.h" #include "Particle.h"
#include "StructProperty.h" #include "StructProperty.h"
#include "ElementNumbers.h"
#include <memory>
class Simulation; class Simulation;
class Renderer; class Renderer;
@@ -77,7 +78,7 @@ public:
by higher-level processes referring to them by name such as Lua or the property tool **/ by higher-level processes referring to them by name such as Lua or the property tool **/
static std::vector<StructProperty> const &GetProperties(); static std::vector<StructProperty> const &GetProperties();
#define ELEMENT_NUMBERS_DECLARE #define ELEMENT_NUMBERS_DECLARE(name, id) void Element_ ## name ();
#include "ElementNumbers.h" ELEMENT_NUMBERS(ELEMENT_NUMBERS_DECLARE)
#undef ELEMENT_NUMBERS_DECLARE #undef ELEMENT_NUMBERS_DECLARE
}; };

View File

@@ -1,4 +1,5 @@
#include "ElementCommon.h" #include "ElementCommon.h"
#include "ElementNumbers.h"
std::array<Element, PT_NUM> const &GetElements() std::array<Element, PT_NUM> const &GetElements()
{ {
@@ -8,8 +9,8 @@ std::array<Element, PT_NUM> const &GetElements()
DoOnce() DoOnce()
{ {
#define ELEMENT_NUMBERS_CALL #define ELEMENT_NUMBERS_CALL(name, id) elements[id].Element_ ## name ();
#include "ElementNumbers.h" ELEMENT_NUMBERS(ELEMENT_NUMBERS_CALL)
#undef ELEMENT_NUMBERS_CALL #undef ELEMENT_NUMBERS_CALL
} }
}; };

View File

@@ -1,11 +1,11 @@
#pragma once #pragma once
#include <array>
#include "SimulationData.h" #include "SimulationData.h"
#include "Element.h" #include "Element.h"
#define ELEMENT_NUMBERS_ENUMERATE
#include "ElementNumbers.h" #include "ElementNumbers.h"
#include <array>
#define ELEMENT_NUMBERS_ENUMERATE(name, id) constexpr int PT_ ## name = id;
ELEMENT_NUMBERS(ELEMENT_NUMBERS_ENUMERATE);
#undef ELEMENT_NUMBERS_ENUMERATE #undef ELEMENT_NUMBERS_ENUMERATE
std::array<Element, PT_NUM> const &GetElements(); std::array<Element, PT_NUM> const &GetElements();

View File

@@ -1,13 +1,5 @@
#ifdef ELEMENT_NUMBERS_CALL #pragma once
# define ELEMENT_DEFINE(name, id) elements[id].Element_ ## name ()
#endif
#ifdef ELEMENT_NUMBERS_DECLARE
# define ELEMENT_DEFINE(name, id) void Element_ ## name ()
#endif
#ifdef ELEMENT_NUMBERS_ENUMERATE
# define ELEMENT_DEFINE(name, id) constexpr int PT_ ## name = id
#endif
@element_defs@ #define ELEMENT_NUMBERS(X) \
@element_numbers@
#undef ELEMENT_DEFINE // last line of the macro, do not remove

View File

@@ -2,6 +2,7 @@
#include "gui/game/tool/Tool.h" #include "gui/game/tool/Tool.h"
#include "common/String.h" #include "common/String.h"
#include "graphics/Pixel.h" #include "graphics/Pixel.h"
#include "ToolNumbers.h"
class Simulation; class Simulation;
struct Particle; struct Particle;
@@ -17,9 +18,8 @@ public:
void (*PerformDrawFill)(SimTool *tool, Simulation *sim, const Brush &brush, ui::Point position); void (*PerformDrawFill)(SimTool *tool, Simulation *sim, const Brush &brush, ui::Point position);
void (*PerformSelect )(SimTool *tool, int toolSelection); void (*PerformSelect )(SimTool *tool, int toolSelection);
#define TOOL_NUMBERS_DECLARE(name, id) void Tool_ ## name ();
#define TOOL_NUMBERS_DECLARE TOOL_NUMBERS(TOOL_NUMBERS_DECLARE)
#include "ToolNumbers.h"
#undef TOOL_NUMBERS_DECLARE #undef TOOL_NUMBERS_DECLARE
SimTool(); SimTool();

View File

@@ -1,4 +1,5 @@
#include "ToolClasses.h" #include "ToolClasses.h"
#include "ToolNumbers.h"
std::vector<SimTool> const &GetTools() std::vector<SimTool> const &GetTools()
{ {
@@ -8,8 +9,8 @@ std::vector<SimTool> const &GetTools()
DoOnce() DoOnce()
{ {
#define TOOL_NUMBERS_CALL #define TOOL_NUMBERS_CALL(name, id) tools.push_back(SimTool()), tools.back().Tool_ ## name ();
#include "ToolNumbers.h" TOOL_NUMBERS(TOOL_NUMBERS_CALL)
#undef TOOL_NUMBERS_CALL #undef TOOL_NUMBERS_CALL
} }
}; };

View File

@@ -1,11 +1,11 @@
#pragma once #pragma once
#include "SimTool.h"
#include "ToolNumbers.h"
#include <vector> #include <vector>
#include <memory> #include <memory>
#include "SimTool.h" #define TOOL_NUMBERS_ENUMERATE(name, id) constexpr int TOOL_ ## name = id;
TOOL_NUMBERS(TOOL_NUMBERS_ENUMERATE)
#define TOOL_NUMBERS_ENUMERATE
#include "ToolNumbers.h"
#undef TOOL_NUMBERS_ENUMERATE #undef TOOL_NUMBERS_ENUMERATE
std::vector<SimTool> const &GetTools(); std::vector<SimTool> const &GetTools();

View File

@@ -1,13 +1,5 @@
#ifdef TOOL_NUMBERS_CALL #pragma once
# define TOOL_DEFINE(name, id) tools.push_back(SimTool()), tools.back().Tool_ ## name ()
#endif
#ifdef TOOL_NUMBERS_DECLARE
# define TOOL_DEFINE(name, id) void Tool_ ## name ()
#endif
#ifdef TOOL_NUMBERS_ENUMERATE
# define TOOL_DEFINE(name, id) constexpr int TOOL_ ## name = id
#endif
@tool_defs@ #define TOOL_NUMBERS(X) \
@tool_numbers@
#undef TOOL_DEFINE // last line of the macro, do not remove