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