0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-12-26 15:33:54 +01:00

ircd::mods: Split into internal header.

This commit is contained in:
Jason Volk 2018-02-08 23:35:42 -08:00
parent e0df498415
commit 9ac16d14d4
2 changed files with 88 additions and 78 deletions

View file

@ -17,39 +17,7 @@ namespace load_mode = boost::dll::load_mode;
#include <ircd/asio.h>
#include <ircd/mapi.h> // Module's internal API
namespace ircd::mods
{
struct mod;
struct log::log extern log;
extern const filesystem::path suffix;
filesystem::path prefix_if_relative(const filesystem::path &path);
filesystem::path postfixed(const filesystem::path &path);
filesystem::path unpostfixed(const filesystem::path &path);
std::string postfixed(const std::string &name);
std::string unpostfixed(const std::string &name);
template<class R, class F> R info(const filesystem::path &, F&& closure);
std::vector<std::string> sections(const filesystem::path &path);
std::vector<std::string> symbols(const filesystem::path &path);
std::vector<std::string> symbols(const filesystem::path &path, const std::string &section);
std::unordered_map<std::string, std::string> mangles(const std::vector<std::string> &);
std::unordered_map<std::string, std::string> mangles(const filesystem::path &path);
std::unordered_map<std::string, std::string> mangles(const filesystem::path &path, const std::string &section);
// Get the full path of a [valid] available module by name
filesystem::path fullpath(const std::string &name);
// Checks if loadable module containing a mapi header (does not verify the magic)
bool is_module(const filesystem::path &);
bool is_module(const filesystem::path &, std::string &why);
bool is_module(const filesystem::path &, std::nothrow_t);
bool is_module(const std::string &fullpath, std::string &why);
bool is_module(const std::string &fullpath, std::nothrow_t);
bool is_module(const std::string &fullpath);
}
#include "mods.h"
ircd::log::log
ircd::mods::log
@ -63,51 +31,6 @@ ircd::mods::suffix
boost::dll::shared_library::suffix()
};
///////////////////////////////////////////////////////////////////////////////
//
// mod (internal)
//
struct ircd::mods::mod
:std::enable_shared_from_this<mod>
{
static std::stack<mod *> loading; // State of current dlopen() recursion.
static std::map<std::string, mod *> loaded;
filesystem::path path;
load_mode::type mode;
std::deque<mod *> children;
std::unordered_map<std::string, std::string> mangles;
boost::dll::shared_library handle;
const std::string _name;
const std::string _location;
mapi::header *header;
// Metadata
auto &operator[](const std::string &s) const { return header->meta.operator[](s); }
auto &operator[](const std::string &s) { return header->meta.operator[](s); }
auto &name() const { return _name; }
auto &location() const { return _location; }
auto &version() const { return header->version; }
auto &description() const { return (*this)["description"]; }
const std::string &mangle(const std::string &name) const;
bool has(const std::string &name) const;
template<class T> const T &get(const std::string &name) const;
template<class T> T &get(const std::string &name);
template<class T = uint8_t> const T *ptr(const std::string &name) const;
template<class T = uint8_t> T *ptr(const std::string &name);
bool unload();
mod(const filesystem::path &,
const load_mode::type & = load_mode::rtld_local | load_mode::rtld_now);
~mod() noexcept;
};
std::stack<ircd::mods::mod *>
ircd::mods::mod::loading
{};
@ -116,6 +39,10 @@ std::map<std::string, ircd::mods::mod *>
ircd::mods::mod::loaded
{};
//
// mod (internal)
//
ircd::mods::mod::mod(const filesystem::path &path,
const load_mode::type &mode)
try

83
ircd/mods.h Normal file
View file

@ -0,0 +1,83 @@
// Matrix Construct
//
// Copyright (C) Matrix Construct Developers, Authors & Contributors
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice is present in all copies. The
// full license for this software is available in the LICENSE file.
namespace ircd::mods
{
struct mod;
struct log::log extern log;
extern const filesystem::path suffix;
filesystem::path prefix_if_relative(const filesystem::path &path);
filesystem::path postfixed(const filesystem::path &path);
filesystem::path unpostfixed(const filesystem::path &path);
std::string postfixed(const std::string &name);
std::string unpostfixed(const std::string &name);
template<class R, class F> R info(const filesystem::path &, F&& closure);
std::vector<std::string> sections(const filesystem::path &path);
std::vector<std::string> symbols(const filesystem::path &path);
std::vector<std::string> symbols(const filesystem::path &path, const std::string &section);
std::unordered_map<std::string, std::string> mangles(const std::vector<std::string> &);
std::unordered_map<std::string, std::string> mangles(const filesystem::path &path);
std::unordered_map<std::string, std::string> mangles(const filesystem::path &path, const std::string &section);
// Get the full path of a [valid] available module by name
filesystem::path fullpath(const std::string &name);
// Checks if loadable module containing a mapi header (does not verify the magic)
bool is_module(const filesystem::path &);
bool is_module(const filesystem::path &, std::string &why);
bool is_module(const filesystem::path &, std::nothrow_t);
bool is_module(const std::string &fullpath, std::string &why);
bool is_module(const std::string &fullpath, std::nothrow_t);
bool is_module(const std::string &fullpath);
}
/// Internal module representation
struct ircd::mods::mod
:std::enable_shared_from_this<mod>
{
static std::stack<mod *> loading; // State of current dlopen() recursion.
static std::map<std::string, mod *> loaded;
filesystem::path path;
load_mode::type mode;
std::deque<mod *> children;
std::unordered_map<std::string, std::string> mangles;
boost::dll::shared_library handle;
const std::string _name;
const std::string _location;
mapi::header *header;
// Metadata
auto &operator[](const std::string &s) const { return header->meta.operator[](s); }
auto &operator[](const std::string &s) { return header->meta.operator[](s); }
auto &name() const { return _name; }
auto &location() const { return _location; }
auto &version() const { return header->version; }
auto &description() const { return (*this)["description"]; }
const std::string &mangle(const std::string &name) const;
bool has(const std::string &name) const;
template<class T> const T &get(const std::string &name) const;
template<class T> T &get(const std::string &name);
template<class T = uint8_t> const T *ptr(const std::string &name) const;
template<class T = uint8_t> T *ptr(const std::string &name);
bool unload();
mod(const filesystem::path &,
const load_mode::type & = load_mode::rtld_local | load_mode::rtld_now);
~mod() noexcept;
};