ResourceTraitsCapstone.hpp 998 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #pragma once
  2. #include <capstone/capstone.h>
  3. #include "ExceptionCapstone.hpp"
  4. namespace ARL::ResourceTraits {
  5. struct CapstoneHandle {
  6. using HandleType = csh;
  7. static inline const HandleType InvalidValue = 0;
  8. [[nodiscard]]
  9. static bool IsValid(const HandleType& Handle) noexcept {
  10. return Handle != InvalidValue;
  11. }
  12. static void Release(HandleType& Handle) {
  13. if (auto err = cs_close(&Handle); err != CS_ERR_OK) {
  14. throw ARL::CapstoneError(__BASE_FILE__, __LINE__, err, "ks_close failed.");
  15. }
  16. }
  17. };
  18. struct CapstoneInsn {
  19. using HandleType = cs_insn*;
  20. static inline const HandleType InvalidValue = nullptr;
  21. [[nodiscard]]
  22. static bool IsValid(const HandleType& Handle) noexcept {
  23. return Handle != InvalidValue;
  24. }
  25. static void Release(const HandleType& Handle) noexcept {
  26. cs_free(Handle, 1);
  27. }
  28. };
  29. }