cleans up project and adds axi4_pin_level test
This commit is contained in:
4
src/CMakeLists.txt
Normal file
4
src/CMakeLists.txt
Normal file
@ -0,0 +1,4 @@
|
||||
project (test_util)
|
||||
|
||||
add_library(${PROJECT_NAME} factory.cpp)
|
||||
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
|
46
src/factory.cpp
Normal file
46
src/factory.cpp
Normal file
@ -0,0 +1,46 @@
|
||||
#include "factory.h"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
auto factory::get_instance() -> factory& {
|
||||
static factory instance{};
|
||||
return instance;
|
||||
}
|
||||
|
||||
factory::factory() :
|
||||
m_constructors{},
|
||||
m_objects{}
|
||||
{ }
|
||||
|
||||
void factory::create() {
|
||||
for (const auto& item : m_constructors) {
|
||||
m_objects[item.first] = item.second();
|
||||
}
|
||||
}
|
||||
|
||||
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()) {
|
||||
m_constructors[name] = create;
|
||||
}
|
||||
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");
|
||||
}
|
||||
|
||||
return it->second.get();
|
||||
}
|
78
src/factory.h
Normal file
78
src/factory.h
Normal file
@ -0,0 +1,78 @@
|
||||
/*
|
||||
* according to https://forums.accellera.org/topic/5754-unit-testing-with-gtest/
|
||||
* factory.h
|
||||
*
|
||||
* Created on: Oct 1, 2022
|
||||
* Author: eyck
|
||||
*/
|
||||
|
||||
#ifndef SRC_FACTORY_H_
|
||||
#define SRC_FACTORY_H_
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <functional>
|
||||
|
||||
class factory {
|
||||
public:
|
||||
static factory& get_instance();
|
||||
|
||||
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 = "");
|
||||
|
||||
void create();
|
||||
|
||||
void destroy();
|
||||
private:
|
||||
using destructor = std::function<void(void*)>;
|
||||
using object = std::unique_ptr<void, destructor>;
|
||||
using constructor = std::function<object(void)>;
|
||||
|
||||
factory();
|
||||
|
||||
factory(const factory& other) = delete;
|
||||
|
||||
factory& operator=(const factory& other) = delete;
|
||||
|
||||
void add_object(const std::string& name, constructor create);
|
||||
|
||||
void* get_object(const std::string& name);
|
||||
|
||||
std::map<std::string, constructor> m_constructors;
|
||||
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(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& {
|
||||
return *static_cast<T*>(factory::get_instance().get_object(name));
|
||||
}
|
||||
|
||||
#endif /* SRC_FACTORY_H_ */
|
Reference in New Issue
Block a user