JSONStats.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #ifndef TestSuite_JSONStats_h
  2. #define TestSuite_JSONStats_h
  3. #include "../../JSONOptions.h"
  4. #if defined(JSON_UNIT_TEST) || defined(JSON_DEBUG)
  5. #define LIBJSON_OBJECT(name)\
  6. static size_t & getCtorCounter(void){\
  7. static size_t count = 0;\
  8. static int i = JSONStats::setCallbacks(getCtorCounter, getCopyCtorCounter, getAssignmentCounter, getDtorCounter, #name);\
  9. return count;\
  10. }\
  11. static size_t & getCopyCtorCounter(void){\
  12. static size_t count = 0;\
  13. static int i = JSONStats::setCallbacks(getCtorCounter, getCopyCtorCounter, getAssignmentCounter, getDtorCounter, #name);\
  14. return count;\
  15. }\
  16. static size_t & getAssignmentCounter(void){\
  17. static size_t count = 0;\
  18. static int i = JSONStats::setCallbacks(getCtorCounter, getCopyCtorCounter, getAssignmentCounter, getDtorCounter, #name);\
  19. return count;\
  20. }\
  21. static size_t & getDtorCounter(void){\
  22. static size_t count = 0;\
  23. static int i = JSONStats::setCallbacks(getCtorCounter, getCopyCtorCounter, getAssignmentCounter, getDtorCounter, #name);\
  24. return count;\
  25. }
  26. #define LIBJSON_CTOR getCtorCounter() += 1
  27. #define LIBJSON_COPY_CTOR getCopyCtorCounter() += 1
  28. #define LIBJSON_ASSIGNMENT getAssignmentCounter() += 1
  29. #define LIBJSON_DTOR getDtorCounter() += 1
  30. #include <map>
  31. #include <sstream>
  32. #include <iostream>
  33. #include <string>
  34. class JSONStats {
  35. public:
  36. ~JSONStats(void){
  37. std::map<getCounter_m, objectStructure*> & mymap = getMapper();
  38. std::map<getCounter_m, objectStructure*>::iterator b = mymap.begin();
  39. std::map<getCounter_m, objectStructure*>::iterator e = mymap.end();
  40. std::cout << "Counters for libjson:" << std::endl;
  41. for(; b != e; ++b){
  42. std::cout << " " << b -> second -> _name << std::endl;
  43. std::cout << " Constructor: " << b -> second -> _cTor() << std::endl;
  44. std::cout << " Copy Constructor: " << b -> second -> _ccTor() << std::endl;
  45. std::cout << " Assignment: " << b -> second -> _assign() << std::endl;
  46. std::cout << " Destructor: " << b -> second -> _dTor() << std::endl;
  47. delete b -> second;
  48. }
  49. }
  50. typedef size_t & (*getCounter_m)(void);
  51. struct objectStructure {
  52. objectStructure(getCounter_m cTor, getCounter_m ccTor, getCounter_m assign, getCounter_m dTor, const std::string & name):
  53. _cTor(cTor), _ccTor(ccTor), _assign(assign), _dTor(dTor), _name(name){}
  54. std::string _name;
  55. getCounter_m _cTor;
  56. getCounter_m _ccTor;
  57. getCounter_m _assign;
  58. getCounter_m _dTor;
  59. };
  60. static int setCallbacks(getCounter_m cTor, getCounter_m ccTor, getCounter_m assign, getCounter_m dtor, const std::string & name){
  61. getMapper()[cTor] = new objectStructure (cTor, ccTor, assign, dtor, name);
  62. return 0;
  63. }
  64. static std::map<getCounter_m, objectStructure*> & getMapper(void) {
  65. static std::map<getCounter_m, objectStructure*> mymap;
  66. return mymap;
  67. }
  68. };
  69. #else
  70. #define LIBJSON_OBJECT(name)
  71. #define LIBJSON_CTOR (void)0
  72. #define LIBJSON_DTOR (void)0
  73. #define LIBJSON_COPY_CTOR (void)0
  74. #define LIBJSON_ASSIGNMENT (void)0
  75. typedef int JSONStats;
  76. #endif
  77. #endif