helper.hpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. #pragma once
  2. namespace variant { namespace detail {
  3. template <typename... Ts> struct helper;
  4. template <typename F, typename... Ts> struct helper<F, Ts...> {
  5. inline static void destroy(size_t id, void * data) {
  6. if (id == sizeof...(Ts)) {
  7. reinterpret_cast<F *>(data)->~F();
  8. } else {
  9. helper<Ts...>::destroy(id, data);
  10. }
  11. }
  12. inline static void move(size_t old_t, void * old_v, void * new_v) {
  13. if (old_t == sizeof...(Ts)) {
  14. new (new_v) F(std::move(*reinterpret_cast<F *>(old_v)));
  15. } else {
  16. helper<Ts...>::move(old_t, old_v, new_v);
  17. }
  18. }
  19. inline static void copy(size_t old_t, const void * old_v, void * new_v) {
  20. if (old_t == sizeof...(Ts)) {
  21. new (new_v) F(*reinterpret_cast<const F *>(old_v));
  22. } else {
  23. helper<Ts...>::copy(old_t, old_v, new_v);
  24. }
  25. }
  26. };
  27. template <> struct helper<> {
  28. inline static void destroy(size_t, void *) {}
  29. inline static void move(size_t, void *, void *) {}
  30. inline static void copy(size_t, const void *, void *) {}
  31. };
  32. }}