473,739 Members | 6,655 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dealing with callbacks

I'm looking for ideas on approaches to dealing with a type of
problem...

Say you have a project with several thousands of lines of code that
uses a certain C API for GUI work (really doesn't matter which one, the
problem is across the board). You of course have much code in which
you are casting objects to void* in order that they can be passed
through the API into some callback function, where you will be casting
from void* back into whatever type you expect.

Here's where the fun comes in...

Much has changed in this code over the years and particular objects
that are commonly cast around have become quite large; there's a lot of
monolithic objects and superfluous inheritance that is causing
problems. You would like to split them up into different higherarchies
and interfaces so that functions that use some subset of the interface
for that object can just respond to a new interface that is composed of
that functionality. This ends up introducing MI into your code.

The question of what to cast to/from really came up a long time
ago...many years ago probably...but nobody really noticed because MI
wasn't an issue so things just kind of worked even though UB is all
over the place.

So you have tons of code that is rather careless about using
reinterpret_cas ts so as soon as you start changing things the way you
would like to things go up shit creek really fast. The compiler of
course won't tell you anything...you can't do an accurate cast at the
other side because the objects passed in have various differing
locations for their bases...

So, what would you do? I'm kind of at a loss as to how to go about
solving this. It doesn't help that the standard cast before I came
along and said it was bad was to use C-style casts...so finding
reinterpret_cas ts isn't going to be easy...

The big picture is actually quite a bit worse than what I am describing
but I'm not looking for consulting. What kind of things have people
done who've experienced something resembling the problem I find myself
in?

Dec 15 '06 #1
11 1253
* Noah Roberts:
I'm looking for ideas on approaches to dealing with a type of
problem...

Say you have a project with several thousands of lines of code that
uses a certain C API for GUI work (really doesn't matter which one, the
problem is across the board). You of course have much code in which
you are casting objects to void* in order that they can be passed
through the API into some callback function
No.

>, where you will be casting
from void* back into whatever type you expect.
[snip]
>
The big picture is actually quite a bit worse than what I am describing
but I'm not looking for consulting. What kind of things have people
done who've experienced something resembling the problem I find myself
in?
It's a management decision: continue to add life-support to the old dog,
or take him to the vet, and/or get a new puppy (not as clever and
familiar initially, and requires some training)?

I have the feeling you're talking about giving the dog a blood infusion
and perhaps transplanting some organs and removing some tumors, to make
it more lively and able to do new tricks.

Ach! An old dog is an old dog. Ever heard about teaching olds new
tricks? Difficult, that is. Perhaps even as difficult as teaching the
old dog not to bark or do other undesirable things.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Dec 15 '06 #2
Noah Roberts wrote:
So you have tons of code that is rather careless about using
reinterpret_cas ts so as soon as you start changing things the way you
would like to things go up shit creek really fast. The compiler of
course won't tell you anything...you can't do an accurate cast at the
other side because the objects passed in have various differing
locations for their bases...
could you do:
static_cast<voi d*>(static_cast <SomeBase*>(&co mplexObj))
when initiating the callback and:
dynamic_cast<Co mplexType*>(sta tic_cast<SomeBa se*>(voidPtr))
on the receiving end of the callback?

The idea being that the cast to SomeBase* is always valid (perhaps you
have a CObject or something like it as a virtual base class?) and that
the dynamic_cast could be tested at runtime or at least asserted upon.

Just an idea. And I know it's still a lot of manual, error-prone work.
I can't think of any easy way to avoid it off the top of my head.

One other tip: I find that if somePtr is a pointer that was obtained
from a bad cast, accessing its typeid (eg:
assert(typeid(* somePtr).name() )) will often segfault immediately rather
than letting the program limp on with undefined behavior.

Dec 15 '06 #3

Noah Roberts wrote:
The big picture is actually quite a bit worse than what I am describing
but I'm not looking for consulting. What kind of things have people
done who've experienced something resembling the problem I find myself
in?
Put in a request to do an audit of your current system?

regards
Andy Little

Dec 15 '06 #4
dex
This is overkill, but part of it may come in handy should you actually
choose to try and refactor your classes using multiple inheritance.
class Derived_MI;
typedef void *Pvoid;
typedef Derived_MI* PDerived_MI;

// shim pointer class to replace any pointer
// created via the 'address of' operator
class Derived_MI_ptr
{
private:
PDerived_MI m_p;

public:
Derived_MI_ptr( PDerived_MI p = NULL) : m_p(p)
{}

Derived_MI_ptr( Derived_MI_ptr &that)
{
if (this != &that)
{
m_p = that.m_p;
}
}

// pass through for the -operator
// but you shouldn't need this for the process of
// finding all the casts in your code
PDerived_MI operator->()
{
return m_p;
}

operator PDerived_MI()
{
//assert(!"Derive d_MI* requested");
__debugbreak();
return m_p;
}

// To catch every occasion where the pointer is cast
// via either reinterpret_cas t<or (void*) casts
operator void*()
{
//assert(!"void* requested");
__debugbreak();
return (void*)m_p;
}
};

