#include "HHMMSS_Clock.h"
#include "StopWatch.h"
extern void appl(IClock&);
int main()
{
std::cout.setf(std::ios::boolalpha);
HHMMSS_Clock c{18, 59, 58};
appl(c);
StopWatch s{};
appl(s);
}
#include <iostream>
#define SHOW(...)\
(void)(std::cout << __FILE__ << ':' << __LINE__ << '\t'\
<< #__VA_ARGS__ << " --> " << (__VA_ARGS__)\
<< std::endl)
#include "IClock.h"
void appl(IClock& c) {
SHOW( c);
SHOW(c.TickUp(), c);
SHOW(c.TickUp(), c);
SHOW(c.TickUp(), c);
SHOW(c.Set({22, 58}), c);
SHOW(c.TickUp(), c);
SHOW(c.TickUp(), c);
SHOW(c.TickUp(), c);
SHOW(c.TickUp(200), c);
SHOW(c.TickUp(400), c);
SHOW(c.TickUp(600), c);
}
#ifndef ICLOCK_H
#define ICLOCK_H
#include <iosfwd>
#include <initializer_list>
class IClock {
public:
virtual ~IClock() =default;
virtual void Set(const std::initializer_list<int>& li) =0;
virtual void TickUp(int = 1) =0;
virtual void Print(std::ostream&) const =0;
};
inline
std::ostream& operator<<(std::ostream& lhs, const IClock& rhs) {
rhs.Print(lhs);
return lhs;
}
#endif
#ifndef HHMMSS_CLOCK_H
#define HHMMSS_CLOCK_H
#include <iosfwd>
#include <cstring>
#include <iostream>
#include "IClock.h"
#include "Counter.h"
#include <cassert>
class HHMMSS_Clock : public IClock {
Counter hours_;
Counter minutes_;
Counter seconds_;
public:
HHMMSS_Clock(int hours = 0, int minutes = 0, int seconds = 0)
: hours_{24}
, minutes_{60, &hours_}
, seconds_{60, &minutes_}
{
Set({ hours, minutes, seconds });
}
HHMMSS_Clock(const HHMMSS_Clock&) =delete; // copy c'tor
HHMMSS_Clock& operator=(const HHMMSS_Clock&) =delete; // copy assignment
HHMMSS_Clock(HHMMSS_Clock&&) =delete; // move c'tor
HHMMSS_Clock& operator=(HHMMSS_Clock&&) =delete; // move assignment
virtual void Set(const std::initializer_list<int>& li) override;
virtual void TickUp(int seconds) override { seconds_.Count(seconds); }
virtual void Print(std::ostream&) const override;
};
#endif
#include "HHMMSS_Clock.h"
#include <cassert>
#include <iostream>
#include <iomanip>
void HHMMSS_Clock::Set(const std::initializer_list<int>& li) {
assert(li.size() <= 3);
auto it{ li.begin() };
for (auto *cntrp : { &hours_, &minutes_, &seconds_ }) {
if (it == li.end()) return;
cntrp->SetValue(*it++);
}
}
void HHMMSS_Clock::Print(std::ostream& s) const {
std::ostream os{s.rdbuf()};
os.fill('0');
os << std::setw(2) << hours_.GetValue() << ':'
<< std::setw(2) << minutes_.GetValue() << ':'
<< std::setw(2) << seconds_.GetValue();
}
#ifndef STOP_WATCH_H
#define STOP_WATCH_H
#include <cassert>
#include <cstring>
#include <iosfwd>
#include <iostream>
#include "IClock.h"
#include "Counter.h"
class StopWatch : public IClock {
Counter seconds_{};
Counter fraction_{};
public:
StopWatch()
: fraction_{1000, &seconds_}
{}
StopWatch(const StopWatch&) =delete; // copy c'tor
StopWatch& operator=(const StopWatch&) =delete; // copy assignment
StopWatch(StopWatch&&) =delete; // move c'tor
StopWatch& operator=(StopWatch&&) =delete; // move assignment
virtual void Set(const std::initializer_list<int>& li);
virtual void TickUp(int steps) override { fraction_.Count(steps); }
virtual void Print(std::ostream&) const override;
};
#endif
clock-2x: *.cpp *.h
g++ -std=c++14 *.cpp -o $@
run: clock-2x
./clock-2x
clean:
-rm -f *.o a.out core clock-2x
zip:
zip ../clock-2x.zip *.txt *.cpp *.h
Adding an interface to the clock
================================
Still to be done: show how different clocks can be added
and all of them operated through that single interface.