36 lines
962 B
C++
36 lines
962 B
C++
#pragma once
|
|
#include <functional>
|
|
#include <mutex>
|
|
#include <vector>
|
|
#include <Mixin/Types.hpp>
|
|
|
|
namespace vl::mixin {
|
|
|
|
// Core provides a function that consumes specs. API buffers until set.
|
|
using RegistrarFn = void(*)(const MixinSpec&);
|
|
|
|
class Registrar {
|
|
public:
|
|
static void set_sink(RegistrarFn fn) {
|
|
std::lock_guard lk(mu_);
|
|
sink_ = fn;
|
|
if (!pending_.empty()) {
|
|
for (auto &s : pending_) sink_(s);
|
|
pending_.clear();
|
|
pending_.shrink_to_fit();
|
|
}
|
|
}
|
|
static void submit(const MixinSpec& spec) {
|
|
std::lock_guard lk(mu_);
|
|
if (sink_) { sink_(spec); }
|
|
else { pending_.push_back(spec); }
|
|
}
|
|
|
|
private:
|
|
static inline std::mutex mu_;
|
|
static inline RegistrarFn sink_ = nullptr;
|
|
static inline std::vector<MixinSpec> pending_;
|
|
};
|
|
|
|
} // namespace vl::mixin
|