Skip to content

Instantly share code, notes, and snippets.

@lucasdemarchi
Created October 22, 2015 14:03
Show Gist options
  • Save lucasdemarchi/cf3908c0ad45d3f27764 to your computer and use it in GitHub Desktop.
Save lucasdemarchi/cf3908c0ad45d3f27764 to your computer and use it in GitHub Desktop.
#include "perf.h"
int main(int argc, const char *argv[])
{
PerfElapsed elapsed;
elapsed.begin();
elapsed.end();
elapsed.print();
PerfCounter count;
count.count();
count.print();
count.count();
count.count();
count.count();
count.count();
count.print();
return 0;
}
#include "perf.h"
#include <stdio.h>
#include <inttypes.h>
class PerfCounterPriv {
public:
void foo();
uint32_t count = 0;
};
void PerfCounterPriv::foo()
{
printf("private counter foo()\n");
}
class PerfElapsedPriv {
public:
void bar();
uint32_t start = 0;
uint32_t end = 0;
};
void PerfElapsedPriv::bar()
{
printf("private elapsed bar()\n");
}
// ---------------------
// Public implementation
PerfCounter::PerfCounter()
{
p = new PerfCounterPriv();
p->foo();
}
PerfCounter::~PerfCounter()
{
delete p;
}
void PerfCounter::print()
{
printf("%" PRIu32 "\n", p->count);
}
void PerfCounter::count()
{
p->count++;
}
PerfElapsed::PerfElapsed()
{
p = new PerfElapsedPriv();
p->bar();
}
PerfElapsed::~PerfElapsed()
{
delete p;
}
void PerfElapsed::print()
{
printf("%" PRIu32 "\n", p->end - p->start);
}
void PerfElapsed::begin()
{
p->start = 0;
}
void PerfElapsed::end()
{
p->end = 20;
}
#pragma once
class Perf {
public:
virtual void print() = 0;
};
class PerfCounterPriv;
class PerfElapsedPriv;
class PerfCounter : public Perf {
public:
PerfCounter();
~PerfCounter();
void print() override;
void count();
private:
PerfCounterPriv *p;
};
class PerfElapsed : public Perf {
public:
PerfElapsed();
~PerfElapsed();
void print() override;
void begin();
void end();
private:
PerfElapsedPriv *p;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment