473,728 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can you make a template (w/ or w/o the pre-processor) solution to do this?

In our C++ project we have some internal bug reporting macros that we
use to get useful information when the program does something
unexpected. Essentially at the point of the error, we invoke an
internal interactive debugger that knows the [important] classes
within our system and allow us to walk around the objects that exist
at the time of the fault. It mostly works fairly well. That catch
being that we have to hand implement some of the code each of the
classes (thus the "important class" caveat), which suggests that what
we really need is a template based approach, since it would be much
nicer to have the system automatically generate an implementation for
all classes, eliminating the important/unimportant distinction.

In addition, our current implementation does not work in static member
functions, which is a secondary problem (but a key one if we go to a
template based approach). Currently, we can sidestep this problem
because we have no important classes that also have static member
functions that use the bug reporting macro. (Actually, this topic
came up because I just made a class "important" that had a static
member function that used the bug report macro, and I had to remove
the static member function from the class and make it a global
function to work around the limitation of our current scheme.)

Essentially, we want an ErrorHere function that works anywhere in our
code. If it is in a non-static member function of the class, it calls
the ErrorHereInClas s virtual member function of the class which allows
the developer to see the data of the object being operated upon.
Anywhere else, we want the code to default to "assert(fal se)" like
behaviour. Our current implementation pretty much achieves that. In
objects that we care about, we have a few things that we add to the
class source code and then have an appropriate ErrorHereInClas s
function that we can specialize. In classes where haven't done that,
we get the default implementation, which essentially prints out an
appropriate error.

Most of the functionality is implemented via an "ErrorHere" header
file. The ErrorHere header file defines a couple of classes and some
macros (to sugar things by hiding some of the internal mechanism).
The key macro appearing to be a set of functions that work like
"assert(false)" , i.e. you call one of them when the code is in trouble
and it figures out how to best report to the user the location of the
problem.

Here is tersely what the ErrorHere header looks like:

class ErrorHereDevelo per; // controls some internal features

class ErrorHereClass {
public:
ErrorHereClass( ErrorHereDevelo per *dev ) :
errorHereDevelo per( dev )
{}
~ErrorHereClass ();
virtual void ErrorHereInClas s( const char *textToReport ) // overriden
{ cout << textToReport << endl; }
void ErrorHereReport ( const char *textToReport )
{ ErrorHereInClas s( textToReport ); }
protected: // data is here for this class and derived classes to use
ErrorHereDevelo per *errorHereDevel oper;
};

// default version that returns above "base" class
ErrorHereClass ErrorHereClassF actory( ErrorHereDevelo per *dev )
{
ErrorHereClass mine( dev );
return mine;
}

// use this call when any developer can handle this bug
extern void ErrorHere( const char *textToReport, ... );
extern class ErrorHereDevelo per *anyone;
#define ErrorHere ErrorHereClassF actory( anyone ).ErrorHereRepo rt

// I use this call when I want users to report the bug only to me
extern void ErrorHereForChr is( const char *textToReport, ... );
extern class ErrorHereDevelo per *chris;
#define ErrorHere ErrorHereClassF actory( chris ).ErrorHereRepo rt

.. . .

As you can see, a "call" to ErrorHere in the pressence of just this
header file,calls the "global" ErrorHereClassF actory which retunrs a
class where the ErrorHereReport function takes and simply prints the
string (using a virtual function). Of course, our real implementation
does something more complex, but this is the essential "base"
functionality.

Now, let's look at a customized class, say "Square":

class ErrorHereClassS quare;

class Square {
ErrorHereClassS quare ErrorHereClassF actory( ErrorHereDevelo per *dev )
{
ErrorHereClassS quare mine( this, dev );
return mine;
}
virtual void ErrorHereInClas s( const char *textToReport )
{ cout << "Square[" << length << ", " << width << "]: " <<
textToReport << endl; }
// rest of Square
. . .
int length, width; // (ok, maybe I meant rectangle!)
};

class ErrorHereClassS quare : public ErrorHereClass {
public:
ErrorHereClassS quare( Square *square; ErrorHereDevelo per *dev )
: ErrorHereClass( dev ),
mySquare( square )
{}
~ErrorHereClass Square();
virtual void ErrorHereInClas s( const char *textToReport ) // override
{ if ( square ) square->ErrorHereInCla ss( textToReport );
else cout << textToReport << endl; }
private:
Square *mySquare;
};

