instance.hpp 598 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #pragma once
  2. namespace nall {
  3. template<typename T>
  4. struct Instance {
  5. ~Instance() {
  6. destruct();
  7. }
  8. auto operator()() -> T& {
  9. return instance.object;
  10. }
  11. template<typename... P>
  12. auto construct(P&&... p) {
  13. if(constructed) return;
  14. constructed = true;
  15. new((void*)(&instance.object)) T(forward<P>(p)...);
  16. }
  17. auto destruct() -> void {
  18. if(!constructed) return;
  19. constructed = false;
  20. instance.object.~T();
  21. }
  22. private:
  23. bool constructed = false;
  24. union Union {
  25. Union() {}
  26. ~Union() {}
  27. T object;
  28. char storage[sizeof(T)];
  29. } instance;
  30. };
  31. }