extension.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated January 1, 2020. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2020, Esoteric Software LLC
  6. *
  7. * Integration of the Spine Runtimes into software or otherwise creating
  8. * derivative works of the Spine Runtimes is permitted under the terms and
  9. * conditions of Section 2 of the Spine Editor License Agreement:
  10. * http://esotericsoftware.com/spine-editor-license
  11. *
  12. * Otherwise, it is permitted to integrate the Spine Runtimes into software
  13. * or otherwise create derivative works of the Spine Runtimes (collectively,
  14. * "Products"), provided that each user of the Products must obtain their own
  15. * Spine Editor license and redistribution of the Products in any form must
  16. * include this license and copyright notice.
  17. *
  18. * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
  24. * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. #ifndef Spine_Extension_h
  30. #define Spine_Extension_h
  31. #include <stdlib.h>
  32. #include <spine/dll.h>
  33. #define SP_UNUSED(x) (void)(x)
  34. namespace spine {
  35. class String;
  36. class SP_API SpineExtension {
  37. public:
  38. template<typename T>
  39. static T *alloc(size_t num, const char *file, int line) {
  40. return (T *) getInstance()->_alloc(sizeof(T) * num, file, line);
  41. }
  42. template<typename T>
  43. static T *calloc(size_t num, const char *file, int line) {
  44. return (T *) getInstance()->_calloc(sizeof(T) * num, file, line);
  45. }
  46. template<typename T>
  47. static T *realloc(T *ptr, size_t num, const char *file, int line) {
  48. return (T *) getInstance()->_realloc(ptr, sizeof(T) * num, file, line);
  49. }
  50. template<typename T>
  51. static void free(T *ptr, const char *file, int line) {
  52. getInstance()->_free((void *) ptr, file, line);
  53. }
  54. static char *readFile(const String &path, int *length) {
  55. return getInstance()->_readFile(path, length);
  56. }
  57. static void setInstance(SpineExtension *inSpineExtension);
  58. static SpineExtension *getInstance();
  59. virtual ~SpineExtension();
  60. /// Implement this function to use your own memory allocator
  61. virtual void *_alloc(size_t size, const char *file, int line) = 0;
  62. virtual void *_calloc(size_t size, const char *file, int line) = 0;
  63. virtual void *_realloc(void *ptr, size_t size, const char *file, int line) = 0;
  64. /// If you provide a spineAllocFunc, you should also provide a spineFreeFunc
  65. virtual void _free(void *mem, const char *file, int line) = 0;
  66. virtual char *_readFile(const String &path, int *length) = 0;
  67. protected:
  68. SpineExtension();
  69. private:
  70. static SpineExtension *_instance;
  71. };
  72. class SP_API DefaultSpineExtension : public SpineExtension {
  73. public:
  74. DefaultSpineExtension();
  75. virtual ~DefaultSpineExtension();
  76. protected:
  77. virtual void *_alloc(size_t size, const char *file, int line);
  78. virtual void *_calloc(size_t size, const char *file, int line);
  79. virtual void *_realloc(void *ptr, size_t size, const char *file, int line);
  80. virtual void _free(void *mem, const char *file, int line);
  81. virtual char *_readFile(const String &path, int *length);
  82. };
  83. // This function is to be implemented by engine specific runtimes to provide
  84. // the default extension for that engine. It is called the first time
  85. // SpineExtension::getInstance() is called, when no instance has been set
  86. // yet.
  87. extern SpineExtension *getDefaultExtension();
  88. }
  89. #endif /* Spine_Extension_h */