PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Examples/d/funcptr/d1/runme.d

#
D | 42 lines | 29 code | 8 blank | 5 comment | 1 complexity | 34d3c018ac76a8ace26c150775549c54 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. module runme;
  2. import tango.io.Stdout;
  3. static import example;
  4. extern(C) int add(int a, int b) {
  5. return a + b;
  6. }
  7. extern(C) int sub(int a, int b) {
  8. return a - b;
  9. }
  10. extern(C) int mul(int a, int b) {
  11. return a * b;
  12. }
  13. void main() {
  14. int a = 37;
  15. int b = 42;
  16. Stdout( "a = " )( a ).newline;
  17. Stdout( "b = " )( b ).newline;
  18. Stdout( "Trying some C callback functions:" ).newline;
  19. Stdout( " ADD(a,b) = " )( example.do_op( a, b, example.ADD ) ).newline;
  20. Stdout( " SUB(a,b) = " )( example.do_op( a, b, example.SUB ) ).newline;
  21. Stdout( " MUL(a,b) = " )( example.do_op( a, b, example.MUL ) ).newline;
  22. version (LDC) {
  23. // Currently, there is no way to specify the calling convention for
  24. // function pointer parameters in D, but LDC does strict typechecking for
  25. // them (which is reasonable, but not covered by the language spec yet).
  26. // As a result, there is no way to make the code below compile with LDC at
  27. // the moment, so just skip it.
  28. } else {
  29. Stdout( "Now the same with callback functions defined in D:" ).newline;
  30. Stdout( " add(a,b) = " )( example.do_op( a, b, &add ) ).newline;
  31. Stdout( " sub(a,b) = " )( example.do_op( a, b, &sub ) ).newline;
  32. Stdout( " mul(a,b) = " )( example.do_op( a, b, &mul ) ).newline;
  33. }
  34. }