diff --git a/src/c_wrapper/utils.h b/src/c_wrapper/utils.h index a851578cdb16f6acc80e1c53763ef319756aa306..40eacbcce2ee255ec6911ced901195245f913fe5 100644 --- a/src/c_wrapper/utils.h +++ b/src/c_wrapper/utils.h @@ -29,6 +29,68 @@ tostring(const T& v) namespace pyopencl { +enum class ArgType { + None, + SizeOf, + Length, +}; + +template +static PYOPENCL_INLINE void +_print_buf_content(std::ostream &stm, const T *p, size_t len) +{ + if (len > 1) { + stm << "["; + } + for (size_t i = 0;i < len;i++) { + stm << p[i]; + if (i != len - 1) { + stm << ", "; + } + } + if (len > 1) { + stm << "]"; + } +} + +template<> +PYOPENCL_INLINE void +_print_buf_content(std::ostream &stm, const char *p, size_t len) +{ + stm << '"'; + stm.write(p, len); + stm << '"'; +} + +template +void +print_buf(std::ostream &stm, const T *p, size_t len, + ArgType arg_type, bool content, bool out) +{ + const size_t ele_size = sizeof(T); + if (out) { + stm << "*(" << (const void*)p << "): "; + _print_buf_content(stm, p, len); + } else { + if (content) { + _print_buf_content(stm, p, len); + stm << " <"; + } + switch (arg_type) { + case ArgType::SizeOf: + stm << ele_size * len << ", "; + case ArgType::Length: + stm << len << ", "; + default: + break; + } + stm << (const void*)p; + if (content) { + stm << ">"; + } + } +} + // TODO template struct CLGenericArgPrinter { @@ -77,12 +139,6 @@ public: } }; -enum class ArgType { - None, - SizeOf, - Length, -}; - template class ArgBuffer { private: @@ -96,7 +152,6 @@ protected: } public: typedef T type; - constexpr static size_t ele_size = sizeof(T); constexpr static ArgType arg_type = AT; ArgBuffer(T *buf, size_t l) noexcept : m_buf(buf), m_len(l) @@ -174,38 +229,6 @@ struct _ArgBufferConverter -static PYOPENCL_INLINE void -_print_buf(std::ostream &stm, Buff &&buff, ArgType arg_type, bool content) -{ - typedef decltype(buff.len()) len_t; - len_t len = buff.len(); - typedef typename std::remove_reference::type _Buff; - size_t ele_size = _Buff::ele_size; - if (content) { - stm << "["; - for (len_t i = 0;i < len;i++) { - stm << buff.get()[i]; - if (i != len - 1) { - stm << ", "; - } - } - stm << "] <"; - } - switch (arg_type) { - case ArgType::SizeOf: - stm << ele_size * len << ", "; - case ArgType::Length: - stm << len << ", "; - default: - break; - } - stm << buff.get(); - if (content) { - stm << ">"; - } -} - template class CLArg class pyopencl_buf : public std::unique_ptr > { size_t m_len; public: - constexpr static size_t ele_size = sizeof(T); PYOPENCL_INLINE pyopencl_buf(size_t len=1) : std::unique_ptr >((T*)(len ? malloc(sizeof(T) * (len + 1)) : @@ -413,7 +436,8 @@ public: PYOPENCL_INLINE void print(std::ostream &stm, bool out=false) { - _print_buf(stm, m_buff, ArgType::Length, out || !is_out); + print_buf(stm, m_buff.get(), m_buff.len(), ArgType::Length, + out || !is_out, out); } };