exposed_object.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // SuperTux -- ExposedObject class
  2. // Copyright (C) 2016 Tobias Markus <tobbi.bugs@googlemail.com>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. #ifndef HEADER_SUPERTUX_SQUIRREL_EXPOSED_OBJECT_HPP
  17. #define HEADER_SUPERTUX_SQUIRREL_EXPOSED_OBJECT_HPP
  18. #include <memory>
  19. #include "squirrel/squirrel_virtual_machine.hpp"
  20. #include "squirrel/squirrel_util.hpp"
  21. #include "squirrel/script_interface.hpp"
  22. #include "util/log.hpp"
  23. /**
  24. * @class ExposedObject
  25. * This class binds a certain GameObject class to a scripting class.
  26. * To bind a game object class to a scripting class, extend the GameObject
  27. * class as shown in the following example:
  28. * \code{.cpp}
  29. * public ExposedObject<Gradient, scripting::Gradient>
  30. * \endcode
  31. *
  32. * and instantiate it in each constructor with the <i>this</i> pointer, like this:
  33. * \code{.cpp}
  34. * Gradient::Gradient(const ReaderMapping& reader) :
  35. * ExposedObject<Gradient, scripting::Gradient>(this)
  36. * \endcode
  37. * @param class S: GameObject class (e.g. Gradient)
  38. * @param class T: Scripting class (e.g. scripting::Gradient)
  39. */
  40. template<class S, class T>
  41. class ExposedObject : virtual public ScriptInterface
  42. {
  43. private:
  44. /**
  45. * The parent object that is exposed to the script interface
  46. */
  47. S* m_parent;
  48. public:
  49. /**
  50. * Constructor
  51. * @param parent GameObject
  52. */
  53. ExposedObject(S* parent) :
  54. m_parent(parent)
  55. {
  56. }
  57. /**
  58. * Exposes the parent GameObject to the script Interface
  59. * @param vm The squirrel virtual machine to expose the object on
  60. * @param table_idx Index of the table to expose the object on
  61. */
  62. virtual void expose(HSQUIRRELVM vm, SQInteger table_idx) override
  63. {
  64. auto name = m_parent->get_name();
  65. if (name.empty())
  66. {
  67. return;
  68. }
  69. log_debug << "Exposing " << m_parent->get_class_name() << " object " << name << std::endl;
  70. auto object = std::make_unique<T>(*m_parent);
  71. expose_object(vm, table_idx, std::move(object), name);
  72. }
  73. /**
  74. * Un-exposes the parent GameObject to the script Interface
  75. * @param vm The squirrel virtual machine to un-expose the object on
  76. * @param table_idx Index of the table to un-expose the object on
  77. */
  78. virtual void unexpose(HSQUIRRELVM vm, SQInteger table_idx) override
  79. {
  80. auto name = m_parent->get_name();
  81. if (name.empty())
  82. {
  83. return;
  84. }
  85. log_debug << "Unexposing object " << name << std::endl;
  86. unexpose_object(vm, table_idx, name);
  87. }
  88. private:
  89. ExposedObject(const ExposedObject&) = delete;
  90. ExposedObject& operator=(const ExposedObject&) = delete;
  91. };
  92. #endif
  93. /* EOF */