]> git.tdb.fi Git - libs/gl.git/commitdiff
Use DynamicObjectLoader for Material and Scene generic loaders
authorMikko Rasa <tdb@tdb.fi>
Tue, 5 Oct 2021 11:49:25 +0000 (14:49 +0300)
committerMikko Rasa <tdb@tdb.fi>
Tue, 5 Oct 2021 23:45:21 +0000 (02:45 +0300)
source/materials/material.cpp
source/materials/material.h
source/render/scene.cpp
source/render/scene.h

index 71610c7cd9174957e0bf650f5f50631c2812ddb7..6d9c0571f429f37e8c2ce265ca8c88e92bfaf70c 100644 (file)
@@ -54,9 +54,9 @@ void Material::set_debug_name(const string &name)
 #endif
 }
 
-Material::MaterialRegistry &Material::get_material_registry()
+Material::GenericLoader::TypeRegistry &Material::get_material_registry()
 {
-       static MaterialRegistry registry;
+       static GenericLoader::TypeRegistry registry;
        static bool initialized = false;
        if(!initialized)
        {
@@ -83,32 +83,5 @@ void Material::Loader::sampler(const string &name)
        obj.sampler = &get_collection().get<Sampler>(name);
 }
 
-
-DataFile::Loader::ActionMap Material::GenericLoader::shared_actions;
-
-Material::GenericLoader::GenericLoader(DataFile::Collection &c):
-       coll(c),
-       material(0),
-       mat_loader(0)
-{
-       set_actions(shared_actions);
-}
-
-Material::GenericLoader::~GenericLoader()
-{
-       delete material;
-       delete mat_loader;
-}
-
-void Material::GenericLoader::init_actions()
-{
-       add("type", &GenericLoader::type);
-}
-
-void Material::GenericLoader::type(const DataFile::Symbol &sym)
-{
-       get_material_registry().invoke(sym.name, *this);
-}
-
 } // namespace GL
 } // namespace Msp
index ea02df6b1e00dad8d0dc5275399a55104c5086d5..77e7d44ec29bdedf174d393d5f505aba0cfeb18f 100644 (file)
@@ -1,8 +1,8 @@
 #ifndef MSP_GL_MATERIAL_H_
 #define MSP_GL_MATERIAL_H_
 
-#include <msp/core/typeregistry.h>
 #include <msp/datafile/collection.h>
+#include <msp/datafile/dynamicobjectloader.h>
 #include <msp/datafile/objectloader.h>
 #include "color.h"
 #include "programdata.h"
@@ -56,37 +56,17 @@ protected:
        };
 
 public:
-       class GenericLoader: public DataFile::Loader
+       class GenericLoader: public DataFile::DynamicObjectLoader<Material>
        {
-       private:
-               template<typename T>
-               struct CreateMaterial
-               {
-                       void operator()(const std::string &, GenericLoader &) const;
-               };
-
-               DataFile::Collection &coll;
-               Material *material;
-               Loader *mat_loader;
-
-               static ActionMap shared_actions;
+               friend class Material;
 
        public:
-               GenericLoader(DataFile::Collection &);
-               ~GenericLoader();
+               GenericLoader(Collection &c): DynamicObjectLoader<Material>(&c) { }
 
-               Material *get_object() { Material *m = material; material = 0; return m; }
-       private:
-               virtual void init_actions();
-
-               void type(const DataFile::Symbol &);
-
-               friend class Material;
+       protected:
+               virtual const TypeRegistry &get_type_registry() const { return get_material_registry(); }
        };
 
-private:
-       typedef TypeRegistry<GenericLoader::CreateMaterial, GenericLoader &> MaterialRegistry;
-
 protected:
        const Sampler *sampler;
        ProgramData shdata;
@@ -112,7 +92,7 @@ public:
        template<typename T>
        static void register_type(const std::string &);
 private:
-       static MaterialRegistry &get_material_registry();
+       static GenericLoader::TypeRegistry &get_material_registry();
 };
 
 template<typename T>
@@ -188,19 +168,6 @@ void Material::PropertyLoader<C>::property_texture(void (C::*set_texture)(const
        (static_cast<C &>(obj).*set_texture)(&static_cast<Collection &>(get_collection()).get<Texture>(name));
 }
 
-
-template<typename T>
-void Material::GenericLoader::CreateMaterial<T>::operator()(const std::string &, GenericLoader &ldr) const
-{
-       if(ldr.material)
-               throw std::logic_error("Material type was already specified");
-
-       T *mat = new T;
-       ldr.material = mat;
-       ldr.mat_loader = new typename T::Loader(*mat, ldr.coll);
-       ldr.add_auxiliary_loader(*ldr.mat_loader);
-}
-
 } // namespace GL
 } // namespace Msp
 
