JSWeakObjectMapRefPrivate.h 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright (C) 2010 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
  14. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  17. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  23. * THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef JSWeakObjectMapRefPrivate_h
  26. #define JSWeakObjectMapRefPrivate_h
  27. #include <JavaScriptCore/JSContextRef.h>
  28. #include <JavaScriptCore/JSValueRef.h>
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. /*! @typedef JSWeakObjectMapRef A weak map for storing JSObjectRefs */
  33. typedef struct OpaqueJSWeakObjectMap* JSWeakObjectMapRef;
  34. /*!
  35. @typedef JSWeakMapDestroyedCallback
  36. @abstract The callback invoked when a JSWeakObjectMapRef is being destroyed.
  37. @param map The map that is being destroyed.
  38. @param data The private data (if any) that was associated with the map instance.
  39. */
  40. typedef void (*JSWeakMapDestroyedCallback)(JSWeakObjectMapRef map, void* data);
  41. /*!
  42. @function
  43. @abstract Creates a weak value map that can be used to reference user defined objects without preventing them from being collected.
  44. @param ctx The execution context to use.
  45. @param data A void* to set as the map's private data. Pass NULL to specify no private data.
  46. @param destructor A function to call when the weak map is destroyed.
  47. @result A JSWeakObjectMapRef bound to the given context, data and destructor.
  48. @discussion The JSWeakObjectMapRef can be used as a storage mechanism to hold custom JS objects without forcing those objects to
  49. remain live as JSValueProtect would.
  50. */
  51. JS_EXPORT JSWeakObjectMapRef JSWeakObjectMapCreate(JSContextRef ctx, void* data, JSWeakMapDestroyedCallback destructor);
  52. /*!
  53. @function
  54. @abstract Associates a JSObjectRef with the given key in a JSWeakObjectMap.
  55. @param ctx The execution context to use.
  56. @param map The map to operate on.
  57. @param key The key to associate a weak reference with.
  58. @param object The user defined object to associate with the key.
  59. */
  60. JS_EXPORT void JSWeakObjectMapSet(JSContextRef ctx, JSWeakObjectMapRef map, void* key, JSObjectRef object);
  61. /*!
  62. @function
  63. @abstract Retrieves the JSObjectRef associated with a key.
  64. @param ctx The execution context to use.
  65. @param map The map to query.
  66. @param key The key to search for.
  67. @result Either the live object associated with the provided key, or NULL.
  68. */
  69. JS_EXPORT JSObjectRef JSWeakObjectMapGet(JSContextRef ctx, JSWeakObjectMapRef map, void* key);
  70. /*!
  71. @function
  72. @abstract Removes the entry for the given key if the key is present, otherwise it has no effect.
  73. @param ctx The execution context to use.
  74. @param map The map to use.
  75. @param key The key to remove.
  76. */
  77. JS_EXPORT void JSWeakObjectMapRemove(JSContextRef ctx, JSWeakObjectMapRef map, void* key);
  78. #ifdef __cplusplus
  79. }
  80. #endif
  81. #endif // JSWeakObjectMapPrivate_h