fork download
  1. #define RESOURCEHOLDER(DEFINITIONS, ENTRYCODE, EXITCODE, OBJECTNAME)\
  2.   struct RESOURCEHOLDER_##OBJECTNAME{\
  3.   DEFINITIONS\
  4. RESOURCEHOLDER_##OBJECTNAME(){\
  5. ENTRYCODE\
  6. }\
  7. ~RESOURCEHOLDER_##OBJECTNAME(){\
  8. EXITCODE\
  9. }\
  10. }OBJECTNAME
  11.  
  12. //Anwendungsbeispiel
  13. #include <iostream>
  14. #include <string>
  15. using std::cout;
  16. using std::string;
  17. int main(){
  18. //2 Resourcen im selben scope:
  19. RESOURCEHOLDER(int i;, i = 5; cout << "RESOURCEHOLDER initialized\n";, cout << "RESOURCEHOLDER destroyed " << i << '\n';, rh);
  20. cout << rh.i << '\n';
  21. rh.i += 2;
  22. RESOURCEHOLDER(double j;, j = 12.; cout << "RESOURCEHOLDER initialized\n";, cout << "RESOURCEHOLDER destroyed " << j << '\n';, rh2);
  23. cout << rh2.j << '\n';
  24. rh2.j += 3;
  25. {
  26. //ich könnte schwören struct RESOURCEHOLDER_rh ist doppelt definiert, aber weder VS2010E noch gcc 4.3.4 stören sich dran
  27. RESOURCEHOLDER(string s;, s = "Hello world"; cout << "RESOURCEHOLDER initialized\n";, cout << "RESOURCEHOLDER destroyed " << s << '\n';, rh);
  28. cout << rh.s << '\n';
  29. rh.s += "!!!";
  30. }
  31. }
  32.  
Success #stdin #stdout 0.01s 2856KB
stdin
Standard input is empty
stdout
RESOURCEHOLDER initialized
5
RESOURCEHOLDER initialized
12
RESOURCEHOLDER initialized
Hello world
RESOURCEHOLDER destroyed Hello world!!!
RESOURCEHOLDER destroyed 15
RESOURCEHOLDER destroyed 7