index 229588679af54726cb3a203ed3d8b21a1e4330c9..75d1859bdd4995d8c42d8c0aa6f4b9ca747d7928 100644 (file)
@@ -73,9 +73,9 @@ bool Scene::frustum_cull(const Renderable &renderable) const
        return false;
 }
 
-Scene::SceneRegistry &Scene::get_scene_registry()
+Scene::GenericLoader::TypeRegistry &Scene::get_scene_registry()
 {
-       static SceneRegistry registry;
+       static Scene::GenericLoader::TypeRegistry registry;
        static bool initialized = false;
        if(!initialized)
        {
@@ -119,32 +119,5 @@ void Scene::Loader::scene(const string &n)
        obj.add(get_collection().get<Scene>(n));
 }
 
-
-DataFile::Loader::ActionMap Scene::GenericLoader::shared_actions;
-
-Scene::GenericLoader::GenericLoader(DataFile::Collection &c):
-       coll(c),
-       scene(0),
-       scene_loader(0)
-{
-       set_actions(shared_actions);
-}
-
-Scene::GenericLoader::~GenericLoader()
-{
-       delete scene;
-       delete scene_loader;
-}
-
-void Scene::GenericLoader::init_actions()
-{
-       add("type", &GenericLoader::type);
-}
-
-void Scene::GenericLoader::type(const DataFile::Symbol &sym)
-{
-       get_scene_registry().invoke(sym.name, *this);
-}
-
 } // namespace GL
 } // namespace Msp
index 6fb45d6102b9fc422f71ba360da33b2e5a87c926..e55de1a84323529dfe4300c6ebec210d52c8664e 100644 (file)
@@ -2,7 +2,7 @@
 #define MSP_GL_SCENE_H_
 
 #include <map>
-#include <msp/core/typeregistry.h>
+#include <msp/datafile/dynamicobjectloader.h>
 #include <msp/datafile/objectloader.h>
 #include "matrix.h"
 #include "renderable.h"
@@ -39,37 +39,17 @@ protected:
        };
 
 public:
-       class GenericLoader: public DataFile::Loader
+       class GenericLoader: public DataFile::DynamicObjectLoader<Scene>
        {
-       private:
-               template<typename T>
-               struct CreateScene
-               {
-                       void operator()(const std::string &, GenericLoader &) const;
-               };
-
-               DataFile::Collection &coll;
-               Scene *scene;
-               Loader *scene_loader;
-
-               static ActionMap shared_actions;
+               friend class Scene;
 
        public:
-               GenericLoader(DataFile::Collection &);
-               ~GenericLoader();
+               GenericLoader(DataFile::Collection &c): DynamicObjectLoader<Scene>(&c) { }
 
-               Scene *get_object() { Scene *s = scene; scene = 0; return s; }
-       private:
-               virtual void init_actions();
-
-               void type(const DataFile::Symbol &);
-
-               friend class Scene;
+       protected:
+               virtual const TypeRegistry &get_type_registry() const { return get_scene_registry(); }
        };
 
-private:
-       typedef TypeRegistry<GenericLoader::CreateScene, GenericLoader &> SceneRegistry;
-
 protected:
        mutable Matrix culling_matrix;
        mutable Vector4 frustum_edges[6];
@@ -92,7 +72,7 @@ public:
        template<typename T>
        static void register_type(const std::string &);
 private:
-       static SceneRegistry &get_scene_registry();
+       static GenericLoader::TypeRegistry &get_scene_registry();
 };
 
 template<typename T>
@@ -101,18 +81,6 @@ void Scene::register_type(const std::string &kw)
        get_scene_registry().register_type<T>(kw);
 }
 
-template<typename T>
-void Scene::GenericLoader::CreateScene<T>::operator()(const std::string &, GenericLoader &ldr) const
-{
-       if(ldr.scene)
-               throw std::logic_error("Scene type was already specified");
-
-       T *scene = new T;
-       ldr.scene = scene;
-       ldr.scene_loader = new typename T::Loader(*scene, ldr.coll);
-       ldr.add_auxiliary_loader(*ldr.scene_loader);
-}
-
 } // namespace GL
 } // namespace Msp