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

  • Thread starter Thread starter Efim
  • Start date Start date
E

Efim

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
 
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,
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 said:
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]
- (e-mail address removed)


Efim said:
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
 
Efim said:
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)
 
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 said:
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

in message news:[email protected]...
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]
- (e-mail address removed)


Efim said:
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: Object the
code.
 
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
 
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 said:
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 said:
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 said:
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]"
wrote
in message 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]
- (e-mail address removed)


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
 
Back
Top