Last active
July 16, 2016 13:47
-
-
Save yangacer/a4c31f33a24417b3963dfa165b018fe5 to your computer and use it in GitHub Desktop.
Utility for observing C++ object copying/moving
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include <string> | |
#include <iostream> | |
using namespace std; | |
struct obs { | |
obs() : m_mv(0), m_cp(0) { m_regs.insert(this); } | |
obs(obs const &cp) { m_regs.insert(this); m_cp++; m_total_cp++;} | |
obs(obs &&mv) { m_regs.insert(this); m_mv++; m_total_mv++;} | |
obs &operator=(obs const &cp) { m_regs.insert(this); m_cp++; m_total_cp++; } | |
obs &operator=(obs const &&mv) { m_regs.insert(this); m_mv++; m_total_mv++;} | |
void reset() { m_cp = 0; m_mv = 0; } | |
static void reset_all() { m_total_mv = m_total_cp = 0; } | |
static void print_cnt() { | |
cout << "total mv: " << | |
obs::m_total_mv << | |
", cp: " << obs::m_total_cp << "\n" | |
; | |
for (auto &it : m_regs){ | |
cout << it << | |
", mv: " << it->m_mv << | |
", cp: " << it->m_cp << | |
"\n" | |
; | |
it->reset(); | |
} | |
reset_all(); | |
} | |
int m_mv, m_cp; | |
static int m_total_mv, m_total_cp; | |
static set<obs *> m_regs; | |
}; | |
int obs::m_total_mv(0); | |
int obs::m_total_cp(0); | |
set<obs*> obs::m_regs; | |
// wrap | |
struct obs_string : string, obs { | |
using string::string; | |
}; | |
int main(){ | |
vector<obs_string> items{"abc", "123", "bcd"}; | |
obs::print_cnt(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment