openshot-audio  0.1.5
juce_osx_ObjCHelpers.h
Go to the documentation of this file.
1 /*
2  ==============================================================================
3 
4  This file is part of the juce_core module of the JUCE library.
5  Copyright (c) 2015 - ROLI Ltd.
6 
7  Permission to use, copy, modify, and/or distribute this software for any purpose with
8  or without fee is hereby granted, provided that the above copyright notice and this
9  permission notice appear in all copies.
10 
11  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
12  TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
13  NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14  DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
15  IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
16  CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 
18  ------------------------------------------------------------------------------
19 
20  NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
21  All other JUCE modules are covered by a dual GPL/commercial license, so if you are
22  using any other modules, be sure to check that you also comply with their license.
23 
24  For more details, visit www.juce.com
25 
26  ==============================================================================
27 */
28 
29 #ifndef JUCE_OSX_OBJCHELPERS_H_INCLUDED
30 #define JUCE_OSX_OBJCHELPERS_H_INCLUDED
31 
32 
33 /* This file contains a few helper functions that are used internally but which
34  need to be kept away from the public headers because they use obj-C symbols.
35 */
36 namespace
37 {
38  //==============================================================================
39  static inline String nsStringToJuce (NSString* s)
40  {
41  return CharPointer_UTF8 ([s UTF8String]);
42  }
43 
44  static inline NSString* juceStringToNS (const String& s)
45  {
46  return [NSString stringWithUTF8String: s.toUTF8()];
47  }
48 
49  static inline NSString* nsStringLiteral (const char* const s) noexcept
50  {
51  return [NSString stringWithUTF8String: s];
52  }
53 
54  static inline NSString* nsEmptyString() noexcept
55  {
56  return [NSString string];
57  }
58 
59  #if JUCE_MAC
60  template <typename RectangleType>
61  static NSRect makeNSRect (const RectangleType& r) noexcept
62  {
63  return NSMakeRect (static_cast <CGFloat> (r.getX()),
64  static_cast <CGFloat> (r.getY()),
65  static_cast <CGFloat> (r.getWidth()),
66  static_cast <CGFloat> (r.getHeight()));
67  }
68 
69  // These hacks are a workaround for newer Xcode builds which by default prevent calls to these objc functions..
70  typedef id (*MsgSendSuperFn) (struct objc_super*, SEL, ...);
71  static inline MsgSendSuperFn getMsgSendSuperFn() noexcept { return (MsgSendSuperFn) (void*) objc_msgSendSuper; }
72 
73  #if ! JUCE_PPC
74  typedef double (*MsgSendFPRetFn) (id, SEL op, ...);
75  static inline MsgSendFPRetFn getMsgSendFPRetFn() noexcept { return (MsgSendFPRetFn) (void*) objc_msgSend_fpret; }
76  #endif
77  #endif
78 }
79 
80 //==============================================================================
81 template <typename ObjectType>
83 {
84  inline NSObjectRetainer (ObjectType* o) : object (o) { [object retain]; }
85  inline ~NSObjectRetainer() { [object release]; }
86 
87  ObjectType* object;
88 };
89 
90 //==============================================================================
91 template <typename SuperclassType>
92 struct ObjCClass
93 {
94  ObjCClass (const char* nameRoot)
95  : cls (objc_allocateClassPair ([SuperclassType class], getRandomisedName (nameRoot).toUTF8(), 0))
96  {
97  }
98 
100  {
101  objc_disposeClassPair (cls);
102  }
103 
105  {
106  objc_registerClassPair (cls);
107  }
108 
109  SuperclassType* createInstance() const
110  {
111  return class_createInstance (cls, 0);
112  }
113 
114  template <typename Type>
115  void addIvar (const char* name)
116  {
117  BOOL b = class_addIvar (cls, name, sizeof (Type), (uint8_t) rint (log2 (sizeof (Type))), @encode (Type));
118  jassert (b); (void) b;
119  }
120 
121  template <typename FunctionType>
122  void addMethod (SEL selector, FunctionType callbackFn, const char* signature)
123  {
124  BOOL b = class_addMethod (cls, selector, (IMP) callbackFn, signature);
125  jassert (b); (void) b;
126  }
127 
128  template <typename FunctionType>
129  void addMethod (SEL selector, FunctionType callbackFn, const char* sig1, const char* sig2)
130  {
131  addMethod (selector, callbackFn, (String (sig1) + sig2).toUTF8());
132  }
133 
134  template <typename FunctionType>
135  void addMethod (SEL selector, FunctionType callbackFn, const char* sig1, const char* sig2, const char* sig3)
136  {
137  addMethod (selector, callbackFn, (String (sig1) + sig2 + sig3).toUTF8());
138  }
139 
140  template <typename FunctionType>
141  void addMethod (SEL selector, FunctionType callbackFn, const char* sig1, const char* sig2, const char* sig3, const char* sig4)
142  {
143  addMethod (selector, callbackFn, (String (sig1) + sig2 + sig3 + sig4).toUTF8());
144  }
145 
146  void addProtocol (Protocol* protocol)
147  {
148  BOOL b = class_addProtocol (cls, protocol);
149  jassert (b); (void) b;
150  }
151 
152  #if JUCE_MAC
153  static id sendSuperclassMessage (id self, SEL selector)
154  {
155  objc_super s = { self, [SuperclassType class] };
156  return getMsgSendSuperFn() (&s, selector);
157  }
158  #endif
159 
160  template <typename Type>
161  static Type getIvar (id self, const char* name)
162  {
163  void* v = nullptr;
164  object_getInstanceVariable (self, name, &v);
165  return static_cast <Type> (v);
166  }
167 
168  Class cls;
169 
170 private:
171  static String getRandomisedName (const char* root)
172  {
173  return root + String::toHexString (juce::Random::getSystemRandom().nextInt64());
174  }
175 
177 };
178 
179 
180 #endif // JUCE_OSX_OBJCHELPERS_H_INCLUDED
static String toHexString(int number)
Definition: juce_String.cpp:1925
NSObjectRetainer(ObjectType *o)
Definition: juce_osx_ObjCHelpers.h:84
#define noexcept
Definition: juce_CompilerSupport.h:141
void addProtocol(Protocol *protocol)
Definition: juce_osx_ObjCHelpers.h:146
Definition: juce_CharPointer_UTF8.h:38
Definition: juce_String.h:43
Class cls
Definition: juce_osx_ObjCHelpers.h:168
SuperclassType * createInstance() const
Definition: juce_osx_ObjCHelpers.h:109
ObjCClass(const char *nameRoot)
Definition: juce_osx_ObjCHelpers.h:94
~NSObjectRetainer()
Definition: juce_osx_ObjCHelpers.h:85
static Type getIvar(id self, const char *name)
Definition: juce_osx_ObjCHelpers.h:161
Definition: juce_osx_ObjCHelpers.h:82
Definition: juce_osx_ObjCHelpers.h:92
CharPointer_UTF8 toUTF8() const
Definition: juce_String.cpp:2057
ObjectType * object
Definition: juce_osx_ObjCHelpers.h:87
#define jassert(a)
Definition: juce_PlatformDefs.h:146
void addMethod(SEL selector, FunctionType callbackFn, const char *sig1, const char *sig2)
Definition: juce_osx_ObjCHelpers.h:129
#define JUCE_DECLARE_NON_COPYABLE(className)
Definition: juce_PlatformDefs.h:191
void addMethod(SEL selector, FunctionType callbackFn, const char *signature)
Definition: juce_osx_ObjCHelpers.h:122
void registerClass()
Definition: juce_osx_ObjCHelpers.h:104
static Random & getSystemRandom() noexcept
Definition: juce_core.cpp:65
void addMethod(SEL selector, FunctionType callbackFn, const char *sig1, const char *sig2, const char *sig3)
Definition: juce_osx_ObjCHelpers.h:135
void addMethod(SEL selector, FunctionType callbackFn, const char *sig1, const char *sig2, const char *sig3, const char *sig4)
Definition: juce_osx_ObjCHelpers.h:141
~ObjCClass()
Definition: juce_osx_ObjCHelpers.h:99
void addIvar(const char *name)
Definition: juce_osx_ObjCHelpers.h:115