On Tuesday, 19 March 2013 21:41:29 UTC+2, DeMarcus wrote:
> To go straight to my problem, I have a class that takes a handle and
> a function with how to deallocate the handle, like this.
>
> class HandleHolder
> {
> public:
> HandleHolder( int handle, std::function<void(int)> deallocator );
> };
>
> I /can/ use it like this.
>
> HandleHolder myHandleHolder( someHandle, nullptr );
May be first write what you want to have:
/// Try_HandleHolder.cpp
#include "HandleHolder.hpp"
#include <iostream> // cout
void close( int i ) {
std::cout << "int handle " << i << " has been CLOSED!" << std::endl;
}
void erase( float f ) {
std::cout << "float handle " << f << " has been ERASED!" <<
std::endl;
}
int main() {
// Should be template and all that
typedef HandleHolder<float> FloatHandle;
FloatHandle h0( 0.0f, erase );
typedef HandleHolder<int> IntHandle;
// Telling that I do not want deallocator should work
IntHandle h1( 1, IntHandle::NoDeleter );
// Providing deallocator should work
IntHandle h2( 2, close );
// Providing nothing should work without deallocator
IntHandle h3( 3 );
// Providing nullptr should not compile; uncomment to check it
// IntHandle h4( 4, nullptr );
// Providing nullptr old way should not compile; uncomment to
check it
// IntHandle h5( 5, 0 );
}
Not sure if that is actually what you want but ... you get the idea.
Then try to write a class that supposedly does it:
/// HandleHolder.hpp
/// This code was in Usenet post of �� Tiib as mere illustration.
#include <iostream> // cout
#include <functional> // function
template<typename T>
struct HandleHolder {
typedef std::function<void( T )> Deleter;
static Deleter const NoDeleter;
explicit HandleHolder( T v, Deleter f = NoDeleter )
: value( v )
, deleter( f )
{}
explicit HandleHolder( T v, std::nullptr_t ) = delete;
~HandleHolder() {
if ( deleter != nullptr ) {
std::cout << "deallocator of "
<< value << " PRESENT!" << std::endl;
deleter( value );
}
else {
std::cout << "deallocator of "
<< value << " MISSING!" << std::endl;
}
}
private:
explicit HandleHolder( T v, std::nullptr_t );
T value;
Deleter deleter;
};
template <class T>
typename HandleHolder<T>::Deleter const HandleHolder<T>::NoDeleter =
nullptr;
Again not sure if it actually runs now at all but ... you get the
idea.
Then post your problem if you get stuck.
We are otherwise in difficulties to see that Entertainers are actually
deallocators (that possibly are needed in destructors) and that the
watchShows are actually constructors.