MappingUtils.hpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /****************************************************************************
  2. Copyright (c) 2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #pragma once
  22. #include <unordered_map>
  23. namespace se {
  24. class Object;
  25. class NativePtrToObjectMap
  26. {
  27. public:
  28. // key: native ptr, value: se::Object
  29. using Map = std::unordered_map<void*, Object*>;
  30. static bool init();
  31. static void destroy();
  32. static Map::iterator find(void* nativeObj);
  33. static Map::iterator erase(Map::iterator iter);
  34. static void erase(void* nativeObj);
  35. static void clear();
  36. static size_t size();
  37. static const Map& instance();
  38. static Map::iterator begin();
  39. static Map::iterator end();
  40. private:
  41. static void emplace(void* nativeObj, Object* seObj);
  42. static Map* __nativePtrToObjectMap;
  43. friend class Object;
  44. };
  45. class NonRefNativePtrCreatedByCtorMap
  46. {
  47. public:
  48. // key: native ptr, value: non-ref object created by ctor
  49. using Map = std::unordered_map<void*, bool>;
  50. static bool init();
  51. static void destroy();
  52. static void emplace(void* nativeObj);
  53. static Map::iterator find(void* nativeObj);
  54. static Map::iterator erase(Map::iterator iter);
  55. static void erase(void* nativeObj);
  56. static void clear();
  57. static size_t size();
  58. static const Map& instance();
  59. static Map::iterator begin();
  60. static Map::iterator end();
  61. private:
  62. static Map* __nonRefNativeObjectCreatedByCtorMap;
  63. };
  64. } // namespace se {