Skip to content

Instantly share code, notes, and snippets.

@dk949
Last active February 25, 2025 10:50
Show Gist options
  • Save dk949/eb6d451f925f196480032f3e8ad14cce to your computer and use it in GitHub Desktop.
Save dk949/eb6d451f925f196480032f3e8ad14cce to your computer and use it in GitHub Desktop.
#ifndef UT_CHECK_HPP
#define UT_CHECK_HPP
/// Class for checking when a c++ object is constructed, copied, moved from or destructed
/* Usage:
*
* std >= c++11
*
* #include "check.hpp"
*
* #include <vector>
* #define EXAMPLE_1
*
* int main() {
* #if defined(EXAMPLE_1)
* {
* std::vector<ut::Check<int>> vi;
* vi.emplace_back(3);
* }
* // Output:
* // Check(i);[i0]
* // ~Check();[i0]
*
* #elif defined(EXAMPLE_2)
* {
* std::vector<ut::Check<int>> vi;
* vi.push_back(3);
* }
* // Output:
* // Check(i);[i0]
* // Check(Check &&);[i1]
* // ~Check();[i0]
* // ~Check();[i1]
*
* #endif
* }
*/
#include <iostream>
#include <utility>
#if __cplusplus >= 202'002L
# include <format>
# include <source_location>
#endif
namespace ut {
template<typename T>
class Check {
private:
static int counter;
int objectNum = 0;
T m_data;
public:
Check() noexcept(std::is_nothrow_default_constructible<T>::value)
: m_data() {
objectNum = counter;
counter++;
std::cout << "Check();[" << typeid(T).name()[0] << objectNum << "]\n";
}
template<typename Tt>
Check(Tt other_data) noexcept(std::is_nothrow_move_constructible<T>::value)
: m_data(std::move(other_data)) {
static_assert(std::is_same<Tt, T>::value, "Implicit conversion to T disallowed in constructor");
objectNum = counter;
counter++;
std::cout << "Check(" << typeid(m_data).name() << ");[" << typeid(T).name()[0] << objectNum << "]\n";
}
// NOLINTNEXTLINE(google-explicit-constructor)
operator T() const noexcept {
ubCheck();
return m_data;
}
Check(Check const &other) noexcept(std::is_nothrow_copy_constructible<T>::value)
: m_data(other.m_data) {
objectNum = counter++;
std::cout << "Check(const Check &);[" << typeid(T).name()[0] << objectNum << "]\n";
}
Check(Check &&other) noexcept(std::is_nothrow_move_constructible<T>::value)
: m_data(std::move(other.m_data)) {
objectNum = counter;
counter++;
std::cout << "Check(Check &&);[" << typeid(T).name()[0] << objectNum << "]\n";
}
Check &operator=(Check const &other) noexcept(std::is_nothrow_copy_assignable<T>::value) {
ubCheck();
std::cout << "Check &operator=(Check);[" << typeid(T).name()[0] << objectNum << "]\n";
m_data = other.m_data;
return *this;
}
Check &operator=(Check &&other) noexcept(std::is_nothrow_move_assignable<T>::value) {
ubCheck();
std::cout << "Check &operator=(Check &&);[" << typeid(T).name()[0] << objectNum << "]\n";
m_data = std::move(other.m_data);
return *this;
}
~Check() noexcept(std::is_nothrow_destructible<T>::value) {
ubCheck();
std::cout << "~Check();[" << typeid(T).name()[0] << objectNum << "]\n";
objectNum = -1;
}
T const &data() const noexcept {
ubCheck();
return m_data;
}
T &data() noexcept {
ubCheck();
return m_data;
}
private:
void ubCheck(
#if __cplusplus >= 202'002L
std::source_location loc = std::source_location::current()
#endif
) const noexcept {
if (objectNum < 0) {
std::cerr << "Undefined behaviour: Use after destructor"
#if __cplusplus >= 202'002L
<< " in function `" << loc.function_name() << '`'
#endif
<< '\n';
std::exit(-1);
}
}
};
template<typename U>
std::ostream &operator<<(std::ostream &os, Check<U> const &i) noexcept(noexcept(os << i.data())) {
os << i.data();
return os;
}
template<typename T>
int Check<T>::counter = 0;
using ICheck = Check<int>;
} // namespace ut
#if __cplusplus >= 202'002L
template<typename D, class CharT>
struct std::formatter<ut::Check<D>, CharT> : public std::formatter<D, CharT> {
template<class FormatContext>
auto format(ut::Check<D> const &c, FormatContext &fc) const {
auto out = fc.out();
return std::format_to(out, "{}", c.data());
}
};
#endif
/*
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <https://unlicense.org>
*/
#endif // UT_CHECK_HPP
type which prints when it is constructed/copied/moved/destructed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment