WIP: how to define the bool conversion / validity check for the function holders?

This commit is contained in:
Fischlurch 2009-06-26 05:27:54 +02:00
parent e91cdd39e9
commit daeff6f5fd
2 changed files with 55 additions and 7 deletions

View file

@ -93,7 +93,24 @@ namespace typelist{
/* ====== Policy classes ====== */
template<class T>
struct BoolCheckable
{
typedef bool (T::*ValidityCheck)() const;
typedef ValidityCheck _unspecified_bool_type;
ValidityCheck isValid;
BoolCheckable() : isValid (&T::isValid) {}
/** implicit conversion to "bool" */
operator _unspecified_bool_type() const { T const& obj = *this;
return (obj.*isValid)()? isValid : 0; } // never throws
bool operator! () const { T const& obj = *this;
return !(obj.*isValid)(); } // ditto
};
/**
* Policy for FunErasure: store an embedded tr1::function
* Using this policy allows to store arbitrary complex functor objects
@ -101,6 +118,7 @@ namespace typelist{
* The price to pay is vtable access and heap storage of function arguments.
*/
class StoreFunction
// : public BoolCheckable<StoreFunction>
{
/** Helper: type erasure */
struct Holder
@ -155,6 +173,12 @@ namespace typelist{
REQUIRE (INSTANCEOF (FunctionHolder<SIG>, &holder_));
return static_cast<FunctionHolder<SIG>&> (holder_).get();
}
bool
isValid() const
{
return reinterpret_cast<void*> (holder_.storage_[0]);
}
};

View file

@ -12,16 +12,20 @@
// 5/08 - how to guard a downcasting access, so it is compiled in only if the involved types are convertible
// 7/08 - combining partial specialisation and subclasses
// 10/8 - abusing the STL containers to hold noncopyable values
// 6/09 - investigating how to build a mixin template providing an operator bool()
#include <nobug.h>
//#include "include/nobugcfg.h"
#include "lib/meta/function-erasure.hpp"
#include <iostream>
//#include <typeinfo>
#include <boost/format.hpp>
//#include <boost/noncopyable.hpp>
#include <cstdlib>
using std::rand;
using std::string;
using std::cout;
using boost::format;
@ -32,6 +36,24 @@ using boost::format;
long checksum = 0;
}
struct TestIt1
: lumiera::typelist::BoolCheckable<TestIt1>
{
int val_;
TestIt1 (int v = (rand() % 10))
: val_(v)
{ }
bool
isValid() const
{
return true;
}
};
int
@ -39,13 +61,15 @@ main (int argc, char* argv[])
{
NOBUG_INIT;
TestIt1 testrosteron (22);
bool boo = true; //testrosteron;
if (!boo)
return -1;
cout << "\n.gulp.\n";
int * p = 0;
int oh = *p;
return 0;
}