Skip to content

Commit b073957

Browse files
committed
lib_common: new class for module visibility
Add MODULE_FLAG_ALIAS (+ complete API) for modules that are not hidden as technical/deprecated moudles but rather an alias. The difference is important for GUI not to print aliased module twice.
1 parent 7ac13a3 commit b073957

File tree

2 files changed

+51
-22
lines changed

2 files changed

+51
-22
lines changed

src/lib_common.cpp

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @author Martin Pulec <[email protected]>
44
*/
55
/*
6-
* Copyright (c) 2012-2023 CESNET, z. s. p. o.
6+
* Copyright (c) 2012-2025 CESNET
77
* All rights reserved.
88
*
99
* Redistribution and use in source and binary forms, with or without
@@ -215,7 +215,7 @@ void open_all(const char *pattern, list<void *> &libs) {
215215
struct lib_info {
216216
const void *data;
217217
int abi_version;
218-
bool hidden;
218+
unsigned visibility_flag;
219219
};
220220

221221
// http://stackoverflow.com/questions/1801892/making-mapfind-operation-case-insensitive
@@ -247,13 +247,22 @@ static auto& get_libmap(){
247247
return libraries;
248248
}
249249

250-
void register_library(const char *name, const void *data, enum library_class cls, int abi_version, int hidden)
250+
/**
251+
* @param name module name (used in listing and on cmdline for spec)
252+
* @param cls module class
253+
* @param name class-specific metadata structure (with callbacks)
254+
* @param abi_version class-specific ABI version
255+
* @param visibility_flag usually 0 or flag from @ref enum module_flag
256+
*/
257+
void
258+
register_library(const char *name, const void *info, enum library_class cls,
259+
int abi_version, unsigned visibility_flag)
251260
{
252261
auto& map = get_libmap()[cls];
253262
if (map.find(name) != map.end()) {
254263
LOG(LOG_LEVEL_ERROR) << "Module \"" << name << "\" (class " << cls << ") multiple initialization!\n";
255264
}
256-
map[name] = {data, abi_version, static_cast<bool>(hidden)};
265+
map[name] = {info, abi_version, visibility_flag};
257266
}
258267

