00 //============================================================================
01 // Name        : timeseries_pimpl.cpp
02 // Author      : Simonas Saltenis
03 // Version     :
04 // Description : Demos PIMPL
05 //============================================================================
06 
07 #include "timeseries.h"
08 #include <algorithm>
09 #include <iterator>
10 #include <random>
11 #include <functional>
12 
13 // note all the necessary headers included...
14 
15 using namespace std;
16 
17 namespace TimeData
18 {
19 
20 class TimeSeries::TimeSeriesImpl
21 {
22 public:
23     TimeSeriesImpl() :
24         maxValue{std::numeric_limits<int>::min()},
25         minValue{std::numeric_limits<int>::max()} {}  // data is empty by default
26 
27     TimeSeriesImpl(std::initializer_list<int> ts) :       // constructor for testing
28         data{ts}
29     { updateMinMax(); }
30 
31     // ... All other implementation stuff
32 
33 private:
34     void updateMinMax();          // should be invoked when data is modified
35 
36     std::vector<int> data;
37     int              maxValue;    // maximum value in data
38     int              minValue;    // minimum value in data
39 };
40 
41 //----------------------------------------------------------------------
42 void TimeSeries::TimeSeriesImpl::updateMinMax()
43 {
44    //... some implementation...
45 }
46 
47 // Other TimeSeries::TimeSeriesImpl methods...
48 //
49 
50 //--------------------------------------------
51 
52 TimeSeries::TimeSeries() : pImpl{std::make_unique<TimeSeriesImpl>()} {}
53 
54 // Ask to generate the default destructor here.
55 // Now the compiler knows the full TimeSeriesImpl type and the destructor of std::unique_ptr<TimeSeriesImpl> will compile 
56 TimeSeries::~TimeSeries() = default;   
57 
58 
59 // Copy operations
60 TimeSeries::TimeSeries(const TimeSeries& ts) : pimpl{std::make_unique<TimeSeriesImpl>(*ts.pimpl)} {}
61 
62 TimeSeries& TimeSeries::operator=(const TimeSeries& ts)
63 {
64     *pimpl = *ts.pimpl;     // delegate to TimeSeriesImpl::operator=
65     return *this;
66 }
67 
68 // For efficiency, we ask for the default versions of the move constructor/assignment operator.
69 TimeSeries::TimeSeries(TimeSeries&& ts) noexcept = default;
70 TimeSeries& TimeSeries::operator=(TimeSeries&& ts) noexcept = default;
71 
72 // Interface implementations using TimeSeriesImpl,
73 // but could also implement the logic here and use TimeSeriesImpl as an encapsulation of private data...
74 //
75 bool TimeSeries::operator<(const TimeSeries& t) const
76 {
77     return *pimpl < *t.pimpl;
78 }
79 
80 } // namespace TimeData
81