class Derived_MI : public BaseMI_1, public BaseMI_2
{
public:
// To catch at compile time every occasion
// where an address is taken of this type
Derived_MI_ptr operator &()
{
// you could also insert a debugger break
__debugbreak();
//assert(!"addres s of Derived_MI taken");
Derived_MI *pThis = const_cast<Deri ved_MI*>(this);
return Derived_MI_ptr( pThis);
}
};

//~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~
int _tmain(int argc, _TCHAR* argv[])
{
// Test 2
Derived_MI dmi;
Derived_MI *pdmi = &dmi;
void* vp = NULL;
//vp = reinterpret_cas t<void*>(&dmi) ; // compile time error !
// debugbreak or assert or exception based upon your choice of
implementation
vp = (void*)&dmi;

return 0;
}

Dec 15 '06 #5
Alf P. Steinbach wrote:

I have the feeling you're talking about giving the dog a blood
infusion and perhaps transplanting some organs and removing some
tumors, to make it more lively and able to do new tricks.

Ach! An old dog is an old dog. Ever heard about teaching olds new
tricks? Difficult, that is. Perhaps even as difficult as teaching
the old dog not to bark or do other undesirable things.

Only because transfusions and new organs for old dogs are expensive,
and new puppies are cheap. If the puppy cost 10x that of patching the
old dog, you might have a different decision.

Rewriting complex systems from scratch is a costly endeavor. It might
be worth it in the long run, but it might not if it in turn is obsolete
in a few years.

Brian
Dec 15 '06 #6

Default User wrote:
Alf P. Steinbach wrote:

I have the feeling you're talking about giving the dog a blood
infusion and perhaps transplanting some organs and removing some
tumors, to make it more lively and able to do new tricks.

Ach! An old dog is an old dog. Ever heard about teaching olds new
tricks? Difficult, that is. Perhaps even as difficult as teaching
the old dog not to bark or do other undesirable things.


Only because transfusions and new organs for old dogs are expensive,
and new puppies are cheap. If the puppy cost 10x that of patching the
old dog, you might have a different decision.

Rewriting complex systems from scratch is a costly endeavor. It might
be worth it in the long run, but it might not if it in turn is obsolete
in a few years.
I kind of thought it was a given among proffessionals that you don't
just throw away years of investment and start all over because some of
the code has gotten difficult to work with.

New puppies aren't cheap. You have to teach them not to shit in the
house...not to bark all damn night long...not to chew up the
furniture...or the neighbor's cat....you have to teach them not to go
digging around in the garbage or the flower bed...there is so much to
teach a new dog that most owners unfortunately just don't do it.

The old dog might be slow and stubborn but a new one is just so damn
irritating and the time spent teaching the new dog the same tricks is
quite extensive. Especially when the old dog has been around for a
long time and has shown itself to be quite the little money maker.

Sorry, but if your best help is, "Throw it all away," that just isn't a
lot of help. If that's what you do in the face of adversity
well....well that's your choice. I'm looking for a different kind of
help though, thanks anyway.

Dec 16 '06 #7

de*@ieee.org wrote:
This is overkill, but part of it may come in handy should you actually
choose to try and refactor your classes using multiple inheritance.
Interesting idea, thanks.

Dec 16 '06 #8
* Noah Roberts:
Default User wrote:
>Alf P. Steinbach wrote:
>>I have the feeling you're talking about giving the dog a blood
infusion and perhaps transplanting some organs and removing some
tumors, to make it more lively and able to do new tricks.

Ach! An old dog is an old dog. Ever heard about teaching olds new
tricks? Difficult, that is. Perhaps even as difficult as teaching
the old dog not to bark or do other undesirable things.

Only because transfusions and new organs for old dogs are expensive,
and new puppies are cheap. If the puppy cost 10x that of patching the
old dog, you might have a different decision.

Rewriting complex systems from scratch is a costly endeavor. It might
be worth it in the long run, but it might not if it in turn is obsolete
in a few years.

I kind of thought it was a given among proffessionals that you don't
just throw away years of investment and start all over because some of
the code has gotten difficult to work with.
You raise two issues.

First, is creating a new app or set of apps (whatever) effectively to
throw away years of investment and start all over? No, because in the
meantime there's knowledge gained, market/clients gained, and perhaps
even code that can be reused (by proper packaging), and also because it
need not be all-or-nothing, and because it need not be sudden but can be
gradual, and not the least because it can save on maintenance due to New
Improved Structure and due to More Complete Knowledge, with the
developers of the new code all present in the firm, whereas some or all
of the old ones have left or will be leaving shortly. It's all about
finding ways to can-do rather than finding ways to can't-do.

