GULSwizzlingCache.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright 2018 Google LLC
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <Foundation/Foundation.h>
  17. /** This class handles the caching and retreival of IMPs as we swizzle and unswizzle them. It uses
  18. * two C++ STL unordered_maps as the underlying data store. This class is NOT thread safe.
  19. */
  20. @interface GULSwizzlingCache : NSObject
  21. /** Singleton initializer.
  22. *
  23. * @return a singleton GULSwizzlingCache.
  24. */
  25. + (instancetype)sharedInstance;
  26. /** Save the existing IMP that exists before we install the new IMP for a class, selector combo.
  27. * If the currentIMP is something that we put there, it will ignore it and instead point newIMP
  28. * to what existed before we swizzled.
  29. *
  30. * @param newIMP new The IMP that is going to replace the current IMP.
  31. * @param currentIMP The IMP returned by class_getMethodImplementation.
  32. * @param aClass The class that we're swizzling.
  33. * @param selector The selector we're swizzling.
  34. */
  35. - (void)cacheCurrentIMP:(IMP)currentIMP
  36. forNewIMP:(IMP)newIMP
  37. forClass:(Class)aClass
  38. withSelector:(SEL)selector;
  39. /** Save the existing IMP that exists before we install the new IMP for a class, selector combo.
  40. * If the currentIMP is something that we put there, it will ignore it and instead point newIMP
  41. * to what existed before we swizzled.
  42. *
  43. * @param newIMP new The IMP that is going to replace the current IMP.
  44. * @param currentIMP The IMP returned by class_getMethodImplementation.
  45. * @param aClass The class that we're swizzling.
  46. * @param selector The selector we're swizzling.
  47. */
  48. + (void)cacheCurrentIMP:(IMP)currentIMP
  49. forNewIMP:(IMP)newIMP
  50. forClass:(Class)aClass
  51. withSelector:(SEL)selector;
  52. /** Returns the cached IMP that would be invoked with the class and selector combo had we
  53. * never swizzled.
  54. *
  55. * @param aClass The class the selector would be invoked on.
  56. * @param selector The selector
  57. * @return The original IMP i.e. the one that existed right before GULSwizzler swizzled either
  58. * this or a superclass.
  59. */
  60. - (IMP)cachedIMPForClass:(Class)aClass withSelector:(SEL)selector;
  61. /** Clears the cache of values we no longer need because we've unswizzled the relevant method.
  62. *
  63. * @param swizzledIMP The IMP we replaced the existing IMP with.
  64. * @param selector The selector which that we swizzled for.
  65. * @param aClass The class that we're swizzling.
  66. */
  67. - (void)clearCacheForSwizzledIMP:(IMP)swizzledIMP selector:(SEL)selector aClass:(Class)aClass;
  68. @end