// examples/test_ctx_pattern translated by mycpp // BEGIN mycpp output #include "mycpp/runtime.h" GLOBAL_STR(str0, "x"); GLOBAL_STR(str1, "x"); GLOBAL_STR(str2, "y"); GLOBAL_STR(str3, "none %d"); namespace test_ctx_pattern { // forward declare class ctx_Eval; } // forward declare namespace test_ctx_pattern namespace test_ctx_pattern { // declare class ctx_Eval { public: ctx_Eval(Dict* vars); ~ctx_Eval(); Dict* vars{}; List* restore{}; DISALLOW_COPY_AND_ASSIGN(ctx_Eval) }; void run_tests(); void run_benchmarks(); } // declare namespace test_ctx_pattern namespace test_ctx_pattern { // define ctx_Eval::ctx_Eval(Dict* vars) { gHeap.PushRoot(reinterpret_cast(&(this->restore))); gHeap.PushRoot(reinterpret_cast(&(this->vars))); this->vars = vars; if (vars != nullptr) { this->restore = Alloc>(); this->restore->append(str0); } mylib::MaybeCollect(); } ctx_Eval::~ctx_Eval() { if (this->vars != nullptr) { this->restore->pop(); } gHeap.PopRoot(); gHeap.PopRoot(); } void run_tests() { Dict* d = nullptr; d = Alloc>(std::initializer_list{str1}, std::initializer_list{str2}); for (int i = 0; i < 1000; ++i) { { // with ctx_Eval ctx{nullptr}; print(StrFormat("none %d", i)); } } } void run_benchmarks() { ; // pass } } // define namespace test_ctx_pattern int main(int argc, char **argv) { gHeap.Init(); char* b = getenv("BENCHMARK"); if (b && strlen(b)) { // match Python's logic fprintf(stderr, "Benchmarking...\n"); test_ctx_pattern::run_benchmarks(); } else { test_ctx_pattern::run_tests(); } gHeap.CleanProcessExit(); }