rename component_factory to factory

This commit is contained in:
2019-05-23 21:29:41 +07:00
parent b58c79c9a5
commit 71ad1c743e
23 changed files with 144 additions and 151 deletions

View File

@@ -40,8 +40,8 @@
#include "asset.hpp"
#include "asset.inl"
#include "atlas.hpp"
#include "component.hpp"
#include "component.inl"
#include "factory.hpp"
#include "factory.inl"
#include "flipbook.hpp"
#include "gobject.hpp"
#include "library.hpp"

View File

@@ -52,13 +52,12 @@ namespace e2d
class content_asset;
class asset;
class factory;
class library;
class asset_cache;
class asset_group;
class asset_dependencies;
class component_factory;
class atlas;
class flipbook;
class gobject;

View File

@@ -8,7 +8,7 @@
#include "../_high.hpp"
#include "../component.hpp"
#include "../factory.hpp"
#include "../node.hpp"
namespace e2d
@@ -27,17 +27,17 @@ namespace e2d
};
template <>
class component_loader<actor> {
class factory_loader<actor> final : factory_loader<> {
public:
static const char* schema_source;
bool operator()(
actor& component,
const component_loader<>::fill_context& ctx) const;
const fill_context& ctx) const;
bool operator()(
asset_dependencies& dependencies,
const component_loader<>::collect_context& ctx) const;
const collect_context& ctx) const;
};
}

View File

@@ -8,7 +8,7 @@
#include "../_high.hpp"
#include "../component.hpp"
#include "../factory.hpp"
namespace e2d
{
@@ -36,17 +36,17 @@ namespace e2d
};
template <>
class component_loader<camera> {
class factory_loader<camera> final : factory_loader<> {
public:
static const char* schema_source;
bool operator()(
camera& component,
const component_loader<>::fill_context& ctx) const;
const fill_context& ctx) const;
bool operator()(
asset_dependencies& dependencies,
const component_loader<>::collect_context& ctx) const;
const collect_context& ctx) const;
};
}

View File

@@ -8,7 +8,7 @@
#include "../_high.hpp"
#include "../component.hpp"
#include "../factory.hpp"
#include "../assets/flipbook_asset.hpp"
namespace e2d
@@ -53,17 +53,17 @@ namespace e2d
};
template <>
class component_loader<flipbook_player> {
class factory_loader<flipbook_player> final : factory_loader<> {
public:
static const char* schema_source;
bool operator()(
flipbook_player& component,
const component_loader<>::fill_context& ctx) const;
const fill_context& ctx) const;
bool operator()(
asset_dependencies& dependencies,
const component_loader<>::collect_context& ctx) const;
const collect_context& ctx) const;
};
}

View File

@@ -8,7 +8,7 @@
#include "../_high.hpp"
#include "../component.hpp"
#include "../factory.hpp"
#include "../assets/flipbook_asset.hpp"
namespace e2d
@@ -25,17 +25,17 @@ namespace e2d
};
template <>
class component_loader<flipbook_source> {
class factory_loader<flipbook_source> final : factory_loader<> {
public:
static const char* schema_source;
bool operator()(
flipbook_source& component,
const component_loader<>::fill_context& ctx) const;
const fill_context& ctx) const;
bool operator()(
asset_dependencies& dependencies,
const component_loader<>::collect_context& ctx) const;
const collect_context& ctx) const;
};
}

View File

@@ -8,7 +8,7 @@
#include "../_high.hpp"
#include "../component.hpp"
#include "../factory.hpp"
#include "../assets/model_asset.hpp"
namespace e2d
@@ -25,17 +25,17 @@ namespace e2d
};
template <>
class component_loader<model_renderer> {
class factory_loader<model_renderer> final : factory_loader<> {
public:
static const char* schema_source;
bool operator()(
model_renderer& component,
const component_loader<>::fill_context& ctx) const;
const fill_context& ctx) const;
bool operator()(
asset_dependencies& dependencies,
const component_loader<>::collect_context& ctx) const;
const collect_context& ctx) const;
};
}

View File

@@ -8,7 +8,7 @@
#include "../_high.hpp"
#include "../component.hpp"
#include "../factory.hpp"
#include "../assets/material_asset.hpp"
namespace e2d
@@ -38,17 +38,17 @@ namespace e2d
};
template <>
class component_loader<renderer> {
class factory_loader<renderer> final : factory_loader<> {
public:
static const char* schema_source;
bool operator()(
renderer& component,
const component_loader<>::fill_context& ctx) const;
const fill_context& ctx) const;
bool operator()(
asset_dependencies& dependencies,
const component_loader<>::collect_context& ctx) const;
const collect_context& ctx) const;
};
}

