www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Problem: handling a function dependent on release mode.

reply "Damian" <damianday hotmail.co.uk> writes:
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);
Jul 20 2012
next sibling parent reply "Namespace" <rswhite4 googlemail.com> writes:
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); } }
Jul 20 2012
parent reply Jacob Carlborg <doob me.com> writes:
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
Jul 21 2012
parent Jacob Carlborg <doob me.com> writes:
On 2012-07-21 19:11, Namespace wrote:

 Equally when they are passed as normal parameters.

Really? I didn't know that. -- /Jacob Carlborg
Jul 22 2012
prev sibling next sibling parent "Damian" <damianday hotmail.co.uk> writes:
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!
Jul 20 2012
prev sibling next sibling parent "Jonathan M Davis" <jmdavisProg gmx.com> writes:
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
Jul 20 2012
prev sibling next sibling parent "Namespace" <rswhite4 googlemail.com> writes:
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.
Jul 21 2012
prev sibling next sibling parent "Namespace" <rswhite4 googlemail.com> writes:
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. ;)
Jul 22 2012
prev sibling next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
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
Jul 22 2012
prev sibling next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
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?
Jul 22 2012
prev sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Sunday, July 22, 2012 23:40:16 Andrej Mitrovic wrote:
 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?

No. The error is correct. A property can't take all of those arguments, and the fact that you gave it default arguments has no effect on the type. Granted, that could be a bit annoying in this case, since then you'd be forced to using __FILE__ and __LINE__ as template arguments, but given how default arguments work, it makes no sense to allow what you're trying to do. On the bright side though, how often do you really need this? In my experience, the main advantages of having __FILE__ and __LINE__ as default arguments is to give exceptions the proper file and line number and to help with unit testing helper functions which need to give the file and line number of their call point on test failure rather than the line number inside of them where the test failed. By the way, I believe that the agreed upon way to handle the check that you're doing is to use an assertion, not an exception - at least as far as Phobos goes. Using enforce there is probably going to kill your performance given the fact that it can't be inlined and front is probably used often, and it makes using ranges in a nothrow context really annoying. The caller needs to check for empty first if there's any question rather than relying on front or popFront to handle it. - Jonathan M Davis
Jul 22 2012