AMDG
Scott McMurray wrote:
On Fri, Aug 8, 2008 at 14:13, Jesse Perla wrote:
Is there any way to get a normal function pointer out of a boost::function
(with its state and potential bindings)? I want to have the option of
calling existing optimization routines that are not functor aware.
If all you can pass is a, say,
double(*)(double), then you might be somewhat out of luck when it
comes to nice solutions. You can always use global variables to pass
the state, but those are obviously not nice.)
Here's a quick hack that hides the global variables:
#include <map>
#include <vector>
#include
#include
#include
#include
#include
#include
#include
#include
template
struct function_holder;
#define BOOST_PP_LOCAL_MACRO(n) \
\
template \
struct function_holder { \
static boost::function impl; \
static R function(BOOST_PP_ENUM_BINARY_PARAMS(n, T, t)) { \
return(impl(BOOST_PP_ENUM_PARAMS(n, t))); \
} \
}; \
\
template \
boost::function \
function_holder::impl;
#define BOOST_PP_LOCAL_LIMITS (0, 10)
#include BOOST_PP_LOCAL_ITERATE()
template
struct function_pool {
static std::map allocated_functions;
static std::vector >
free_functions;
struct insert_free_function {
template<class N>
void operator()(const N&) const {
free_functions.push_back(std::make_pair(
&function_holder >::function,
&function_holder >::impl));
}
};
static void init() {
boost::mpl::for_each
(insert_free_function());
}
static Sig* allocate(const boost::function<Sig>& f) {
std::pair result =
free_functions.back();
*result.second = f;
allocated_functions.insert(result);
free_functions.pop_back();
return(result.first);
}
static void free(Sig* victim) {
typename std::map::iterator pos =
allocated_functions.find(victim);
free_functions.push_back(*pos);
allocated_functions.erase(pos);
}
};
template
std::map function_pool::allocated_functions;
template
std::vector > function_pool::free_functions;
#include <iostream>
struct pool : function_pool {};
struct hello {
void operator()(int) const {
std::cout << "Hello, ";
}
};
struct world {
void operator()(int) const {
std::cout << "World!" << std::endl;
}
};
int main() {
pool::init();
void (*hello_function)(int) = pool::allocate(hello());
void (*world_function)(int) = pool::allocate(world());
hello_function(1);
world_function(1);
pool::free(hello_function);
pool::free(world_function);
}
In Christ,
Steven Watanabe