Skip to content

Commit

Permalink
Add debug output for CopyablePtr
Browse files Browse the repository at this point in the history
  • Loading branch information
hakonhagland committed Jan 29, 2024
1 parent 1224f8c commit 8de0893
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion opm/utility/CopyablePtr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@

#ifndef OPM_COPYABLE_PTR_HPP
#define OPM_COPYABLE_PTR_HPP

#include <iostream>
#include <cstdlib>

namespace Opm {
namespace Utility {
// Wraps std::unique_ptr and makes it copyable.
Expand All @@ -29,13 +33,28 @@ namespace Utility {
template <class T>
class CopyablePtr {
public:
CopyablePtr() : ptr_(nullptr) {}
CopyablePtr() : ptr_(nullptr) {
if (isDebugEnabled()) {
std::cout << "CopyablePtr Default Constructed: ptr_ = nullptr" << std::endl;
}
}
CopyablePtr(const CopyablePtr& other) {
if (other) { // other does not contain a nullptr
ptr_ = std::make_unique<T>(*other.get());
if (isDebugEnabled()) {
std::cout << "CopyablePtr Copied: New ptr_ = " << ptr_.get() << std::endl;
}
}
else {
ptr_ = nullptr;
if (isDebugEnabled()) {
std::cout << "CopyablePtr Copied: ptr_ = nullptr (from nullptr)" << std::endl;
}
}
}
~CopyablePtr() {
if (isDebugEnabled()) {
std::cout << "CopyablePtr Destructed: Freeing ptr_ = " << ptr_.get() << std::endl;
}
}
// assignment operator
Expand Down Expand Up @@ -64,6 +83,11 @@ class CopyablePtr {
T* release() const {return ptr_.release();}
private:
std::unique_ptr<T> ptr_;

static bool isDebugEnabled() {
const char* debugEnv = std::getenv("OPM_DEBUG");
return debugEnv && std::string(debugEnv) == "1";
}
};

} // namespace Utility
Expand Down

0 comments on commit 8de0893

Please sign in to comment.