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
# define IMPL_DEFINE(subsystem, factory) std::unique_ptr<ClipboardImpl> factory();
#endif
#ifdef CLIPBOARD_IMPLS_DEFINE
# define IMPL_DEFINE(subsystem, factory) { subsystem, factory },
#endif
#pragma once
@impl_defs@
#undef IMPL_DEFINE
#define CLIPBOARD_IMPLS(X) \
@clipboard_impls@
// last line of the macro, do not remove

View File

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

View File

@@ -203,42 +203,42 @@ subdir('resampler')
subdir('simulation')
subdir('tasks')
simulation_elem_defs = []
element_numbers_x = []
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
elements_conf_data = configuration_data()
elements_conf_data.set('element_defs', '\n'.join(simulation_elem_defs))
configure_file(
input: 'simulation/ElementNumbers.template.h',
output: 'ElementNumbers.h',
configuration: elements_conf_data
configuration: configuration_data({
'element_numbers': '\n\t'.join(element_numbers_x),
}),
)
if platform_clipboard
clipboard_impl_defs = []
clipboard_impls_x = []
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
clipboard_impls_data = configuration_data()
clipboard_impls_data.set('impl_defs', '\n'.join(clipboard_impl_defs))
configure_file(
input: 'common/clipboard/ClipboardImpls.template.h',
output: 'ClipboardImpls.h',
configuration: clipboard_impls_data
configuration: configuration_data({
'clipboard_impls': '\n\t'.join(clipboard_impls_x),
}),
)
endif
simulation_tool_defs = []
tool_numbers_x = []
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
tools_conf_data = configuration_data()
tools_conf_data.set('tool_defs', '\n'.join(simulation_tool_defs))
configure_file(
input: 'simulation/ToolNumbers.template.h',
output: 'ToolNumbers.h',
configuration: tools_conf_data
configuration: configuration_data({
'tool_numbers': '\n\t'.join(tool_numbers_x),
}),
)
configure_file(

View File

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

View File

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

View File

@@ -1,11 +1,11 @@
#pragma once
#include <array>
#include "SimulationData.h"
#include "Element.h"
#define ELEMENT_NUMBERS_ENUMERATE
#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
std::array<Element, PT_NUM> const &GetElements();

View File

@@ -1,13 +1,5 @@
#ifdef ELEMENT_NUMBERS_CALL
# 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
#pragma once
@element_defs@
#undef ELEMENT_DEFINE
#define ELEMENT_NUMBERS(X) \
@element_numbers@
// last line of the macro, do not remove

View File

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

View File

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

View File

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

View File

@@ -1,13 +1,5 @@
#ifdef TOOL_NUMBERS_CALL
# 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
#pragma once
@tool_defs@
#undef TOOL_DEFINE
#define TOOL_NUMBERS(X) \
@tool_numbers@
// last line of the macro, do not remove