ModelFactory.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. * @file artis/builder/ModelFactory.hpp
  3. * @author See the AUTHORS file
  4. */
  5. /*
  6. * Copyright (C) 2012-2019 ULCO http://www.univ-littoral.fr
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #ifndef __ARTIS_BUILDER_MODEL_FACTORY_HPP
  22. #define __ARTIS_BUILDER_MODEL_FACTORY_HPP
  23. #include <algorithm>
  24. #include <memory>
  25. #include <mutex>
  26. #include <typeinfo>
  27. #include <boost/core/demangle.hpp>
  28. #include <artis/kernel/AbstractModel.hpp>
  29. namespace artis {
  30. namespace builder {
  31. template<typename M, typename U, typename V>
  32. class ObjectCreator {
  33. public:
  34. ObjectCreator() { }
  35. virtual ~ObjectCreator() { }
  36. virtual std::string get() const = 0;
  37. virtual M* operator()(artis::kernel::AbstractModels<U,
  38. V> submodels) const = 0;
  39. };
  40. template<typename M, typename I, typename O, typename U, typename V>
  41. class ModelFactory {
  42. typedef ModelFactory<M, I, O, U, V> type;
  43. public:
  44. virtual ~ModelFactory() { }
  45. bool add(const I& id, O* creator)
  46. {
  47. std::lock_guard<std::mutex> lock(_mutex);
  48. return creators.insert(typename Creators::value_type(
  49. creator->get(),
  50. std::make_pair(id, creator))).second;
  51. }
  52. M* create(const std::string& id, artis::kernel::AbstractModels<U,
  53. V> submodels)
  54. {
  55. std::lock_guard<std::mutex> lock(_mutex);
  56. typename Creators::const_iterator it = creators.find(id);
  57. if (it != creators.end()) {
  58. return (*it->second.second)(submodels);
  59. } else {
  60. return nullptr;
  61. }
  62. }
  63. static ModelFactory& factory()
  64. {
  65. std::call_once(_flag, []() { _instance.reset(new ModelFactory()); });
  66. return *_instance;
  67. }
  68. int make_id()
  69. {
  70. std::lock_guard<std::mutex> lock(_mutex);
  71. return ++id;
  72. }
  73. bool remove(const I& id)
  74. {
  75. std::lock_guard<std::mutex> lock(_mutex);
  76. typename Creators::const_iterator it = creators.find(id);
  77. if (it != creators.end()) {
  78. creators.erase(it);
  79. return true;
  80. } else {
  81. return false;
  82. }
  83. }
  84. private:
  85. ModelFactory() { id = -1; }
  86. typedef std::pair<I, O*> Creator;
  87. typedef std::map<std::string, Creator> Creators;
  88. static std::shared_ptr<type> _instance;
  89. static std::once_flag _flag;
  90. std::mutex _mutex;
  91. Creators creators;
  92. int id;
  93. };
  94. }
  95. }
  96. template<typename M, typename I, typename O, typename U, typename V>
  97. std::shared_ptr<artis::builder::ModelFactory<M, I, O, U, V> >
  98. artis::builder::ModelFactory<M, I, O, U, V>::_instance;
  99. template<typename M, typename I, typename O, typename U, typename V>
  100. std::once_flag artis::builder::ModelFactory<M, I, O, U, V>::_flag;
  101. #define DECLARE_MODEL(mdl, type, fct, U, V) \
  102. class creator_##mdl : public artis::builder::ObjectCreator < type, U, V > { \
  103. public: \
  104. creator_##mdl() \
  105. { \
  106. fct::factory().add(fct::factory().make_id(), this); \
  107. } \
  108. std::string get() const \
  109. { return boost::core::demangle(typeid(mdl).name()); } \
  110. type* operator()(artis::kernel::AbstractModels < U, V > submodels) const \
  111. { return new mdl(submodels); } \
  112. static creator_##mdl an_##mdl; \
  113. }; \
  114. creator_##mdl an_##mdl;
  115. #endif