tan  0.0.1
utils.h
1 #ifndef __TAN_INCLUDE_BASE_UTILS_H__
2 #define __TAN_INCLUDE_BASE_UTILS_H__
3 
4 namespace tanlang {
5 
6 /**
7  * \brief A helper function to cast between pointers types.
8  * This enables safety checks in DEBUG mode but minimal overhead during runtime.
9  * \details Uses dynamic_cast for type checking in DEBUG mode, C-style type casting in RELEASE mode.
10  * \tparam To Target type
11  * \tparam From Optional, source type
12  * \param p Pointer
13  * \return Converted pointer
14  */
15 template <typename To, typename From> To *pcast(From *p) {
16 
17 #ifdef DEBUG
18  auto *ret = dynamic_cast<To *>(p);
19  TAN_ASSERT(ret);
20 #else
21  auto *ret = (To *)p;
22 #endif
23 
24  return ret;
25 }
26 
27 } // namespace tanlang
28 
29 #endif //__TAN_INCLUDE_BASE_UTILS_H__