-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.cpp
72 lines (56 loc) · 2.11 KB
/
example.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
// Copyright 2015 Jon Chesterfield. All rights reserved.
// This file is part of pimpl.
// pimpl is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// pimpl is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with pimpl. If not, see <http://www.gnu.org/licenses/>.
#include "example.hpp"
#include "pimpl_impl.hpp"
#include <cassert>
#include <vector>
// Usually a class that has behaviour we care about
// In this example, it's arbitrary
class example_impl
{
public:
example_impl(int x = 0) { insert(x); }
example_impl(int a, int b) { insert(a); insert(b);}
void insert(int x) { local_state.push_back(3 * x); }
int retrieve() { return local_state.back(); }
bool operator==(example_impl const &other)
{
return local_state == other.local_state;
}
bool operator!=(example_impl const &other) { return !(*this == other); }
private:
// Potentially exotic local state
// For example, maybe we don't want std::vector in the header
std::vector<int> local_state;
};
void example::first_method(int x)
{
self()->insert(x);
}
int example::second_method()
{
return self()->retrieve();
}
static_assert(sizeof(example_impl) == example::capacity,
"example capacity has diverged");
static_assert(alignof(example_impl) == example::alignment,
"example alignment has diverged");
namespace pimpl
{
template class base<example_impl, sizeof(example_impl), alignof(example_impl)>;
using targ = base<example_impl, sizeof(example_impl), alignof(example_impl)>;
// Need to do something to instantiate the forwarding constructors since
// the pimpl header cannot, in general, guess their types.
template targ::base(int &&);
template targ::base(int &&, int&&);
}