Use weak symbol to allow not to defining display
Diffstat:
8 files changed, 27 insertions(+), 13 deletions(-)
@@ -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
@@ -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); }
@@ -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;
@@ -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);
@@ -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
@@ -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());
@@ -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;
}
@@ -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); }