scoped_objc_class_swizzler.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_MAC_SCOPED_OBJC_CLASS_SWIZZLER_H_
  5. #define BASE_MAC_SCOPED_OBJC_CLASS_SWIZZLER_H_
  6. #import <objc/runtime.h>
  7. #include "base/base_export.h"
  8. namespace base::mac {
  9. // Within a given scope, swaps method implementations of a class interface, or
  10. // between two class interfaces. The argument and return types must match.
  11. class BASE_EXPORT ScopedObjCClassSwizzler {
  12. public:
  13. // Given two classes that each respond to |selector|, swap the implementations
  14. // of those methods.
  15. ScopedObjCClassSwizzler(Class target, Class source, SEL selector);
  16. // Given two selectors on the same class interface, |target| (e.g. via
  17. // inheritance or categories), swap the implementations of methods |original|
  18. // and |alternate|.
  19. ScopedObjCClassSwizzler(Class target, SEL original, SEL alternate);
  20. ScopedObjCClassSwizzler(const ScopedObjCClassSwizzler&) = delete;
  21. ScopedObjCClassSwizzler& operator=(const ScopedObjCClassSwizzler&) = delete;
  22. ~ScopedObjCClassSwizzler();
  23. // Return a callable function pointer for the replaced method. To call this
  24. // from the replacing function, the first two arguments should be |self| and
  25. // |_cmd|. These are followed by the (variadic) method arguments.
  26. IMP GetOriginalImplementation() const;
  27. // Invoke the original function directly, optionally with some arguments.
  28. // Prefer this to hanging onto pointers to the original implementation
  29. // function or to casting the result of GetOriginalImplementation() yourself.
  30. template <typename Ret, typename... Args>
  31. Ret InvokeOriginal(id receiver, SEL selector, Args... args) const {
  32. auto func = reinterpret_cast<Ret (*)(id, SEL, Args...)>(
  33. GetOriginalImplementation());
  34. return func(receiver, selector, args...);
  35. }
  36. private:
  37. // Delegated constructor.
  38. void Init(Class target, Class source, SEL original, SEL alternate);
  39. Method old_selector_impl_;
  40. Method new_selector_impl_;
  41. };
  42. } // namespace base::mac
  43. #endif // BASE_MAC_SCOPED_OBJC_CLASS_SWIZZLER_H_