ModelFactory.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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() = default;
  35. virtual ~ObjectCreator() = default;
  36. virtual std::string get() const = 0;
  37. virtual M* operator()(const artis::kernel::AbstractModels<U, V>& /* submodels */) const = 0;
  38. };
  39. template<typename M, typename I, typename O, typename U, typename V>
  40. class ModelFactory {
  41. typedef ModelFactory<M, I, O, U, V> type;
  42. public:
  43. virtual ~ModelFactory() = default;
  44. bool add(const I& id, O* creator)
  45. {
  46. std::lock_guard<std::mutex> lock(_mutex);
  47. return _creators.insert(typename Creators::value_type(
  48. creator->get(),
  49. std::make_pair(id, creator))).second;
  50. }
  51. M* create(const std::string& id, const artis::kernel::AbstractModels<U, V>& submodels)
  52. {
  53. std::lock_guard<std::mutex> lock(_mutex);
  54. typename Creators::const_iterator it = _creators.find(id);
  55. if (it != _creators.end()) {
  56. return (*it->second.second)(submodels);
  57. } else {
  58. return nullptr;
  59. }
  60. }
  61. static ModelFactory& factory()
  62. {
  63. std::call_once(_flag, []() { _instance.reset(new ModelFactory()); });
  64. return *_instance;
  65. }
  66. int make_id()
  67. {
  68. std::lock_guard<std::mutex> lock(_mutex);
  69. return ++_id;
  70. }
  71. bool remove(const I& id)
  72. {
  73. std::lock_guard<std::mutex> lock(_mutex);
  74. typename Creators::const_iterator it = _creators.find(id);
  75. if (it != _creators.end()) {
  76. _creators.erase(it);
  77. return true;
  78. } else {
  79. return false;
  80. }
  81. }
  82. private:
  83. ModelFactory() { _id = -1; }
  84. typedef std::pair<I, O*> Creator;
  85. typedef std::map<std::string, Creator> Creators;
  86. static std::shared_ptr<type> _instance;
  87. static std::once_flag _flag;
  88. std::mutex _mutex;
  89. Creators _creators;
  90. int _id;
  91. };
  92. }
  93. }
  94. template<typename M, typename I, typename O, typename U, typename V>
  95. std::shared_ptr<artis::builder::ModelFactory<M, I, O, U, V> >
  96. artis::builder::ModelFactory<M, I, O, U, V>::_instance;
  97. template<typename M, typename I, typename O, typename U, typename V>
  98. std::once_flag artis::builder::ModelFactory<M, I, O, U, V>::_flag;
  99. #define DECLARE_MODEL(mdl, type, fct, U, V) \
  100. class creator_##mdl : public artis::builder::ObjectCreator < type, U, V > { \
  101. public: \
  102. creator_##mdl() \
  103. { \
  104. fct::factory().add(fct::factory().make_id(), this); \
  105. } \
  106. std::string get() const \
  107. { return boost::core::demangle(typeid(mdl).name()); } \
  108. type* operator()(const artis::kernel::AbstractModels < U, V >& submodels) const \
  109. { return new mdl(submodels); } \
  110. static creator_##mdl an_##mdl; \
  111. }; \
  112. creator_##mdl an_##mdl;
  113. #endif