-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
94 lines (67 loc) · 2.03 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <cstdio>
#include <memory>
#include <string>
#include <ctime>
#include <iostream>
#include <chrono>
#include "abc.h"
#include "test_pimpl.h"
#include "test_zeropimpl.h"
static const int kNumAllocs = 100000000;
void runPimpl()
{
for ( int i = 0; i < kNumAllocs; ++i ) {
const int r = std::rand();
std::unique_ptr<PObject> o(r%2 ? new PObject(r) : new PWidget(r, r));
}
}
void runZeropimpl()
{
for ( int i = 0; i < kNumAllocs; ++i ) {
const int r = std::rand();
std::unique_ptr<ZObject> o(r%2 ? new ZObject(r) : new ZWidget(r, r));
}
}
int main(int argc, char ** argv)
{
if ( argc > 1 ) {
std::srand(42);
const std::string arg = argv[1];
std::cout << arg << std::endl;
const auto begin = std::chrono::system_clock::now();
if ( arg == "pimpl" ) {
runPimpl();
} else if ( arg == "zeropimpl" ) {
runZeropimpl();
}
std::chrono::duration<double> time = std::chrono::system_clock::now() - begin;
std::cout << "time: " << time.count() << std::endl;
return 0;
}
std::printf("start\n");
std::fflush(stdout);
std::unique_ptr<A> a(A::make(33));
std::printf("a->value=%d\n", a->value());
std::fflush(stdout);
a->setValue(55);
std::printf("a->value=%d\n", a->value());
std::fflush(stdout);
std::unique_ptr<B> b(B::make(87));
std::printf("b->value=%d b->data=%d\n", b->value(), b->data());
std::fflush(stdout);
b->setValue(45);
std::printf("b->value=%d b->data=%d\n", b->value(), b->data());
std::fflush(stdout);
std::unique_ptr<A> b2(B::make(44));
const B * const b2reader = static_cast<const B*>(b2.get());
std::printf("b2->value=%d b2->data=%d\n", b2reader->value(), b2reader->data());
b2->event(48);
std::printf("b2->value=%d b2->data=%d\n", b2reader->value(), b2reader->data());
std::unique_ptr<C> c(new C(77));
std::printf("c->value=%d c->data=%d c->A::value()=%d\n", c->value, c->data(), c->A::value());
A * const ca = c.get();
ca->event(66);
std::printf("c->value=%d c->data=%d c->A::value()=%d\n", c->value, c->data(), c->A::value());
std::unique_ptr<A> cap = std::move(c);
return 0;
}