259268
const void *load_library(const char *name, enum library_class cls, int abi_version)
@@ -293,10 +302,12 @@ const void *load_library(const char *name, enum library_class cls, int abi_versi
293302

294303
/**
295304
* Prints list of modules of given class
296-
* @param full include hidden modules
305+
* @param full include hidden modules and aliases
297306
*/
298307
void list_modules(enum library_class cls, int abi_version, bool full) {
299-
const auto & class_set = get_libraries_for_class(cls, abi_version, full);
308+
const auto &class_set = get_libraries_for_class(
309+
cls, abi_version,
310+
full ? MODULE_SHOW_ALL : MODULE_SHOW_VISIBLE_ONLY);
300311
for (auto && item : class_set) {
301312
col() << "\t" << SBOLD(item.first.c_str()) << "\n";
302313
}
@@ -333,15 +344,19 @@ bool list_all_modules() {
333344
return ret;
334345
}
335346

336-
map<string, const void *> get_libraries_for_class(enum library_class cls, int abi_version, bool include_hidden)
347+
map<string, const void *>
348+
get_libraries_for_class(enum library_class cls, int abi_version,
349+
unsigned include_flags)
337350
{
338351
map<string, const void *> ret;
339352
auto& libraries = get_libmap();
340353
auto it = libraries.find(cls);
341354
if (it != libraries.end()) {
342355
for (auto && item : it->second) {
343356
if (abi_version == item.second.abi_version) {
344-
if (include_hidden || !item.second.hidden) {
357+
if (item.second.visibility_flag == 0 ||
358+
(include_flags &
359+
item.second.visibility_flag) != 0U) {
345360
ret[item.first] = item.second.data;
346361
}
347362
} else {
@@ -354,4 +369,3 @@ map<string, const void *> get_libraries_for_class(enum library_class cls, int ab
354369

355370
return ret;
356371
}
357-

src/lib_common.h

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @author Martin Pulec <[email protected]>
44
*/
55
/*
6-
* Copyright (c) 2011-2023 CESNET, z. s. p. o.
6+
* Copyright (c) 2011-2025 CESNET
77
* All rights reserved.
88
*
99
* Redistribution and use in source and binary forms, with or without
@@ -85,45 +85,59 @@ enum library_class {
8585
LIBRARY_CLASS_AUDIO_FILTER,
8686
};
8787
const void *load_library(const char *name, enum library_class, int abi_version);
88-
void register_library(const char *name, const void *info, enum library_class, int abi_version, int hidden);
88+
void register_library(const char *name, const void *info, enum library_class,
89+
int abi_version, unsigned visibility_flag);
8990
void list_modules(enum library_class, int abi_version, bool full);
9091
bool list_all_modules();
9192
#ifdef __cplusplus
9293
}
9394
#endif
9495

96+
enum module_flag {
97+
MODULE_SHOW_VISIBLE_ONLY = 0, ///< display only modules w/o flag
98+
MODULE_FLAG_HIDDEN = 1 << 0, ///< flag - do not show in listing
99+
MODULE_FLAG_ALIAS =
100+
1 << 1, ///< ditto + hide for GUI, for explicit init only
101+
MODULE_SHOW_ALL =
102+
MODULE_FLAG_HIDDEN | MODULE_FLAG_ALIAS, ///< display all modules
103+
};
104+
95105
#ifdef __cplusplus
96106
#include <map>
97107
#include <string>
98-
std::map<std::string, const void *> get_libraries_for_class(enum library_class cls, int abi_version, bool include_hidden = true);
108+
std::map<std::string, const void *>
109+
get_libraries_for_class(enum library_class cls, int abi_version,
110+
unsigned include_flags = MODULE_FLAG_HIDDEN);
99111
#endif
100112

101113
/**
102114
* Placeholder that installs module via constructor for every macro
103-
* REGISTER_MODULE/REGISTER_MODULE_HIDDEN call
115+
* REGISTER_MODULE* call
104116
* @param name non-quoted module name
105117
* @param lclass class of the module
106118
* @param abi abi version (specific for every class)
107119
* @param funcname unique function name that will be used to register
108120
* the module (as a constructor)
109-
* @param hidden 0/1 - whether the module should be visible by eg. '-c help'
110-
* (for technical and deprecated modules), default true
121+
* @param flag optional flag to limit visibility (@ref module_flag;
122+
* for technical, deprecated modules and aliases), default 0
111123
*/
112-
#define REGISTER_MODULE_FUNCNAME(name, info, lclass, abi, funcname, hidden) static void funcname(void) __attribute__((constructor));\
124+
#define REGISTER_MODULE_FUNCNAME(name, info, lclass, abi, funcname, flag) \
125+
static void funcname(void) __attribute__((constructor)); \
113126
\
114127
static void funcname(void)\
115128
{\
116-
register_library(#name, info, lclass, abi, hidden);\
129+
register_library(#name, info, lclass, abi, flag);\
117130
}\
118131
struct NOT_DEFINED_STRUCT_THAT_SWALLOWS_SEMICOLON
119132

120-
#define REGISTER_MODULE_FUNC_FUNCNAME(name, func, lclass, abi, funcname, hidden) static void funcname(void) __attribute__((constructor));\
133+
#define REGISTER_MODULE_FUNC_FUNCNAME(name, func, lclass, abi, funcname, flag) \
134+
static void funcname(void) __attribute__((constructor)); \
121135
\
122136
static void funcname(void)\
123137
{\
124138
const void *info = func();\
125139
if (info) {\
126-
register_library(#name, info, lclass, abi, hidden);\
140+
register_library(#name, info, lclass, abi, flag);\
127141
}\
128142
}\
129143
struct NOT_DEFINED_STRUCT_THAT_SWALLOWS_SEMICOLON
@@ -150,10 +164,11 @@ struct NOT_DEFINED_STRUCT_THAT_SWALLOWS_SEMICOLON
150164
#define REGISTER_MODULE_WITH_FUNC(name, func, lclass, abi) REGISTER_MODULE_FUNC_FUNCNAME(name, func, lclass, abi, UNIQUE_LABEL, 0)
151165

152166
/**
153-
* Similar to @ref REGISTER_MODULE but do not show the module under help
154-
* of corresponding class (usable for technical or deprecated modules).
167+
* Similar to @ref REGISTER_MODULE but allow @ref module_flag to be added to limit visibility.
155168
*/
156-
#define REGISTER_HIDDEN_MODULE(name, info, lclass, abi) REGISTER_MODULE_FUNCNAME(name, info, lclass, abi, UNIQUE_LABEL, 1)
169+
#define REGISTER_MODULE_WITH_FLAG(name, info, lclass, abi, flag) \
170+
REGISTER_MODULE_FUNCNAME(name, info, lclass, abi, UNIQUE_LABEL, flag)
171+
#define REGISTER_HIDDEN_MODULE(name, info, lclass, abi) REGISTER_MODULE_WITH_FLAG(name, info, lclass, abi, MODULE_FLAG_HIDDEN)
157172

158173
#endif // defined LIB_COMMON_H
159174

0 commit comments

Comments
 (0)