stamen

Stamen - static menu generator
git clone git://git.dimitrijedobrota.com/stamen.git
Log | Files | Refs | README | LICENSE

commit 6f2789a11b79acf217c112117a6969a0ec5e4a57
parent 972666d81653fd991e17ca3971c6cf61d3efa7d4
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Thu, 13 Jun 2024 23:18:14 +0200

Use weak symbol to allow not to defining display

Diffstat:
Mdemo/CMakeLists.txt | 6++++--
Mdemo/dynamic.cpp | 5+++--
Mdemo/main.cpp | 13+++++++++++--
Minclude/stamen.h | 3++-
Minclude/stamen.hpp | 5++---
Msrc/generate.cpp | 2+-
Msrc/menu.cpp | 2+-
Msrc/stamen.cpp | 4+++-
8 files changed, 27 insertions(+), 13 deletions(-)

diff --git a/demo/CMakeLists.txt b/demo/CMakeLists.txt @@ -10,14 +10,16 @@ add_custom_command( ) add_executable(demo main.cpp ${GENERATE_OUT}/demo_menu.cpp) -target_link_libraries(demo stamen) +# target_link_libraries(demo stamen) - no need to link +target_include_directories(demo PUBLIC ../include) set_target_properties(demo PROPERTIES LINKER_LANGUAGE CXX) target_include_directories(demo PRIVATE ${GENERATE_OUT} ${CMAKE_CURRENT_SOURCE_DIR}) set_target_properties(demo PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION ${PROJECT_VERSION_MAJOR} - RUNTIME_OUTPUT_DIRECTORY "${GENERATE_OUT}") + RUNTIME_OUTPUT_DIRECTORY "${GENERATE_OUT}" +) add_custom_command( OUTPUT ${GENERATE_OUT}/demo_menu.h ${GENERATE_OUT}/demo_menu.c diff --git a/demo/dynamic.cpp b/demo/dynamic.cpp @@ -1,8 +1,9 @@ #include "stamen.hpp" #include <iostream> -// need to link against stamen library in order to use stamen::builtin_display -const stamen::display_f stamen::stamen_display = stamen::builtin_display; +// still need to link against stamen library +// as builtin_display is being used +// because there is no override int finish(int) { exit(1); } diff --git a/demo/main.cpp b/demo/main.cpp @@ -3,8 +3,17 @@ #include <iostream> -// need to link against stamen library in order to use stamen::builtin_display -const stamen::display_f stamen::stamen_display = stamen::builtin_display; +int test_display(const char *title, const stamen::item_t itemv[], int size) { + for (auto i = 0ul; i < size; i++) { + std::cout << i + 1 << ": " << itemv[i].prompt << '\n'; + } + std::cout << "Auto calling option 1...\n"; + itemv[1].callback(1); + return 0; +} + +// no need to link against stamen library as custom display is provided +const stamen::display_f stamen::stamen_display = test_display; int operation1(int) { std::cout << "operation 1" << std::endl; diff --git a/include/stamen.h b/include/stamen.h @@ -19,9 +19,10 @@ extern const stamen_display_f stamen_display; #if !defined __cplusplus || defined WITH_C_BINDINGS -int stamen_dynamic(const char *code); void stamen_read(const char *filename); void stamen_insert(const char *code, stamen_callback_f callback); + +int stamen_dynamic(const char *code); int stamen_builtin_display(const char *title, const stamen_item_t itemv[], int size); diff --git a/include/stamen.hpp b/include/stamen.hpp @@ -9,11 +9,10 @@ using callback_f = stamen_callback_f; using display_f = stamen_display_f; using item_t = stamen_item_t; -extern const display_f &display; - -int dynamic(const char *code); void read(const char *filename); void insert(const char *code, callback_f callback); + +int dynamic(const char *code); int builtin_display(const char *title, const item_t itemv[], int size); } // namespace stamen diff --git a/src/generate.cpp b/src/generate.cpp @@ -44,7 +44,7 @@ class Generator { } os << "\t};\n"; - if (cpp) os << "\treturn stamen::display"; + if (cpp) os << "\treturn stamen::stamen_display"; else os << "\treturn stamen_display"; os << std::format("(\"{}\"", menu.getTitle()); diff --git a/src/menu.cpp b/src/menu.cpp @@ -61,7 +61,7 @@ int Menu::display_stub(int idx) { if (ml_it != menu_lookup.end()) { const Menu &menu = ml_it->second; st.push_back(&menu); - int ret = display(menu.title.c_str(), menu.getItemv(), menu.getSize()); + int ret = stamen_display(menu.title.c_str(), menu.getItemv(), menu.getSize()); st.pop_back(); return ret; } diff --git a/src/stamen.cpp b/src/stamen.cpp @@ -7,9 +7,11 @@ #include <ostream> #include <variant> + namespace stamen { -const display_f &display = stamen_display; +__attribute__((weak)) +extern const stamen_display_f stamen_display = stamen_builtin_display; int dynamic(const char *code) { return Menu::dynamic(code); } void read(const char *filename) { Menu::read(filename); }