hollyhock
util.hpp
1 #pragma once
2 #include <stdint.h>
3 
4 // No point in documenting these macros. If people are trying to use them,
5 // they're doing something weird and I don't want to help them :)
6 // If you need to know what they do, look at how they're used.
8 
17 #define VTABLE_FAKE_ENTRY(n, x) uint32_t fakeentry##x[n * 3]
18 
29 template<typename TReturn, typename ...TArgs>
30 struct VTableFunction {
31  int32_t offset;
32  uint32_t unused;
33  TReturn (*func)(void *self, TArgs...);
34 
35  TReturn operator()(void *self, TArgs... args) {
36  void *self2 = reinterpret_cast<void *>(
37  reinterpret_cast<uint8_t *>(self) + offset
38  );
39 
40  return func(self2, args...);
41  }
42 };
44 
48 class Wrapped {
49 public:
56  template <typename T>
57  T *GetWrapped() {
58  return static_cast<T *>(m_wrapped);
59  }
60 
61 protected:
62  Wrapped() = default;
63  ~Wrapped() = default;
64 
65  // Since we use the trick with the vtable to overide functions (@c .me), we
66  // can't let the object's address change. Prevent that by deleting the copy
67  // constructor and assignment operator.
68  Wrapped(Wrapped const &) = delete;
69  void operator=(Wrapped const &) = delete;
70 
72  void *m_wrapped;
73 };
74 
78 class GUIElement : public Wrapped {};
Definition: util.hpp:78
Definition: util.hpp:48
void * m_wrapped
A pointer to the wrapped class.
Definition: util.hpp:72
T * GetWrapped()
Definition: util.hpp:57