View mode: basic / threaded / horizontal-split · Log in · Help
July 20, 2012
Problem: handling a function dependent on release mode.
Trying to convert the below code to D, for the life of me I
cannot do it :( I'm guess I need to use a template for this
but I just cannot get my head around it. Any help please?

#ifdef SFML_DEBUG

     // If in debug mode, perform a test on every call
     #define alCheck(Func) ((Func), priv::alCheckError(__FILE__,
__LINE__))

#else

     // Else, we don't add any overhead
     #define alCheck(Func) (Func)

#endif


void alCheckError(const std::string& file, unsigned int line);
July 20, 2012
Re: Problem: handling a function dependent on release mode.
On Friday, 20 July 2012 at 21:36:59 UTC, Damian wrote:
> Trying to convert the below code to D, for the life of me I
> cannot do it :( I'm guess I need to use a template for this
> but I just cannot get my head around it. Any help please?
>
> #ifdef SFML_DEBUG
>
>      // If in debug mode, perform a test on every call
>      #define alCheck(Func) ((Func), priv::alCheckError(__FILE__,
> __LINE__))
>
> #else
>
>      // Else, we don't add any overhead
>      #define alCheck(Func) (Func)
>
> #endif
>
>
> void alCheckError(const std::string& file, unsigned int line);

Maybe something like that?

void alCheck(lazy void Func, string filename = __FILE__, uint
line = __LINE__) {
	Func();
	
	debug {
		alCheckError(filename, line);
	}
}
July 20, 2012
Re: Problem: handling a function dependent on release mode.
On Friday, 20 July 2012 at 21:47:55 UTC, Namespace wrote:
> On Friday, 20 July 2012 at 21:36:59 UTC, Damian wrote:
>> Trying to convert the below code to D, for the life of me I
>> cannot do it :( I'm guess I need to use a template for this
>> but I just cannot get my head around it. Any help please?
>>
>> #ifdef SFML_DEBUG
>>
>>     // If in debug mode, perform a test on every call
>>     #define alCheck(Func) ((Func), priv::alCheckError(__FILE__,
>> __LINE__))
>>
>> #else
>>
>>     // Else, we don't add any overhead
>>     #define alCheck(Func) (Func)
>>
>> #endif
>>
>>
>> void alCheckError(const std::string& file, unsigned int line);
>
> Maybe something like that?
>
> void alCheck(lazy void Func, string filename = __FILE__, uint
> line = __LINE__) {
> 	Func();
> 	
> 	debug {
> 		alCheckError(filename, line);
> 	}
> }

That works perfect, you sir are a genius!
July 21, 2012
Re: Problem: handling a function dependent on release mode.
On Friday, July 20, 2012 23:36:58 Damian wrote:
> Trying to convert the below code to D, for the life of me I
> cannot do it :( I'm guess I need to use a template for this
> but I just cannot get my head around it. Any help please?
> 
> #ifdef SFML_DEBUG
> 
> // If in debug mode, perform a test on every call
> #define alCheck(Func) ((Func), priv::alCheckError(__FILE__,
> __LINE__))
> 
> #else
> 
> // Else, we don't add any overhead
> #define alCheck(Func) (Func)
> 
> #endif
> 
> 
> void alCheckError(const std::string& file, unsigned int line);

It is impossible to have a function which relies on -release unless you put 
all calls to it in an assertion. There is _no_ way to check whether -release 
is enabled or not.

-debug enables debug blocks:

debug
{
//my code that runs with -debug only
}

but that's only if -debug is used. It's perfectly possible to compile with 
neither -release nor -debug. The result is that talking about "debug mode" in 
D is really confusing and inaccurate.

- Jonathan M Davis
July 21, 2012
Re: Problem: handling a function dependent on release mode.
On 2012-07-20 23:47, Namespace wrote:

> Maybe something like that?
>
> void alCheck(lazy void Func, string filename = __FILE__, uint
> line = __LINE__) {
>      Func();
>
>      debug {
>          alCheckError(filename, line);
>      }
> }

If "filename" and "line" is template parameters they will get the 
default values from the call site.

void alCheck (string filename = __FILE__, uint line = __LINE__) (lazy 
void Func)
{}

-- 
/Jacob Carlborg
July 21, 2012
Re: Problem: handling a function dependent on release mode.
On Saturday, 21 July 2012 at 16:05:24 UTC, Jacob Carlborg wrote:
> On 2012-07-20 23:47, Namespace wrote:
>
>> Maybe something like that?
>>
>> void alCheck(lazy void Func, string filename = __FILE__, uint
>> line = __LINE__) {
>>     Func();
>>
>>     debug {
>>         alCheckError(filename, line);
>>     }
>> }
>
> If "filename" and "line" is template parameters they will get 
> the default values from the call site.
>
> void alCheck (string filename = __FILE__, uint line = __LINE__) 
> (lazy void Func)
> {}

Equally when they are passed as normal parameters.
July 22, 2012
Re: Problem: handling a function dependent on release mode.
On 2012-07-21 19:11, Namespace wrote:

> Equally when they are passed as normal parameters.

Really? I didn't know that.

-- 
/Jacob Carlborg
July 22, 2012
Re: Problem: handling a function dependent on release mode.
On Sunday, 22 July 2012 at 16:19:29 UTC, Jacob Carlborg wrote:
> On 2012-07-21 19:11, Namespace wrote:
>
>> Equally when they are passed as normal parameters.
>
> Really? I didn't know that.

Really. Try it. ;)
July 22, 2012
Re: Problem: handling a function dependent on release mode.
On Sunday, July 22, 2012 18:25:58 Namespace wrote:
> On Sunday, 22 July 2012 at 16:19:29 UTC, Jacob Carlborg wrote:
> > On 2012-07-21 19:11, Namespace wrote:
> >> Equally when they are passed as normal parameters.
> > 
> > Really? I didn't know that.
> 
> Really. Try it. ;)

Yeah. Don't have them be template parameters unless you need to, otherwise you 
get a different template instantiation _every_ time that you call the function.

- Jonathan M Davis
July 22, 2012
Re: Problem: handling a function dependent on release mode.
On 7/22/12, Jonathan M Davis <jmdavisProg@gmx.com> wrote:
> Yeah. Don't have them be template parameters unless you need to, otherwise
> you
> get a different template instantiation _every_ time that you call the
> function.

I've just noticed something:

@property front(T)(T arr, string file = __FILE__, size_t line = __LINE__)
   if (isArray!T)
{
   enforce(arr.length, safeFmt("%s(%s): Cannot call front on empty
array.", file, line));
   return std.array.front(arr);
}

This errors at compilation: Error: properties can only have zero, one,
or two parameter

Do you think I should file this?
« First   ‹ Prev
1 2
Top | Discussion index | About this forum | D home