|
Boost Users : |
Subject: Re: [Boost-users] [function] Boost.Function won't compile under Borland DevStudio 2006
From: OvermindDL1 (overminddl1_at_[hidden])
Date: 2010-04-13 15:29:28
2010/4/13 Fábio 'Petrucio' Stange <petrucio_at_[hidden]>:
>
>> 2010/4/13 Fábio 'Petrucio' Stange <petrucio_at_[hidden]>:
>> Â But it otherwise works and runs just fine. Â Based on the error you
>> posted in your first post, that is perfectly valid code, not sure what
>> could be upsetting it, and not sure how it could be fixed without
>> slowing down the code (which would be fine if you do not mind that),
>>
>
> I don't mind that.
Well the initial error appears to just be instancing problem. Might
put a static bool set to false on init, test it after the fact, and if
false set it to true and also set the struct above it, that incurs
obvious overhead, and as stated, there might be more problems as well.
2010/4/13 Fábio 'Petrucio' Stange <petrucio_at_[hidden]>:
>> but there might still be further issues, especially if this compiler
>> has issues with something as simple as boost::function. Â I do not see
>> how this could easily be worked around, that compiler is unsupported
>> by just about everything in existence due to how non-compliant it has
>> always been (it even makes VC6 look intelligent by comparison), but
>> perhaps just a plain function pointer would work fine if you do not
>> need the enhanced capabilities of boost::function?
>>
>
> Yes, I've been using function pointers for awhile, but boost::function does
> give me some extra leeway in defining callback functions inside classes
> which I very much would like to use.
>
> If a function requires a void(*callback)() function pointer, is there any
> other way I can pass it a function declared
> void FooBar::Callback() {}
> without this function being a static member? That's what I was trying to get
> boost::bind and boost::function to do.
Then you could do something like:
struct voidCallbackHandler0 : private boost::noncopyable { // do not
need the boost::noncopyable part if you add a private copy constructor
to prevent accidental copying
virtual ~voidCallbackHandler0() {}
virtual void operator()() = 0;
}
boost::shared_ptr<voidCallbackHandler0> voidCallbackHandler0Ptr; // if
shared_ptr does not work for you either, handle your own reference
counting in voidCallbackHandler0 itself and make *SURE* to only pass
around pointers, or no reference counting but just be sure to not leak
memory and so forth
And use it like:
#include <theAboveCallbackHandler.h>
#include <iostream.h>
void callThing(voidCallbackHandler0 callback) {
(*callback)();
}
class FooBar {
public:
void Callback(){std::cout << "Called back ok\n";}
}
struct myFooBarCallbackCaller : public voidCallbackHandler0 {
FooBar *self;
void operator()() {inst->Callback();)
}
// Or hey, make a callback inline
struct myInlineCallbackCaller : public voidCallbackHandler0 {
void operator()() {std::cout << "Inline called back ok\n";)
}
int main(int argc, char* argv[])
{
voidCallbackHandler0Ptr callback1(new myFooBarCallbackCaller);
FooBar fb;
callback1.self = &fb;
FooBar(callback1);
// or many other ways:
voidCallbackHandler0Ptr callback2(new myInlineCallbackCaller);
FooBar(callback2);
return 0;
}
Obviously need to customize them for each type of callback you will
need, and there are *much* better ways to do this, more reusable, and
faster, but that requires decent template support (unsure how well
that compiler works), and you pretty much end up remaking boost
function/bind anyway. This is not the fastest or cleanest way, but it
should work, and you only need memory management if *any* of the
subclasses can hold state (the 'self') and/or they need to be held
longer then the scope of the function call.
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net