ModelFactory.hpp 4.4 KB

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