473,466 Members | 1,370 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Generic Pointer Type


For objects, we have "void*" as the generic pointer type. For instance:

enum ParamType { Int, Double };

void Func(void *const p,ParamType const pt)
{
switch (pt)
{
case Int: *(int*)p = 42; break;
case Double: *(double*)p = 42; break;
}
}

However, we're not so lucky when it comes to function pointer types. The
behaviour of the following snippet is undefined:

enum ParamType { Int, Double };

void Func(void *const p,ParamType const pt)
{
switch (pt)
{
case Int: ((void(*)(int))p)(42); break;
case Double: ((void(*)(double))p)(42); break;
}
}

I thought I might go about writing a class which would serve as a generic
function pointer. As this was just a pet project, I thought I'd make its
behaviour as well-defined as possible (to the extreme just for the craic!).
I'm designing the class in such a way that it should behave exactly like an
intrinsic function pointer type. I've also taken some liberties in writing
the code, e.g. the behaviour is undefined if you assign from an
uninitialised FuncPtr object. Here's what I've got at the moment:

#include <cstddef>
#include <cstdlib>
#include <cstring>
#include <new>

class FuncPtr {
private:

std::size_t quant; /* Size in bytes of function pointer */
void *pbytes;

public:

FuncPtr() : pbytes() {}

template<class T>
FuncPtr(T const p) : quant(sizeof p)
{
pbytes = std::malloc(quant);
if (!pbytes) throw std::bad_alloc();

std::memcpy(pbytes,&p,quant);
}

FuncPtr(FuncPtr const &a) : quant(a.quant)
{
pbytes = std::malloc(quant);
if (!pbytes) throw std::bad_alloc();

std::memcpy(pbytes,a.pbytes,quant);
}

FuncPtr &operator=(FuncPtr const &a)
{
quant = a.quant;

pbytes = std::realloc(pbytes,quant);
if (!pbytes) throw std::bad_alloc();

std::memcpy(pbytes,a.pbytes,quant);

return *this;
}

~FuncPtr() { free(pbytes); }

template<class T>
operator T() const
{
return *(T*)pbytes;
}
};

enum ParamType { Int, Double };

void Foo(FuncPtr const p,ParamType const pt)
{
switch (pt)
{
case Int: ((void(*)(int))p)(5); break;
case Double: ((void(*)(double))p)(5); break;
}
}

#include <iostream>
#include <ostream>

void FuncI(int) { std::cout << "Int function called" << std::endl; }
void FuncD(double) { std::cout << "Double function called" << std::endl; }

int main()
{
FuncPtr a;
FuncPtr const b = FuncD;

a = b;

Foo(a,Double);

Foo(FuncI,Int);
}
I realise it's a bit much to use dynamic memory allocation, but it's sort
of the only way I could _guarantee_ the correct alignment and size (short
of playing around with unions and what have you).

Anywho I just thought I'd throw that out there to see what kind of
responses I get.

--

Frederick Gotham
Nov 6 '06 #1
3 1728
Frederick Gotham:
template<class T>
operator T() const
{
return *(T*)pbytes;
}

That would be more politically correct as:

return *(T const*)pbytes;

(although it doesn't make a difference).

--

Frederick Gotham
Nov 6 '06 #2

Frederick Gotham wrote:
I thought I might go about writing a class which would serve as a generic
function pointer. As this was just a pet project, I thought I'd make its
behaviour as well-defined as possible (to the extreme just for the craic!).
I'm designing the class in such a way that it should behave exactly like an
intrinsic function pointer type. I've also taken some liberties in writing
the code, e.g. the behaviour is undefined if you assign from an
uninitialised FuncPtr object.
Not to rain on your parade man but boost has this already. Check out
the function library. It will be in the next C++ standard and works
with things like bind, lambda, and all the algorithms in the std lib.
It's a pretty good wheel. Your time might be spent better on something
else.

Nov 6 '06 #3

Noah Roberts wrote:
Frederick Gotham wrote:
I thought I might go about writing a class which would serve as a generic
function pointer. As this was just a pet project, I thought I'd make its
behaviour as well-defined as possible (to the extreme just for the craic!).
I'm designing the class in such a way that it should behave exactly like an
intrinsic function pointer type. I've also taken some liberties in writing
the code, e.g. the behaviour is undefined if you assign from an
uninitialised FuncPtr object.

Not to rain on your parade man but boost has this already. Check out
the function library. It will be in the next C++ standard and works
with things like bind, lambda, and all the algorithms in the std lib.
It's a pretty good wheel. Your time might be spent better on something
else.
Here's your original problem solved with boost::function:
#include <iostream>
#include <boost/any.hpp>

#include <boost/function.hpp>

void f1(int x) { std::cout << "void f1(int).\n"; }
void f2(double x) { std::cout << "void f2(double).\n"; }
int f3(double x) { std::cout << "int f3(double).\n"; return
static_cast<int>(x); }

void Func(boost::function<void (int)const & f)
{
f(42);
}

struct functor
{
void operator() (int x) { std::cout << "void functor::()(int).\n"; }
};

int main()
{
boost::function<void (int)f;

f = &f1;
Func(f);

f = &f2;
Func(f);

f = &f3;
Func(f);

Func(&f3);

functor fun;
Func(fun);

int y; std::cin >y;
return 0;
}

f can be assigned any invocable object that can be called with an int
as parameter and returns an object convertable to void. Func can be
used the same.

Nov 6 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: aurgathor | last post by:
Howdy, How do I pass some function a generic comparison function? I figured out one non-generic case, but since this code got parameter declarations in two places, it's obviously not generic....
1
by: Albert | last post by:
Hello all, I've got several questions and I'm sure you'll find them easy to answer, but sadly I don't know the answers though: What do people mean by generic pointers Why is a pointer to void...
11
by: redefined.horizons | last post by:
First, I would thank all of those that took the time to answer my question about creating an array based on a numeric value stored in a variable. I realize after reading the responses and doing...
3
by: markww | last post by:
Hi, I have a wrapper around some 3rd party database library function. The pseudo code looks like the following - it is meant to open a table in a database, extract values from a table, then copy...
4
by: Andrew Ducker | last post by:
I have a collection of classes descending from a single root class (let's call it RootClass). They all currently have a property of Logical, of type Logical. However they actually return a...
4
by: palani12kumar | last post by:
Can any one tell me what is generic poiner? im struggling a lot to know what it is?
1
by: interX | last post by:
Hi I'm new in VC++ and have a question to generics. I have a generic class, which contains an array of the generic type. This array I can pin and then I would like to get an unmanaged pointer to...
7
by: juerg.lemke | last post by:
Hi everyone I am interested in having multiple functions with different prototypes and deciding, by setting a pointer, which of them to use later in the program. Eg: int f1(void); char*...
32
by: copx | last post by:
Why doesn't the C standard include generic function pointers? I use function pointers a lot and the lack of generic ones is not so cool. There is a common compiler extension (supported by GCC...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.