Second, is code brittleness ever a reason to ditch code? Yes. Other
reasons may include e.g. that the original OS or hardware has or will
shortly become obsolete, but other than that, there's not much reason to
ditch code /except/ that it has become brittle and "difficult to work
with": it's a/the main reason, not a "just because"-reason.
[snip]
Sorry, but if your best help is, "Throw it all away," that just isn't a
lot of help. If that's what you do in the face of adversity
well....well that's your choice. I'm looking for a different kind of
help though, thanks anyway.
That's a bit turned on the head / inside-out, and also a bit personal
and hypothetical. Now that I'm focusing on those three aspects, perhaps
you can see that it's a very emotional response? I don't know what that
added emotion means, though. Perhaps my suggestion is simply not an
option due to locked-in position of management.

It might be an idea to review common antipatterns, e.g. take a look at
<url: http://en.wikipedia.or g/wiki/Anti-pattern>.

Is all that void* pointer-casting just one symptom, or is it one of a
larger family of symptoms?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Dec 16 '06 #9
On 15 Dec 2006 23:27:43 -0800, "Noah Roberts" wrote:
>I kind of thought it was a given among proffessionals that you don't
just throw away years of investment and start all over because some of
the code has gotten difficult to work with.
It's called 'refactoring' nowadays. The idea is to improve application
code, design and architecture without changing the core functionality.
I guess that's what you want to achieve. Sometimes however it's
cheaper to re-write parts of the application.

Best wishes,
Roland Pibinger
Dec 16 '06 #10

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

Similar topics

1
2296
by: Melissa Wallis | last post by:
I have a class with 5 callbacks. Two of the callbacks work fine but the others don't. The main difference is that the callbacks that don't work are composed of a sequence of structs. I noticed a comment about this same problem on the web but no solution was noted. What makes the callbacks with the sequences so different? It seems that when one of the callbacks with a sequence is called it just hangs. I am talking to a TAO orb from...
2
2083
by: Mike C. Fletcher | last post by:
I'm looking at rewriting parts of Twisted and TwistedSNMP to eliminate __del__ methods (and the memory leaks they create). Looking at the docs for 2.3's weakref.ref, there's no mention of whether the callbacks are held with a strong reference. My experiments suggest they are not... i.e. I'm trying to use this pattern: class Closer( object ): """Close the OIDStore (without a __del__)""" def __init__( self, btree ): """Initialise the...
1
1914
by: vijaya | last post by:
I've to invoke a unmanaged dll fucntion in C# which uses a callback fucntion.The unmanaged dll fucntion returns a pointer to a structure to its callback fucntion.The user should collect those structure fields in a buffer. In my managed code(i.e. in C# program), I've used a delegate for invoking callback function and I've declared the structure too. The dll fucntion is executing finely without any errors but I'm not getting any values...
0
2732
by: ck388 | last post by:
For some reason when I enable the callback feature of the gridview I still get a page refresh, that is it seems like there is a postback that occurs, not a callback which is just supposed to update not the whole page, but a portion of the page. Strangely enough the URL below http://beta.asp.net/QUICKSTARTV20/aspnet/doc/ctrlref/data/gridview.aspx (VB GridView Paging and Sorting Callbacks example)
5
3246
by: Christopher Jastram | last post by:
I'm a self-taught programmer, so this might be a pretty dumb question. If it is, please point me in the right direction and I shall apologize profusely. I have a question regarding C++ and object members. Can anyone help? I'm writing a C++ wrapper for a fairly old programming interface to a document editing program that has no OOP whatsoever; only tons of structs. This program has different callbacks I'm supposed to implement for...
6
3204
by: m | last post by:
Hello, I have an application that processes thousands of files each day. The filenames and various related file information is retrieved, related filenames are associate and placed in a linked list within a single object, which is then placed on a stack(This cuts down thread creation and deletions roughly by a factor of 4). I create up to 12 threads, which then process a single object off of the stack. I use a loop with a boolean...
0
1145
by: anilkoli | last post by:
I want clear cut idea about callbacks and also of delegates I have doughts about callbacks, I feel callbacks are used for 1. recursion 2. dynamically calling a perticular method out of many methods, deciding at runtime. 3. Notification
9
3343
by: zholthran | last post by:
Hi folks, after reading several threads on this issue (-> subject) I fear that I got a problem that cannot easily be solved by the offered workarounds in an acceptable way, at least not with my limited c & c++ experience. Maybe some of you can help. the problem: I need several instances of a class whose (non-static!) methods should serve as callbacks for a dll (which can' be manipulated/adapted in any
42
2062
by: lorlarz | last post by:
Contrary to what one authority in the JavaScript field says: JavaScript does make errors when dealing with just with integers. This authority (Douglas Crockford.) says: "integer arithmetic in floating point is exact" Well, I can prove this is incorrect with this program: http://mynichecomputing.com/digitallearning/yourOwn.htm This a program that uses only integers, yet comes up short in its
0
8969
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9337
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9266
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8215
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6054
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4570
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4826
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.