View File

@@ -8,7 +8,7 @@
#include "../_high.hpp"
#include "../component.hpp"
#include "../factory.hpp"
namespace e2d
{
@@ -23,17 +23,17 @@ namespace e2d
};
template <>
class component_loader<scene> {
class factory_loader<scene> final : factory_loader<> {
public:
static const char* schema_source;
bool operator()(
scene& component,
const component_loader<>::fill_context& ctx) const;
const fill_context& ctx) const;
bool operator()(
asset_dependencies& dependencies,
const component_loader<>::collect_context& ctx) const;
const collect_context& ctx) const;
};
}

View File

@@ -8,7 +8,7 @@
#include "../_high.hpp"
#include "../component.hpp"
#include "../factory.hpp"
#include "../assets/sprite_asset.hpp"
namespace e2d
@@ -33,17 +33,17 @@ namespace e2d
};
template <>
class component_loader<sprite_renderer> {
class factory_loader<sprite_renderer> final : factory_loader<> {
public:
static const char* schema_source;
bool operator()(
sprite_renderer& component,
const component_loader<>::fill_context& ctx) const;
const fill_context& ctx) const;
bool operator()(
asset_dependencies& dependencies,
const component_loader<>::collect_context& ctx) const;
const collect_context& ctx) const;
};
}

View File

@@ -4,8 +4,8 @@
* Copyright (C) 2018-2019, by Matvey Cherevko (blackmatov@gmail.com)
******************************************************************************/
#ifndef E2D_INCLUDE_GUARD_DEE612552A524342A523868F34FF88BA
#define E2D_INCLUDE_GUARD_DEE612552A524342A523868F34FF88BA
#ifndef E2D_INCLUDE_GUARD_9C9B1BA27AAC42CBAA07890458076B81
#define E2D_INCLUDE_GUARD_9C9B1BA27AAC42CBAA07890458076B81
#pragma once
#include "_high.hpp"
@@ -13,25 +13,25 @@
namespace e2d
{
//
// bad_component_factory_operation
// bad_factory_operation
//
class bad_component_factory_operation final : public exception {
class bad_factory_operation final : public exception {
public:
const char* what() const noexcept final {
return "bad component factory operation";
return "bad factory operation";
}
};
//
// component_loader
// factory_loader
//
template < typename Component = void >
class component_loader;
class factory_loader;
template <>
class component_loader<> {
class factory_loader<> {
public:
struct fill_context {
const str parent_address;
@@ -57,71 +57,66 @@ namespace e2d
: parent_address(std::move(nparent_address))
, root(nroot) {}
};
public:
};
//
// component_creator_base
// factory_creator
//
class component_creator_base;
using component_creator_base_iptr = intrusive_ptr<component_creator_base>;
class factory_creator;
using factory_creator_iptr = intrusive_ptr<factory_creator>;
class component_creator_base
class factory_creator
: private noncopyable
, public ref_counter<component_creator_base> {
, public ref_counter<factory_creator> {
public:
component_creator_base() = default;
virtual ~component_creator_base() noexcept = default;
factory_creator() = default;
virtual ~factory_creator() noexcept = default;
virtual bool validate_json(
const rapidjson::Value& root) const = 0;
virtual bool fill_prototype(
ecs::prototype& prototype,
const component_loader<>::fill_context& ctx) const = 0;
const factory_loader<>::fill_context& ctx) const = 0;
virtual bool collect_dependencies(
asset_dependencies& dependencies,
const component_loader<>::collect_context& ctx) const = 0;
const factory_loader<>::collect_context& ctx) const = 0;
};
//
// component_creator
//
template < typename Component >
class component_creator : public component_creator_base {
class typed_factory_creator final : public factory_creator {
public:
component_creator();
~component_creator() noexcept override = default;
typed_factory_creator();
~typed_factory_creator() noexcept final = default;
bool validate_json(
const rapidjson::Value& root) const override;
const rapidjson::Value& root) const final;
bool fill_prototype(
ecs::prototype& prototype,
const component_loader<>::fill_context& ctx) const override;
const factory_loader<>::fill_context& ctx) const final;
bool collect_dependencies(
asset_dependencies& dependencies,
const component_loader<>::collect_context& ctx) const override;
const factory_loader<>::collect_context& ctx) const final;
private:
component_loader<Component> loader_;
factory_loader<Component> loader_;
std::unique_ptr<rapidjson::SchemaDocument> schema_;
};
//
// component_factory
// factory
//
class component_factory : public module<component_factory> {
class factory : public module<factory> {
public:
component_factory() = default;
~component_factory() noexcept final = default;
factory() = default;
~factory() noexcept final = default;
template < typename Component >
component_factory& register_component(str_hash type);
factory& register_component(str_hash type);
bool validate_json(
str_hash type,
@@ -130,19 +125,19 @@ namespace e2d
bool fill_prototype(
str_hash type,
ecs::prototype& prototype,
const component_loader<>::fill_context& ctx) const;
const factory_loader<>::fill_context& ctx) const;
bool collect_dependencies(
str_hash type,
asset_dependencies& dependencies,
const component_loader<>::collect_context& ctx) const;
const factory_loader<>::collect_context& ctx) const;
private:
component_creator_base_iptr find_creator(str_hash type) const;
factory_creator_iptr find_creator(str_hash type) const;
private:
mutable std::mutex mutex_;
hash_map<str_hash, component_creator_base_iptr> creators_;
hash_map<str_hash, factory_creator_iptr> creators_;
};
}
#include "component.inl"
#include "factory.inl"
#endif

