Skip to content

Commit

Permalink
renamed Backend into ResourceInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
mdorier committed Oct 21, 2024
1 parent 73be9c9 commit 7e53a7b
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 62 deletions.
63 changes: 30 additions & 33 deletions include/alpha/Backend.hpp → include/alpha/ResourceInterface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
*
* See COPYRIGHT in top-level directory.
*/
#ifndef __ALPHA_BACKEND_HPP
#define __ALPHA_BACKEND_HPP
#ifndef __ALPHA_RESOURCE_INTERFACE_HPP
#define __ALPHA_RESOURCE_INTERFACE_HPP

#include <alpha/Result.hpp>
#include <unordered_set>
Expand All @@ -16,26 +16,26 @@
/**
* @brief Helper class to register backend types into the backend factory.
*/
template<typename BackendType>
template<typename ResourceInterfaceType>
class __AlphaBackendRegistration;

namespace alpha {

/**
* @brief Interface for resource backends. To build a new backend,
* implement a class MyBackend that inherits from Backend, and put
* ALPHA_REGISTER_BACKEND(mybackend, MyBackend); in a cpp file
* implement a class MyResourceInterface that inherits from ResourceInterface, and put
* ALPHA_REGISTER_BACKEND(mybackend, MyResourceInterface); in a cpp file
* that includes your backend class' header file.
*
* Your backend class should also have two static functions to
* respectively create and open a resource:
*
* std::unique_ptr<Backend> create(const json& config)
* std::unique_ptr<Backend> attach(const json& config)
* std::unique_ptr<ResourceInterface> create(const json& config)
* std::unique_ptr<ResourceInterface> attach(const json& config)
*/
class Backend {
class ResourceInterface {

template<typename BackendType>
template<typename ResourceInterfaceType>
friend class ::__AlphaBackendRegistration;

std::string m_name;
Expand All @@ -45,32 +45,32 @@ class Backend {
/**
* @brief Constructor.
*/
Backend() = default;
ResourceInterface() = default;

/**
* @brief Move-constructor.
*/
Backend(Backend&&) = default;
ResourceInterface(ResourceInterface&&) = default;

/**
* @brief Copy-constructor.
*/
Backend(const Backend&) = default;
ResourceInterface(const ResourceInterface&) = default;

/**
* @brief Move-assignment operator.
*/
Backend& operator=(Backend&&) = default;
ResourceInterface& operator=(ResourceInterface&&) = default;

/**
* @brief Copy-assignment operator.
*/
Backend& operator=(const Backend&) = default;
ResourceInterface& operator=(const ResourceInterface&) = default;

/**
* @brief Destructor.
*/
virtual ~Backend() = default;
virtual ~ResourceInterface() = default;

/**
* @brief Return the name of backend.
Expand All @@ -84,11 +84,6 @@ class Backend {
*/
virtual std::string getConfig() const = 0;

/**
* @brief Prints Hello World.
*/
virtual void sayHello() = 0;

/**
* @brief Compute the sum of two integers.
*
Expand All @@ -115,7 +110,7 @@ class Backend {
*/
class ResourceFactory {

template<typename BackendType>
template<typename ResourceInterfaceType>
friend class ::__AlphaBackendRegistration;

using json = nlohmann::json;
Expand All @@ -133,9 +128,10 @@ class ResourceFactory {
*
* @return a unique_ptr to the created Resource.
*/
static std::unique_ptr<Backend> createResource(const std::string& backend_name,
const thallium::engine& engine,
const json& config);
static std::unique_ptr<ResourceInterface> createResource(
const std::string& backend_name,
const thallium::engine& engine,
const json& config);

/**
* @brief Opens an existing resource and returns a unique_ptr to the
Expand All @@ -145,19 +141,20 @@ class ResourceFactory {
* @param engine Thallium engine.
* @param config Configuration object to pass to the backend's open function.
*
* @return a unique_ptr to the created Backend.
* @return a unique_ptr to the created ResourceInterface.
*/
static std::unique_ptr<Backend> openResource(const std::string& backend_name,
const thallium::engine& engine,
const json& config);
static std::unique_ptr<ResourceInterface> openResource(
const std::string& backend_name,
const thallium::engine& engine,
const json& config);

private:

static std::unordered_map<std::string,
std::function<std::unique_ptr<Backend>(const thallium::engine&, const json&)>> create_fn;
std::function<std::unique_ptr<ResourceInterface>(const thallium::engine&, const json&)>> create_fn;

static std::unordered_map<std::string,
std::function<std::unique_ptr<Backend>(const thallium::engine&, const json&)>> open_fn;
std::function<std::unique_ptr<ResourceInterface>(const thallium::engine&, const json&)>> open_fn;
};

} // namespace alpha
Expand All @@ -166,7 +163,7 @@ class ResourceFactory {
#define ALPHA_REGISTER_BACKEND(__backend_name, __backend_type) \
static __AlphaBackendRegistration<__backend_type> __alpha ## __backend_name ## _backend( #__backend_name )

template<typename BackendType>
template<typename ResourceInterfaceType>
class __AlphaBackendRegistration {

using json = nlohmann::json;
Expand All @@ -176,12 +173,12 @@ class __AlphaBackendRegistration {
__AlphaBackendRegistration(const std::string& backend_name)
{
alpha::ResourceFactory::create_fn[backend_name] = [backend_name](const thallium::engine& engine, const json& config) {
auto p = BackendType::create(engine, config);
auto p = ResourceInterfaceType::create(engine, config);
p->m_name = backend_name;
return p;
};
alpha::ResourceFactory::open_fn[backend_name] = [backend_name](const thallium::engine& engine, const json& config) {
auto p = BackendType::open(engine, config);
auto p = ResourceInterfaceType::open(engine, config);
p->m_name = backend_name;
return p;
};
Expand Down
20 changes: 11 additions & 9 deletions src/Backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* See COPYRIGHT in top-level directory.
*/
#include "alpha/Backend.hpp"
#include "alpha/ResourceInterface.hpp"

namespace tl = thallium;

Expand All @@ -12,23 +12,25 @@ namespace alpha {
using json = nlohmann::json;

std::unordered_map<std::string,
std::function<std::unique_ptr<Backend>(const tl::engine&, const json&)>> ResourceFactory::create_fn;
std::function<std::unique_ptr<ResourceInterface>(const tl::engine&, const json&)>> ResourceFactory::create_fn;

std::unordered_map<std::string,
std::function<std::unique_ptr<Backend>(const tl::engine&, const json&)>> ResourceFactory::open_fn;
std::function<std::unique_ptr<ResourceInterface>(const tl::engine&, const json&)>> ResourceFactory::open_fn;

std::unique_ptr<Backend> ResourceFactory::createResource(const std::string& backend_name,
const tl::engine& engine,
const json& config) {
std::unique_ptr<ResourceInterface> ResourceFactory::createResource(
const std::string& backend_name,
const tl::engine& engine,
const json& config) {
auto it = create_fn.find(backend_name);
if(it == create_fn.end()) return nullptr;
auto& f = it->second;
return f(engine, config);
}

std::unique_ptr<Backend> ResourceFactory::openResource(const std::string& backend_name,
const tl::engine& engine,
const json& config) {
std::unique_ptr<ResourceInterface> ResourceFactory::openResource(
const std::string& backend_name,
const tl::engine& engine,
const json& config) {
auto it = open_fn.find(backend_name);
if(it == open_fn.end()) return nullptr;
auto& f = it->second;
Expand Down
6 changes: 3 additions & 3 deletions src/ProviderImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#ifndef __ALPHA_PROVIDER_IMPL_H
#define __ALPHA_PROVIDER_IMPL_H

#include "alpha/Backend.hpp"
#include "alpha/ResourceInterface.hpp"

#include <thallium.hpp>
#include <thallium/serialization/stl/string.hpp>
Expand Down Expand Up @@ -51,8 +51,8 @@ class ProviderImpl : public tl::provider<ProviderImpl> {
// Client RPC
tl::auto_remote_procedure m_compute_sum;
// FIXME: other RPCs go here ...
// Backends
std::shared_ptr<Backend> m_backend;
// ResourceInterfaces
std::shared_ptr<ResourceInterface> m_backend;

ProviderImpl(const tl::engine& engine, uint16_t provider_id, const std::string& config, const tl::pool& pool)
: tl::provider<ProviderImpl>(engine, provider_id, "alpha")
Expand Down
12 changes: 4 additions & 8 deletions src/dummy/DummyBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ DummyResource::DummyResource(thallium::engine engine, const json& config)

}

void DummyResource::sayHello() {
std::cout << "Hello World" << std::endl;
}

std::string DummyResource::getConfig() const {
return m_config.dump();
}
Expand All @@ -35,12 +31,12 @@ alpha::Result<bool> DummyResource::destroy() {
return result;
}

std::unique_ptr<alpha::Backend> DummyResource::create(const thallium::engine& engine, const json& config) {
std::unique_ptr<alpha::ResourceInterface> DummyResource::create(const thallium::engine& engine, const json& config) {
(void)engine;
return std::unique_ptr<alpha::Backend>(new DummyResource(engine, config));
return std::unique_ptr<alpha::ResourceInterface>(new DummyResource(engine, config));
}

std::unique_ptr<alpha::Backend> DummyResource::open(const thallium::engine& engine, const json& config) {
std::unique_ptr<alpha::ResourceInterface> DummyResource::open(const thallium::engine& engine, const json& config) {
(void)engine;
return std::unique_ptr<alpha::Backend>(new DummyResource(engine, config));
return std::unique_ptr<alpha::ResourceInterface>(new DummyResource(engine, config));
}
13 changes: 4 additions & 9 deletions src/dummy/DummyBackend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
#ifndef __DUMMY_BACKEND_HPP
#define __DUMMY_BACKEND_HPP

#include <alpha/Backend.hpp>
#include <alpha/ResourceInterface.hpp>

using json = nlohmann::json;

/**
* Dummy implementation of an alpha Backend.
*/
class DummyResource : public alpha::Backend {
class DummyResource : public alpha::ResourceInterface {

thallium::engine m_engine;
json m_config;
Expand Down Expand Up @@ -55,11 +55,6 @@ class DummyResource : public alpha::Backend {
*/
std::string getConfig() const override;

/**
* @brief Prints Hello World.
*/
void sayHello() override;

/**
* @brief Compute the sum of two integers.
*
Expand Down Expand Up @@ -87,7 +82,7 @@ class DummyResource : public alpha::Backend {
*
* @return a unique_ptr to a resource
*/
static std::unique_ptr<alpha::Backend> create(const thallium::engine& engine, const json& config);
static std::unique_ptr<alpha::ResourceInterface> create(const thallium::engine& engine, const json& config);

/**
* @brief Static factory function used by the ResourceFactory to
Expand All @@ -98,7 +93,7 @@ class DummyResource : public alpha::Backend {
*
* @return a unique_ptr to a resource
*/
static std::unique_ptr<alpha::Backend> open(const thallium::engine& engine, const json& config);
static std::unique_ptr<alpha::ResourceInterface> open(const thallium::engine& engine, const json& config);
};

#endif

0 comments on commit 7e53a7b

Please sign in to comment.