473,385 Members | 1,655 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

What pattern can be used instead of C++ macro?

Hi all,
Due to performance issue, I want to pevent execution of ToString() function
in the code like the following:

if(reporting_level & DEBUG_LEVEL)
log(reporting_level,string.Format("Event of type {1} arrived. Event data:
{2}.",event,obj.ToString()));

It is clear that in Format it can be used different parameters and not
always they should use ToString, thus it's problematically to use Object as
parameter in the wrapper function.

On the other hand,I don't want to add "if.." in hundreds lines of the code.

What the pattern can I use instead of macro to prevent ToString from
execution?

Thanks,
Efim
Nov 15 '05 #1
6 2898
Efim,

Is it that you don't want the ToString method to be called, or you don't
want the log method to be called (which violates naming conventions if the
method is public, btw) if you are not in debug mode?

If you don't want the ToString method to be called, then there is little
that you can do, as you could pass the object directly to the Format method,
but it would just call ToString anyways on the object (if it was of type
object).

If you don't want the log method to be called at all during debugging,
then you can use the ConditionalAttribute class to have the compiler ignore
methods if a condition is not met.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- ni**************@exisconsulting.com
"Efim" <ef***@nice.com> wrote in message
news:uV**************@TK2MSFTNGP12.phx.gbl...
Hi all,
Due to performance issue, I want to pevent execution of ToString() function in the code like the following:

if(reporting_level & DEBUG_LEVEL)
log(reporting_level,string.Format("Event of type {1} arrived. Event data:
{2}.",event,obj.ToString()));

It is clear that in Format it can be used different parameters and not
always they should use ToString, thus it's problematically to use Object as parameter in the wrapper function.

On the other hand,I don't want to add "if.." in hundreds lines of the code.
What the pattern can I use instead of macro to prevent ToString from
execution?

Thanks,
Efim

Nov 15 '05 #2
Nicholas,
Because there is no way to prevent ToString to be called if Log method is
called, I want to prevent log method to be called.
I am not sure that ConditionalAttribute class can be helpfull here, because
log is called from the release version also. If there is any problem, I can
(in the Registry or config file) set debug reporting level so the
application can print out more details. Thus the decision to call or do not
call log method cannot be taken at compile time.

Thanks,
Efim

"Nicholas Paldino [.NET/C# MVP]" <ni**************@exisconsulting.com> wrote
in message news:uj**************@TK2MSFTNGP12.phx.gbl...
Efim,

Is it that you don't want the ToString method to be called, or you don't want the log method to be called (which violates naming conventions if the
method is public, btw) if you are not in debug mode?

If you don't want the ToString method to be called, then there is little that you can do, as you could pass the object directly to the Format method, but it would just call ToString anyways on the object (if it was of type
object).

If you don't want the log method to be called at all during debugging,
then you can use the ConditionalAttribute class to have the compiler ignore methods if a condition is not met.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- ni**************@exisconsulting.com
"Efim" <ef***@nice.com> wrote in message
news:uV**************@TK2MSFTNGP12.phx.gbl...
Hi all,
Due to performance issue, I want to pevent execution of ToString() function
in the code like the following:

if(reporting_level & DEBUG_LEVEL)
log(reporting_level,string.Format("Event of type {1} arrived. Event data: {2}.",event,obj.ToString()));

It is clear that in Format it can be used different parameters and not
always they should use ToString, thus it's problematically to use

Object as
parameter in the wrapper function.

On the other hand,I don't want to add "if.." in hundreds lines of the

code.

What the pattern can I use instead of macro to prevent ToString from
execution?

Thanks,
Efim


Nov 15 '05 #3
Efim <ef***@nice.com> wrote:
Because there is no way to prevent ToString to be called if Log method is
called, I want to prevent log method to be called.
I am not sure that ConditionalAttribute class can be helpfull here, because
log is called from the release version also. If there is any problem, I can
(in the Registry or config file) set debug reporting level so the
application can print out more details. Thus the decision to call or do not
call log method cannot be taken at compile time.


Write another method which returns whether or not reporting is enabled
for that log level, then use:

if (log.IsDebugEnabled)
{
log.Debug (...)
}

(etc)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #4
I still don't think anyone is clear what you're trying
to do.

In which cases do you NOT want ToString() to be called?

How would you do this in C++? Please show us some type
of example so that we might try to translate it into
C# proper for you.

-C

P.S.- calling obj.ToString() in your log method is
redundant because the String.Format() method takes
System.Object as it's parameter list and will
automatically call ToString() on all objects being
passed in, so you're essentially doing:

obj.ToString().ToString();

The second ToString() is System.String.ToString()
which is basically a single line of code, but
still, it's the principle :)

"Efim" <ef***@nice.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Nicholas,
Because there is no way to prevent ToString to be called if Log method is called, I want to prevent log method to be called.
I am not sure that ConditionalAttribute class can be helpfull here, because log is called from the release version also. If there is any problem, I can (in the Registry or config file) set debug reporting level so the
application can print out more details. Thus the decision to call or do not call log method cannot be taken at compile time.

Thanks,
Efim

"Nicholas Paldino [.NET/C# MVP]" <ni**************@exisconsulting.com> wrote in message news:uj**************@TK2MSFTNGP12.phx.gbl...
Efim,

Is it that you don't want the ToString method to be called, or you
don't
want the log method to be called (which violates naming conventions if the method is public, btw) if you are not in debug mode?

If you don't want the ToString method to be called, then there is
little
that you can do, as you could pass the object directly to the Format method,
but it would just call ToString anyways on the object (if it was of

type object).

If you don't want the log method to be called at all during debugging, then you can use the ConditionalAttribute class to have the compiler

ignore
methods if a condition is not met.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- ni**************@exisconsulting.com
"Efim" <ef***@nice.com> wrote in message
news:uV**************@TK2MSFTNGP12.phx.gbl...
Hi all,
Due to performance issue, I want to pevent execution of ToString()

function
in the code like the following:

if(reporting_level & DEBUG_LEVEL)
log(reporting_level,string.Format("Event of type {1} arrived. Event data: {2}.",event,obj.ToString()));

It is clear that in Format it can be used different parameters and
not always they should use ToString, thus it's problematically to use

Object
as
parameter in the wrapper function.

On the other hand,I don't want to add "if.." in hundreds lines of the code.

What the pattern can I use instead of macro to prevent ToString

from execution?

Thanks,
Efim



Nov 15 '05 #5
"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP***********************@news.microsoft.com. ..

<SNIP>
Write another method which returns whether or not reporting is enabled
for that log level, then use:

if (log.IsDebugEnabled)
{
log.Debug (...)
}


What about Trace switches? It seems like this has all
been written for you (generic).

-c
Nov 15 '05 #6
If I recall correctly, Macros serve as a basic
type of inline statement/method, right?

I think you should have a static method on
a class (Log.Log() maybe?) that has a few
statements in it.

The .NET JIT is extremely efficient and
will most likely inline all of these
method calls, thus achieving the same
type of performance as Macros.

Log.Log(reporting_level, String.Format(...));

public sealed class Log
{
[DllImport("log.dll", EntryPoint"Log", ExactSpelling=true)]
private static extern ExternLog(int debugLevel, string data);

private Log(){}

public static void Log( int debugLevel, string data)
{
ExternLog(debugLevel, data);
}
}

-c

"Efim" <ef***@nice.com> wrote in message
news:eK**************@TK2MSFTNGP12.phx.gbl...
Well, the following is the code I would use in C++:

#define LOG(_l,_p) \

if (_l & DEBUG_LEVEL) { \

log(_l,_p);
Now, if I want to print out some line, I can write something like the
following:

LOG(reporting_level,string.Format("Event data {1}.",event,obj));

Another thing: I have no control over the log method because it is placed in C++ dll, which I have to use due to some design limitations.

Thanks,

Efim

"Chad Myers" <cm****@N0.SP.4M.austin.rr.com> wrote in message
news:xG*******************@twister.austin.rr.com.. .
I still don't think anyone is clear what you're trying
to do.

In which cases do you NOT want ToString() to be called?

How would you do this in C++? Please show us some type
of example so that we might try to translate it into
C# proper for you.

-C

P.S.- calling obj.ToString() in your log method is
redundant because the String.Format() method takes
System.Object as it's parameter list and will
automatically call ToString() on all objects being
passed in, so you're essentially doing:

obj.ToString().ToString();

The second ToString() is System.String.ToString()
which is basically a single line of code, but
still, it's the principle :)

"Efim" <ef***@nice.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Nicholas,
Because there is no way to prevent ToString to be called if Log method
is
called, I want to prevent log method to be called.
I am not sure that ConditionalAttribute class can be helpfull
here, because
log is called from the release version also. If there is any
problem, I can
(in the Registry or config file) set debug reporting level so the
application can print out more details. Thus the decision to call
or do not
call log method cannot be taken at compile time.

Thanks,
Efim

"Nicholas Paldino [.NET/C# MVP]"
<ni**************@exisconsulting.com> wrote
in message news:uj**************@TK2MSFTNGP12.phx.gbl...
> Efim,
>
> Is it that you don't want the ToString method to be called,
or you
don't
> want the log method to be called (which violates naming
conventions if the
> method is public, btw) if you are not in debug mode?
>
> If you don't want the ToString method to be called, then
there is
little
> that you can do, as you could pass the object directly to the
Format method,
> but it would just call ToString anyways on the object (if it was of type
> object).
>
> If you don't want the log method to be called at all during

debugging,
> then you can use the ConditionalAttribute class to have the
compiler ignore
> methods if a condition is not met.
>
> Hope this helps.
>
>
> --
> - Nicholas Paldino [.NET/C# MVP]
> - ni**************@exisconsulting.com
>
>
> "Efim" <ef***@nice.com> wrote in message
> news:uV**************@TK2MSFTNGP12.phx.gbl...
> > Hi all,
> > Due to performance issue, I want to pevent execution of ToString() > function
> > in the code like the following:
> >
> > if(reporting_level & DEBUG_LEVEL)
> > log(reporting_level,string.Format("Event of type {1} arrived.

Event
data:
> > {2}.",event,obj.ToString()));
> >
> > It is clear that in Format it can be used different parameters and not
> > always they should use ToString, thus it's problematically to
use Object
> as
> > parameter in the wrapper function.
> >
> > On the other hand,I don't want to add "if.." in hundreds lines

of the
> code.
> >
> > What the pattern can I use instead of macro to prevent
ToString from
> > execution?
> >
> > Thanks,
> > Efim
> >
> >
>
>



Nov 15 '05 #7

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

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
1
by: Steven T. Hatton | last post by:
I just wrote some code that does the following: Starting frome existing code that generates a grid of nested boxes representing the range of indeces in a set of bounded unsigned int values,...
7
by: Newbie_sw2003 | last post by:
Where should I use them? I am giving you my understandings. Please correct me if I am wrong: MACRO: e.g.:#define ref-name 99 The code is substituted by the MACRO ref-name. So no overhead....
19
by: Charles Law | last post by:
Take a solution with a project hierarchy along the lines of an n-tier system, so that we have a data layer, business layer and presentation layer. The presentation layer is coupled to the business...
23
by: Xah Lee | last post by:
The Concepts and Confusions of Pre-fix, In-fix, Post-fix and Fully Functional Notations Xah Lee, 2006-03-15 Let me summarize: The LISP notation, is a functional notation, and is not a...
2
by: =?Utf-8?B?QWFyb24=?= | last post by:
Hi, I'm having a tricky problem where I want to accept a regular expression pattern from user input but can't get teh escape characters to be prcoessed correctly. If I take the same pattern and...
20
by: jacob navia | last post by:
Consider this code static typedef struct { int boo; } FOO; This provokes with MSVC: ------------------------------ Microsoft (R) C/C++ Optimizing Compiler Version 14.00.50727.762 for x64...
14
by: Just_a_fan | last post by:
In VB6, I could easily take the value from a combo box and make a command with it, this: baudrate = cboBaud(listindex). With the new dotted stuff in VB9, I can't seem to do that. Here's an...
32
by: Stephen Horne | last post by:
I've been using Visual C++ 2003 for some time, and recently started working on making my code compile in GCC and MinGW. I hit on lots of unexpected problems which boil down to the same template...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
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,...

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.