mirror of
https://github.com/matrix-construct/construct
synced 2025-01-13 16:33:53 +01:00
mapi: Reincarnate MAPI III key-value metadata, C++ style.
This commit is contained in:
parent
679855be15
commit
0096a4a8c6
4 changed files with 65 additions and 25 deletions
|
@ -26,6 +26,15 @@
|
|||
namespace ircd {
|
||||
namespace mapi {
|
||||
|
||||
enum flags
|
||||
{
|
||||
NO_FLAGS = 0x00,
|
||||
RELAXED_INIT = 0x01,
|
||||
};
|
||||
|
||||
using export_vector = std::vector<std::pair<const void *, std::type_index>>;
|
||||
using metadata = std::map<std::string, std::string>;
|
||||
|
||||
struct init
|
||||
:std::function<void ()>
|
||||
{
|
||||
|
@ -38,15 +47,6 @@ struct fini
|
|||
using std::function<void ()>::function;
|
||||
};
|
||||
|
||||
enum flags
|
||||
{
|
||||
NO_FLAGS = 0x00,
|
||||
RELAXED_INIT = 0x01,
|
||||
};
|
||||
|
||||
// Associates RTTI keyed by object location
|
||||
using export_vector = std::vector<std::pair<const void *, std::type_index>>;
|
||||
|
||||
struct exports
|
||||
:export_vector
|
||||
{
|
||||
|
@ -61,12 +61,16 @@ struct header
|
|||
0x4D41
|
||||
};
|
||||
|
||||
magic_t magic; // The magic must match MAGIC
|
||||
version_t version; // Version indicator
|
||||
enum flags flags; // Option flags
|
||||
int64_t timestamp; // Module's compile epoch
|
||||
const char *desc;
|
||||
struct exports exports;
|
||||
magic_t magic; // The magic must match MAGIC
|
||||
version_t version; // Version indicator
|
||||
enum flags flags; // Option flags
|
||||
int64_t timestamp; // Module's compile epoch
|
||||
struct exports exports; // Generated export vector
|
||||
metadata meta; // Various key-value metadata
|
||||
|
||||
// get and set metadata
|
||||
auto &operator[](const std::string &s) const;
|
||||
auto &operator[](const std::string &s);
|
||||
|
||||
template<class... Exports>
|
||||
header(const char *const &desc,
|
||||
|
@ -93,8 +97,11 @@ header::header(const char *const &desc,
|
|||
,version(4)
|
||||
,flags(flags)
|
||||
,timestamp(RB_DATECODE)
|
||||
,desc(desc)
|
||||
,exports{std::forward<Exports>(exports)...}
|
||||
,meta
|
||||
{
|
||||
{ "description", desc }
|
||||
}
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -104,6 +111,19 @@ header::sym_name
|
|||
"IRCD_MODULE"
|
||||
};
|
||||
|
||||
inline auto &
|
||||
header::operator[](const std::string &key)
|
||||
{
|
||||
return meta[key];
|
||||
}
|
||||
|
||||
inline auto &
|
||||
header::operator[](const std::string &key)
|
||||
const
|
||||
{
|
||||
return meta.at(key);
|
||||
}
|
||||
|
||||
template<class... List>
|
||||
exports::exports(List&&... list)
|
||||
{
|
||||
|
|
|
@ -55,9 +55,10 @@ template<class T> const T &get(const mod &mod, const std::string &symbol);
|
|||
template<class T> T *ptr(mod &mod, const std::string &symbol);
|
||||
template<class T> T &get(mod &mod, const std::string &symbol);
|
||||
const mapi::header &header(const mod &);
|
||||
const char *const &desc(const mod &);
|
||||
const std::string &meta(const mod &, const std::string &key);
|
||||
const version_t &version(const mod &);
|
||||
const int64_t ×tamp(const mod &);
|
||||
const std::string &desc(const mod &);
|
||||
std::string location(const mod &);
|
||||
std::string name(const mod &);
|
||||
|
||||
|
|
|
@ -39,7 +39,7 @@ struct sym
|
|||
struct mod
|
||||
{
|
||||
boost::dll::shared_library handle;
|
||||
const mapi::header *header;
|
||||
mapi::header *header;
|
||||
std::map<std::string, sym> handled;
|
||||
std::multimap<std::type_index, std::string> unhandled;
|
||||
|
||||
|
@ -72,6 +72,11 @@ try
|
|||
throw error("Bad magic [%04x] need: [%04x]",
|
||||
header->magic,
|
||||
mapi::header::MAGIC);
|
||||
|
||||
// Set some basic metadata
|
||||
auto &meta(header->meta);
|
||||
meta["name"] = name(*this);
|
||||
meta["location"] = location(*this);
|
||||
}
|
||||
catch(const boost::system::system_error &e)
|
||||
{
|
||||
|
@ -131,6 +136,18 @@ const
|
|||
return handle.has(name);
|
||||
}
|
||||
|
||||
const std::string &
|
||||
desc(const mod &mod)
|
||||
try
|
||||
{
|
||||
return meta(mod, "description");
|
||||
}
|
||||
catch(const std::out_of_range &e)
|
||||
{
|
||||
static const std::string empty;
|
||||
return empty;
|
||||
}
|
||||
|
||||
std::string
|
||||
name(const mod &mod)
|
||||
{
|
||||
|
@ -149,12 +166,6 @@ version(const mod &mod)
|
|||
return header(mod).version;
|
||||
}
|
||||
|
||||
const char *const &
|
||||
desc(const mod &mod)
|
||||
{
|
||||
return header(mod).desc;
|
||||
}
|
||||
|
||||
const mapi::exports &
|
||||
exports(const mod &mod)
|
||||
{
|
||||
|
@ -167,6 +178,13 @@ flags(const mod &mod)
|
|||
return header(mod).flags;
|
||||
}
|
||||
|
||||
const std::string &
|
||||
meta(const mod &mod,
|
||||
const std::string &key)
|
||||
{
|
||||
return header(mod)[key];
|
||||
}
|
||||
|
||||
const mapi::header &
|
||||
header(const mod &mod)
|
||||
{
|
||||
|
|
|
@ -176,9 +176,10 @@ try
|
|||
const auto iit(mods.emplace_hint(it, nombre, std::move(mod)));
|
||||
{
|
||||
const auto &mod(iit->second);
|
||||
const auto &desc(mods::desc(*mod));
|
||||
log.info("Loaded module %s \"%s\"",
|
||||
mods::name(*mod).c_str(),
|
||||
desc(*mod)?: "<no description>");
|
||||
desc.size()? desc.c_str() : "<no description>");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue