Cpp-Processing
pointer-utils.h
1 #pragma once
2 #include <memory>
3 #include <stdexcept>
4 #include <sstream>
5 
6 #ifndef NDEBUG
7 // Utility function to safely dereference a unique_ptr (debug builds only)
8 #pragma message("Compiling with debug checks for unique_ptr dereferencing")
9 template <typename T>
10 T* safe_dereference_impl(const std::unique_ptr<T>& ptr, const char* file, int line) {
11  if (!ptr) {
12  std::ostringstream oss;
13  oss << "Null pointer dereference at " << file << ":" << line;
14  throw std::runtime_error(oss.str());
15  }
16  return ptr.get();
17 }
18 
19 // Macro to include runtime checks in debug builds
20 #define sf_deref(ptr) safe_dereference_impl(ptr, __FILE__, __LINE__)
21 #else
22 // No-op in release builds
23 #define sf_deref(ptr) (ptr.get())
24 #endif
25