applies cklang-tidy fixes
This commit is contained in:
@ -7,39 +7,33 @@ auto factory::get_instance() -> factory& {
|
||||
return instance;
|
||||
}
|
||||
|
||||
factory::factory() :
|
||||
m_constructors{},
|
||||
m_objects{}
|
||||
{ }
|
||||
factory::factory()
|
||||
: m_constructors{}
|
||||
, m_objects{} {}
|
||||
|
||||
void factory::create() {
|
||||
for (const auto& item : m_constructors) {
|
||||
for(const auto& item : m_constructors) {
|
||||
m_objects[item.first] = item.second();
|
||||
}
|
||||
}
|
||||
|
||||
void factory::destroy() {
|
||||
m_objects.clear();
|
||||
}
|
||||
void factory::destroy() { m_objects.clear(); }
|
||||
|
||||
void factory::add_object(const std::string& name, constructor create) {
|
||||
auto it = m_constructors.find(name);
|
||||
|
||||
if (it == m_constructors.cend()) {
|
||||
if(it == m_constructors.cend()) {
|
||||
m_constructors[name] = create;
|
||||
}
|
||||
else {
|
||||
throw std::runtime_error("factory::add(): "
|
||||
+ name + " object already exist in factory");
|
||||
} else {
|
||||
throw std::runtime_error("factory::add(): " + name + " object already exist in factory");
|
||||
}
|
||||
}
|
||||
|
||||
auto factory::get_object(const std::string& name) -> void* {
|
||||
auto it = m_objects.find(name);
|
||||
|
||||
if (it == m_objects.cend()) {
|
||||
throw std::runtime_error("factory::get(): "
|
||||
+ name + " object doesn't exist in factory");
|
||||
if(it == m_objects.cend()) {
|
||||
throw std::runtime_error("factory::get(): " + name + " object doesn't exist in factory");
|
||||
}
|
||||
|
||||
return it->second.get();
|
||||
|
@ -9,29 +9,28 @@
|
||||
#ifndef SRC_FACTORY_H_
|
||||
#define SRC_FACTORY_H_
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
class factory {
|
||||
public:
|
||||
static factory& get_instance();
|
||||
|
||||
template<typename T, typename ...Args>
|
||||
class add {
|
||||
template <typename T, typename... Args> class add {
|
||||
public:
|
||||
add(Args&&... args);
|
||||
|
||||
add(const std::string& name, Args&&... args);
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
static T& get(const std::string& name = "");
|
||||
template <typename T> static T& get(const std::string& name = "");
|
||||
|
||||
void create();
|
||||
|
||||
void destroy();
|
||||
|
||||
private:
|
||||
using destructor = std::function<void(void*)>;
|
||||
using object = std::unique_ptr<void, destructor>;
|
||||
@ -51,27 +50,15 @@ private:
|
||||
std::map<std::string, object> m_objects;
|
||||
};
|
||||
|
||||
template<typename T, typename ...Args>
|
||||
factory::add<T, Args...>::add(Args&&... args) {
|
||||
add("", args...);
|
||||
template <typename T, typename... Args> factory::add<T, Args...>::add(Args&&... args) { add("", args...); }
|
||||
|
||||
template <typename T, typename... Args> factory::add<T, Args...>::add(const std::string& name, Args&&... args) {
|
||||
factory::get_instance().add_object(name, [args...]() -> object {
|
||||
return object{new T(std::forward<Args>(args)...), [](void* obj) { delete static_cast<T*>(obj); }};
|
||||
});
|
||||
}
|
||||
|
||||
template<typename T, typename ...Args>
|
||||
factory::add<T, Args...>::add(const std::string& name, Args&&... args) {
|
||||
factory::get_instance().add_object(name,
|
||||
[args...] () -> object {
|
||||
return object{
|
||||
new T(std::forward<Args>(args)...),
|
||||
[] (void* obj) {
|
||||
delete static_cast<T*>(obj);
|
||||
}
|
||||
};
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
template<typename T> auto
|
||||
factory::get(const std::string& name) -> T& {
|
||||
template <typename T> auto factory::get(const std::string& name) -> T& {
|
||||
return *static_cast<T*>(factory::get_instance().get_object(name));
|
||||
}
|
||||
|
||||
|
@ -7,26 +7,26 @@
|
||||
|
||||
#include "factory.h"
|
||||
#include <catch2/catch_session.hpp>
|
||||
#include <cstdlib>
|
||||
#include <scc/report.h>
|
||||
#include <scc/trace.h>
|
||||
#include <scc/tracer.h>
|
||||
#include <util/ities.h>
|
||||
#include <cstdlib>
|
||||
|
||||
using namespace scc;
|
||||
using namespace sc_core;
|
||||
|
||||
int sc_main(int argc, char* argv[]) {
|
||||
auto my_name = util::split(argv[0], '/').back();
|
||||
scc::init_logging(LogConfig().logLevel(getenv("SCC_TEST_VERBOSE")?log::DEBUG:log::FATAL).logAsync(false));
|
||||
scc::init_logging(LogConfig().logLevel(getenv("SCC_TEST_VERBOSE") ? log::DEBUG : log::FATAL).logAsync(false));
|
||||
// create tracer if environment variable SCC_TEST_TRACE is defined
|
||||
std::unique_ptr<scc::tracer> tracer;
|
||||
if(getenv("SCC_TEST_TRACE"))
|
||||
tracer=std::make_unique<scc::tracer>(my_name, scc::tracer::file_type::TEXT, true);
|
||||
tracer = std::make_unique<scc::tracer>(my_name, scc::tracer::file_type::TEXT, true);
|
||||
// instantiate design(s)
|
||||
factory::get_instance().create();
|
||||
// run tests
|
||||
int result = Catch::Session().run( argc, argv );
|
||||
int result = Catch::Session().run(argc, argv);
|
||||
// destroy design(s)
|
||||
sc_stop();
|
||||
factory::get_instance().destroy();
|
||||
|
Reference in New Issue
Block a user