monotone

monotone Mtn Source Tree

Root/outdated_indicator.cc

1// 2007 Timothy Brownawell <tbrownaw@gmail.com>
2// GNU GPL V2 or later
3
4#include "base.hh"
5#include "outdated_indicator.hh"
6#include "sanity.hh"
7
8class outdated_indicator_factory_impl
9{
10 unsigned int changed;
11 unsigned int dispensed;
12public:
13 outdated_indicator_factory_impl();
14 void note_change();
15 unsigned int last_change() const;
16 unsigned int dispense();
17};
18
19outdated_indicator_factory_impl::outdated_indicator_factory_impl()
20 : changed(0), dispensed(0)
21{}
22
23unsigned int
24outdated_indicator_factory_impl::last_change() const
25{
26 return changed;
27}
28
29unsigned int
30outdated_indicator_factory_impl::dispense()
31{
32 I(changed == dispensed || changed == dispensed + 1);
33 dispensed = changed;
34 return dispensed;
35}
36
37void
38outdated_indicator_factory_impl::note_change()
39{
40 I(changed == dispensed || changed == dispensed + 1);
41 if (changed == dispensed)
42 ++changed;
43}
44
45
46outdated_indicator::outdated_indicator()
47 : parent(), when(0)
48{}
49
50outdated_indicator::outdated_indicator(boost::shared_ptr<outdated_indicator_factory_impl> p)
51 : parent(p), when(p->dispense())
52{}
53
54bool
55outdated_indicator::outdated()
56{
57 if (parent)
58 {
59 I(when <= parent->last_change());
60 return when < parent->last_change();
61 }
62 else
63 return true;
64}
65
66
67outdated_indicator_factory::outdated_indicator_factory()
68 : impl(new outdated_indicator_factory_impl)
69{}
70
71outdated_indicator_factory::~outdated_indicator_factory()
72{
73 impl->note_change();
74}
75
76outdated_indicator
77outdated_indicator_factory::get_indicator()
78{
79 return outdated_indicator(impl);
80}
81
82void
83outdated_indicator_factory::note_change()
84{
85 impl->note_change();
86}
87
88#ifdef BUILD_UNIT_TESTS
89#include "unit_tests.hh"
90
91UNIT_TEST(outdated_indicator, )
92{
93 outdated_indicator indicator;
94 {
95 outdated_indicator_factory factory;
96 UNIT_TEST_CHECK(indicator.outdated());
97 indicator = factory.get_indicator();
98 UNIT_TEST_CHECK(!indicator.outdated());
99 factory.note_change();
100 UNIT_TEST_CHECK(indicator.outdated());
101 factory.note_change();
102 factory.note_change();
103 indicator = factory.get_indicator();
104 UNIT_TEST_CHECK(!indicator.outdated());
105 }
106 UNIT_TEST_CHECK(indicator.outdated());
107}
108
109#endif
110
111
112// Local Variables:
113// mode: C++
114// fill-column: 76
115// c-file-style: "gnu"
116// indent-tabs-mode: nil
117// End:
118// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s:
119

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status