So, as you can see, the special class ErrorHereClassS quare and the per
class function ErrorHereClassF actory allows us to customize the code
for our classes. However, this code is "boilerplat e" and it would be
nice if we could somehow use a template to create them. I think I
understand how to write the template that will create the
ErrorHere<Class Square>. That seems relatively straight forward.

However, it would also be nice, if we could somehow get the
functionality to work in static member functions, i.e. to have
something that would "revert" to the default implementation if the
"this" pointer wasn't available. I have no idea how to make a
(function?) template that tests its current context and determines if
the function it is being called within has a "this" pointer or not.
My fear is that since we make ErrorHere calls through-out our code
(including in static member functions and non-member functions) that
if we attempt ErrorHere in a static member function with a template
solution in place, that this will result in a call to the template
class constructor for the corresponding ErrorHere<Class > without
having a this pointer to pass to the class. At least that's what
happens when I define a per class ErrorClassFacto ry method in a class
that makes ErrorHere calls from static member functions.

I apologize if this is trivial to accomplish, but it is just too
subtle for me and my meager template programming ability. In the end
I would like something that works within the limits of both Visual C++
6.x and g++ 3.2.3.

-Chris

*************** *************** *************** *************** *************** **
Chris Clark Internet : co*****@world.s td.com
Compiler Resources, Inc. Web Site : http://world.std.com/~compres
23 Bailey Rd voice : (508) 435-5016
Berlin, MA 01503 USA fax : (978) 838-0263 (24 hours)
------------------------------------------------------------------------------
Jul 22 '05 #1
0 1641

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

Similar topics

2
1173
by: madhukar_bm | last post by:
Can we declare a template function as a friend to a class, if yes please give me the syntax.Thanks in advance rgrds Madhukar
11
9920
by: Matt | last post by:
I'm posting this question for one of my developers who's not quite as newsgroup-savvy. Any suggestions? The question follows, along with relevant source code. -Matt I have a templated queue class as part of a statically linkable library (in GQueue.h and .cpp below). The library compiles fine, but when I go to use it in an executable, I get linker errors complaining
2
2100
by: Doc | last post by:
Per earlier post, I am trying to save 'out' production data from a program called Solomon - basically (I think) this was /is an Access/Sql based program. We are updating to different application and I need to bring over ONLY certain DATA. When I did an ODBC link to the DB all the tables are present. I linked the three or four tables and have my fields (these are the ones I want to modify for LATER import into the new application)
5
3419
by: Leonardo D'Ippolito | last post by:
Hello all. The code bellow prints (in the command line window) the MAC ADDRESSES of all NIC cards enabled in the computer. What I need is: a library based on this code, that will return a string with the output of this command line tool. I need to use this library in my C# (.NET) program. In other words, convert this native .EXE in a library that I could use in my managed C# application.
1
1223
by: Abhishek Srivastava | last post by:
Hello All, In almost all websites which I have developed, we need to follow a layout template (header, footer, menu on left hand side etc) In ASP.NET solutions, we can do this in two ways 1) use user controls. But frankly, making a "control" which renders a table of width 100% with
1
1305
by: William Gower | last post by:
I need to develop a datagrid that uses columns from a table. In addition I need two columns (checkboxes) that the user will use to indicate that this record can be closed later. The checkboxed column is not a field in the table. It is just for the purposes of the Datagrid and I do not need to store whether it was checked or not. Do I use a Template Column for this?
0
2736
by: Steven Blair | last post by:
Hi, I would like to create a Calender effect like this website: http://www.expedia.co.uk/default.aspx This site uses ASP.NET, so I guess we should be able to use VS 2005 to recreate this. I should note, I am using Atlas June CTP.
2
2006
by: adrian.hawryluk | last post by:
Hi everyone, I've been using templates for a while, but I'm not at full power yet (knowledge=power) ;). Does anyone know where I can get information on this 'new' template usage? template<a (b, c, d)> Does one define it like:
3
1603
by: Boltar | last post by:
Hi I have a template class thats defined in a header file and implemented in a .cpp module that is compiled to a .o file. No problems there. But when I try to use this class from another .o file I get undefined error messages from the linux linker, eg: cl_foo<int>(...) not defined. Presumbly this is because theres no actual instance of cl_foo<int> created in the .o file that contains the template class. Is there a
0
8759
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9424
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9135
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8133
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...
1
6718
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6013
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
4793
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3236
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 we have to send another system
3
2161
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.