View File

@@ -4,32 +4,32 @@
* Copyright (C) 2018-2019, by Matvey Cherevko (blackmatov@gmail.com)
******************************************************************************/
#ifndef E2D_INCLUDE_GUARD_85A0D62C0D794C4C9DDFB1BAED3C0A18
#define E2D_INCLUDE_GUARD_85A0D62C0D794C4C9DDFB1BAED3C0A18
#ifndef E2D_INCLUDE_GUARD_6CDF0C8E49C9459EB7762C8AF7F7ED85
#define E2D_INCLUDE_GUARD_6CDF0C8E49C9459EB7762C8AF7F7ED85
#pragma once
#include "_high.hpp"
#include "component.hpp"
#include "factory.hpp"
namespace e2d
{
//
// component_creator
// typed_factory_creator
//
template < typename Component >
component_creator<Component>::component_creator() {
typed_factory_creator<Component>::typed_factory_creator() {
rapidjson::Document doc;
if ( doc.Parse(loader_.schema_source).HasParseError() ) {
the<debug>().error("COMPONENT: Failed to parse component loader schema");
throw bad_component_factory_operation();
the<debug>().error("FACTORY: Failed to parse factory loader schema");
throw bad_factory_operation();
}
json_utils::add_common_schema_definitions(doc);
schema_ = std::make_unique<rapidjson::SchemaDocument>(doc);
}
template < typename Component >
bool component_creator<Component>::validate_json(
bool typed_factory_creator<Component>::validate_json(
const rapidjson::Value& root) const
{
rapidjson::SchemaValidator validator(*schema_);
@@ -38,10 +38,10 @@ namespace e2d
}
rapidjson::StringBuffer sb;
if ( !validator.GetInvalidDocumentPointer().StringifyUriFragment(sb) ) {
the<debug>().error("COMPONENT: Failed to validate component json");
the<debug>().error("FACTORY: Failed to validate component json");
return false;
}
the<debug>().error("COMPONENT: Failed to validate component json:\n"
the<debug>().error("FACTORY: Failed to validate component json:\n"
"--> Invalid schema keyword: %0\n"
"--> Invalid document pointer: %1",
validator.GetInvalidSchemaKeyword(),
@@ -50,9 +50,9 @@ namespace e2d
}
template < typename Component >
bool component_creator<Component>::fill_prototype(
bool typed_factory_creator<Component>::fill_prototype(
ecs::prototype& prototype,
const component_loader<>::fill_context& ctx) const
const factory_loader<>::fill_context& ctx) const
{
Component component;
prototype.apply_to_component(component);
@@ -66,27 +66,26 @@ namespace e2d
}
template < typename Component >
bool component_creator<Component>::collect_dependencies(
bool typed_factory_creator<Component>::collect_dependencies(
asset_dependencies& dependencies,
const component_loader<>::collect_context& ctx) const
const factory_loader<>::collect_context& ctx) const
{
return loader_(dependencies, ctx);
}
//
// component_factory
// factory
//
template < typename Component >
component_factory& component_factory::register_component(str_hash type) {
factory& factory::register_component(str_hash type) {
std::lock_guard<std::mutex> guard(mutex_);
if ( creators_.count(type) > 0 ) {
throw bad_component_factory_operation();
throw bad_factory_operation();
}
component_creator_base_iptr creator(new component_creator<Component>());
factory_creator_iptr creator(new typed_factory_creator<Component>());
creators_.emplace(type, std::move(creator));
return *this;
}
}
#endif