Nui
class.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "mac_includes.hpp"
4 #include "type_encodings.hpp"
5 
6 namespace Nui::MacOs
7 {
8  template <typename T>
10  {
11  public:
12  static auto createInstance(std::string const& name)
13  {
14  auto cls = objc_getClass(name.c_str());
15  auto inst = class_createInstance(cls, sizeof(T) /* - sizeof(Class)?*/);
16  // here or offset by sizeof(Class)?
17  new (inst) T(cls);
18  return inst;
19  }
20 
21  static auto createNsObjectClassPair(std::string const& name)
22  {
23  return ClassWrapper<T>{objc_allocateClassPair(reinterpret_cast<Class>("NSObject"_cls), name.c_str(), 0)};
24  }
25 
26  Class native() const noexcept
27  {
28  return m_class;
29  }
30 
31  ClassWrapper(Class cls)
32  : m_class{cls}
33  {}
34 
35  void addProtocol(std::string const& name)
36  {
37  class_addProtocol(m_class, objc_getProtocol(name.c_str()));
38  }
39 
41  {
42  objc_registerClassPair(m_class);
43  }
44 
45  template <typename FunctionT>
46  void addMethod(std::string const& name, FunctionT func)
47  {
48  class_addMethod(
49  m_class, sel_registerName(name.c_str()), reinterpret_cast<IMP>(func), encodeType<FunctionT>().c_str());
50  }
51 
52  private:
53  Class m_class;
54  };
55 }
Definition: class.hpp:10
void addProtocol(std::string const &name)
Definition: class.hpp:35
static auto createInstance(std::string const &name)
Definition: class.hpp:12
static auto createNsObjectClassPair(std::string const &name)
Definition: class.hpp:21
void addMethod(std::string const &name, FunctionT func)
Definition: class.hpp:46
Class native() const noexcept
Definition: class.hpp:26
void registerClassPair()
Definition: class.hpp:40
ClassWrapper(Class cls)
Definition: class.hpp:31
Definition: class.hpp:7