Empirical
Classes | Namespaces | Enumerations | Functions | Variables
Ptr.h File Reference

A wrapper for pointers that does careful memory tracking (but only in debug mode). More...

#include <unordered_map>
#include "assert.h"
#include "vector.h"

Go to the source code of this file.

Classes

class  emp::PtrInfo
 
class  emp::PtrTracker
 Facilitate tracking of all Ptr objects in this run. More...
 
class  emp::Ptr< TYPE >
 
struct  emp::Ptr< TYPE >::hash_t
 

Namespaces

 emp
 If we are in emscripten, make sure to include the header.
 
 emp::internal
 

Enumerations

enum  emp::PtrStatus { emp::PtrStatus::DELETED =0, emp::PtrStatus::ACTIVE, emp::PtrStatus::ARRAY }
 

Functions

static constexpr size_t emp::internal::Log2 (size_t x)
 An anonymous log2 calculator for hashing below. More...
 
void emp::SetPtrDebug (bool _d=true)
 
bool emp::GetPtrDebug ()
 
template<typename T >
std::ostream & emp::operator<< (std::ostream &out, const emp::Ptr< T > &ptr)
 
template<typename T , typename... Ts>
std::istream & emp::operator>> (std::istream &is, emp::Ptr< T > &ptr)
 
template<typename T >
Ptr< T > emp::ToPtr (T *_in, bool own=false)
 Convert a T* to a Ptr<T>. By default, don't track. More...
 
template<typename T >
Ptr< T > emp::TrackPtr (T *_in, bool own=true)
 Convert a T* to a Ptr<T> that we DO track. More...
 
template<typename T , typename... ARGS>
Ptr< T > emp::NewPtr (ARGS &&...args)
 Create a new Ptr of the target type; use the args in the constructor. More...
 
template<typename T >
Ptr< T > emp::CopyPtr (Ptr< T > in)
 Copy an object pointed to and return a Ptr to the copy. More...
 
template<typename T >
emp::vector< Ptr< T > > emp::CopyPtrs (const emp::vector< Ptr< T >> &in)
 Copy a vector of objects pointed to; return a vector of Ptrs to the new copies. More...
 
template<typename T >
emp::vector< Ptr< T > > emp::ClonePtrs (const emp::vector< Ptr< T >> &in)
 Copy a vector of objects pointed to by using their Clone() member function; return vector. More...
 
template<typename T , typename... ARGS>
Ptr< T > emp::NewArrayPtr (size_t array_size, ARGS &&...args)
 Create a pointer to an array of objects. More...
 

Variables

static bool emp::internal::ptr_debug = false
 

Detailed Description

A wrapper for pointers that does careful memory tracking (but only in debug mode).

Note
This file is part of Empirical, https://github.com/devosoft/Empirical
Date
2016-2018
Note
Status: BETA

Ptr objects behave as normal pointers under most conditions. However, if a program is compiled with EMP_TRACK_MEM set, then these pointers perform extra tests to ensure that they point to valid memory and that memory is freed before pointers are released.

If you trip an assert, you can re-do the run a track a specific pointer by defining EMP_ABORT_PTR_NEW or EMP_ABORT_PTR_DELETE to the ID of the pointer in question. This will allow you to track the pointer more easily in a debugger.

Todo:

Track information about emp::vector and emp::array objects to make sure we don't point directly into them? (A resize() could make such pointers invalid!) Or better, warn it vector memory could have moved.

Get working with threads