-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcomposable.hpp
More file actions
55 lines (46 loc) · 1.44 KB
/
composable.hpp
File metadata and controls
55 lines (46 loc) · 1.44 KB
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
#pragma once
#include "hierarchy.hpp"
#include "comparable.hpp"
#include "counter.hpp"
#include "clonable.hpp"
#include <algorithm>
#include <assert.h>
namespace crtp {
using std::min;
//--------------------------------------------------------------------------
class person2
: public member<person2>
, public counter<person2>
, public relative_comparable<person2>
, public clonable<person2>
{
public:
typedef member<person2> base_t;
person2(person2* parent, string const& name)
: base_t(parent), name_(name) {}
string const& name() const { return name_; }
private:
string name_;
};
ptrdiff_t compare(person2 const& lhs, person2 const& rhs) {
string const& lname = lhs.name();
string const& rname = rhs.name();
ptrdiff_t diff = 0;
for (size_t i = 0; i < min(lname.size(), rname.size()); ++i) {
diff = lname[i] - rname[i];
if (diff)
return diff;
}
return lname.size() - rname.size();
}
bool test_composable() {
person2 phil(nullptr, "Phil");
person2& ben = phil.add_child("Ben");
person2& alek = phil.add_child("Alek");
assert(alek < ben);
assert(phil > ben);
assert(person2::objects_created == 5); // temporaries were created
assert(person2::objects_alive == 3);
return true;
}
}