Chage TimeTableSet interface so cliet should modify it througt a proxy object.

This commit is contained in:
Sergey Magidovich 2015-12-10 11:58:06 +03:00 committed by Sergey Yershov
parent 7a30febd40
commit c7dcbc9d0c
2 changed files with 77 additions and 26 deletions

View file

@ -157,33 +157,38 @@ UNIT_TEST(TestAppendTimeTable)
{
{
TimeTableSet tts;
TEST(!tts.empty(), ());
TEST(!tts.Empty(), ());
auto tt = tts.back();
{
auto tt = tts.Back();
TEST(tt.RemoveWorkingDay(osmoh::Weekday::Sunday), ());
TEST(tt.RemoveWorkingDay(osmoh::Weekday::Saturday), ());
TEST(tts.Replace(tt, 0), ());
TEST(tt.RemoveWorkingDay(osmoh::Weekday::Sunday), ());
TEST(tt.RemoveWorkingDay(osmoh::Weekday::Saturday), ());
TEST(tt.Commit(), ());
TEST(tts.Append(tts.GetComplementTimeTable()), ());
TEST_EQUAL(tts.Back().GetWorkingDays(), (set<osmoh::Weekday>{osmoh::Weekday::Sunday,
osmoh::Weekday::Saturday}), ());
}
{
auto tt = tts.Front();
TEST(tt.RemoveWorkingDay(osmoh::Weekday::Monday), ());
TEST(tt.RemoveWorkingDay(osmoh::Weekday::Tuesday), ());
TEST(tt.Commit(), ());
}
TEST(tts.Append(tts.GetComplementTimeTable()), ());
TEST_EQUAL(tts.back().GetWorkingDays(), (set<osmoh::Weekday>{osmoh::Weekday::Sunday,
osmoh::Weekday::Saturday}), ());
tt = tts.front();
TEST(tt.RemoveWorkingDay(osmoh::Weekday::Monday), ());
TEST(tt.RemoveWorkingDay(osmoh::Weekday::Tuesday), ());
TEST(tts.Replace(tt, 0), ());
TEST(tts.Append(tts.GetComplementTimeTable()), ());
TEST_EQUAL(tts.back().GetWorkingDays(), (set<osmoh::Weekday>{osmoh::Weekday::Monday,
TEST_EQUAL(tts.Back().GetWorkingDays(), (set<osmoh::Weekday>{osmoh::Weekday::Monday,
osmoh::Weekday::Tuesday}), ());
TEST(!tts.GetComplementTimeTable().IsValid(), ());
TEST(!tts.Append(tts.GetComplementTimeTable()), ());
TEST_EQUAL(tts.size(), 3, ());
TEST_EQUAL(tts.Size(), 3, ());
TEST(!tts.Remove(0), ());
TEST(tts.Remove(1), ());
TEST_EQUAL(tts.size(), 2, ());
TEST_EQUAL(tts.Size(), 2, ());
TEST_EQUAL(tts.GetUnhandledDays(), (set<osmoh::Weekday>{osmoh::Weekday::Sunday,
osmoh::Weekday::Saturday}), ());
}
@ -196,9 +201,9 @@ UNIT_TEST(TestAppendTimeTable)
TEST(tts.Append(tt), ());
TEST_EQUAL(tts.size(), 2, ());
TEST_EQUAL(tts.front().GetWorkingDays().size(), 4, ());
TEST_EQUAL(tts.back().GetWorkingDays().size(), 3, ());
TEST_EQUAL(tts.Size(), 2, ());
TEST_EQUAL(tts.Front().GetWorkingDays().size(), 4, ());
TEST_EQUAL(tts.Back().GetWorkingDays().size(), 3, ());
TEST(!tts.GetComplementTimeTable().IsValid(), ());
}
@ -209,16 +214,33 @@ UNIT_TEST(TestAppendTimeTable)
TEST(tts.Append(tt), ());
TEST_EQUAL(tts.size(), 2, ());
TEST_EQUAL(tts.front().GetWorkingDays().size(), 6, ());
TEST_EQUAL(tts.back().GetWorkingDays().size(), 1, ());
TEST_EQUAL(tts.Size(), 2, ());
TEST_EQUAL(tts.Front().GetWorkingDays().size(), 6, ());
TEST_EQUAL(tts.Back().GetWorkingDays().size(), 1, ());
TEST(!tts.GetComplementTimeTable().IsValid(), ());
tt = tts[0];
tt = tts.Front();
tt.AddWorkingDay(osmoh::Weekday::Friday);
TEST(!tts.Append(tt), ());
TEST_EQUAL(tts.front().GetWorkingDays().size(), 6, ());
TEST_EQUAL(tts.back().GetWorkingDays().size(), 1, ());
TEST_EQUAL(tts.Front().GetWorkingDays().size(), 6, ());
TEST_EQUAL(tts.Back().GetWorkingDays().size(), 1, ());
}
{
TimeTableSet tts;
{
auto tt = tts.GetComplementTimeTable();
tt.AddWorkingDay(osmoh::Weekday::Friday);
TEST(tts.Append(tt), ());
}
TEST_EQUAL(tts.Size(), 2, ());
TEST_EQUAL(tts.Front().GetWorkingDays().size(), 6, ());
TEST_EQUAL(tts.Back().GetWorkingDays().size(), 1, ());
auto tt = tts.Front();
tt.AddWorkingDay(osmoh::Weekday::Friday);
TEST(!tt.Commit(), ());
}
}

View file

@ -47,7 +47,28 @@ private:
osmoh::TTimespans m_excludeTime;
};
class TimeTableSet : public vector<TimeTable>
template <typename TTimeTableSet>
class TimeTableProxyBase : public TimeTable
{
public:
TimeTableProxyBase(TTimeTableSet & tts, size_t const index, TimeTable const & tt):
TimeTable(tt),
m_index(index),
m_tts(tts)
{
}
bool Commit() { return m_tts.Replace(*this, m_index); } // Slice base class on copy.
private:
size_t const m_index;
TTimeTableSet & m_tts;
};
class TimeTableSet;
using TTimeTableProxy = TimeTableProxyBase<TimeTableSet>;
class TimeTableSet : vector<TimeTable>
{
public:
using vector<TimeTable>::vector;
@ -58,6 +79,13 @@ public:
TimeTable GetComplementTimeTable() const;
TTimeTableProxy Get(size_t const index) { return TTimeTableProxy(*this, index, (*this)[index]); }
TTimeTableProxy Front() { return Get(0); }
TTimeTableProxy Back() { return Get(Size() - 1); }
size_t Size() const { return size(); }
bool Empty() const { return empty(); }
bool Append(TimeTable const & tt);
bool Remove(size_t const index);
@ -65,6 +93,7 @@ public:
private:
static bool UpdateByIndex(TimeTableSet & ttSet, size_t const index);
};
} // namespace ui
} // namespace editor