-
Notifications
You must be signed in to change notification settings - Fork 156
/
ChartEntryBase.cpp
257 lines (205 loc) · 8.42 KB
/
ChartEntryBase.cpp
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/************************************************************************
* Copyright(c) 2010, One Unified. All rights reserved. *
* *
* This file is provided as is WITHOUT ANY WARRANTY *
* without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* *
* This software may not be used nor distributed without proper license *
* agreement. *
* *
* See the file LICENSE.txt for redistribution information. *
************************************************************************/
#include <algorithm>
#include <boost/log/trivial.hpp>
#include <boost/date_time/posix_time/posix_time_io.hpp>
#include "ChartEntryBase.h"
namespace ou { // One Unified
//
// ChartEntryBase
//
ChartEntryBase::ChartEntryBase()
: m_ixStart( 0 ), m_nElements( 0 ), m_eColour( ou::Colour::Black ) {
}
ChartEntryBase::ChartEntryBase( const ChartEntryBase& rhs )
: m_ixStart( rhs.m_ixStart ), m_nElements( rhs.m_nElements ), m_eColour( rhs.m_eColour )
, m_sName( rhs.m_sName )
{
}
ChartEntryBase::ChartEntryBase( ChartEntryBase&& rhs )
: m_ixStart( rhs.m_ixStart ), m_nElements( rhs.m_nElements ), m_eColour( rhs.m_eColour )
, m_sName( std::move( rhs.m_sName ) )
{
}
ChartEntryBase::~ChartEntryBase() {
}
//
// ChartEntryTime
//
ChartEntryTime::ChartEntryTime() :
ChartEntryBase()
{
}
//ChartEntryTime::ChartEntryTime( size_type nSize )
//: ChartEntryBase( nSize ),
// m_dtViewPortBegin( boost::posix_time::not_a_date_time ), m_dtViewPortEnd( boost::posix_time::not_a_date_time )
//{
// m_bufferedDateTime.Reserve( nSize );
// m_vChartTime.reserve( nSize );
// ChartEntryBase::Reserve( nSize );
//}
//ChartEntryBaseWithTime::ChartEntryBaseWithTime( const ChartEntryBaseWithTime& rhs ) :
// ChartEntryBase( rhs ),
// m_dtViewPortBegin( rhs.m_dtViewPortBegin ), m_dtViewPortEnd( rhs.m_dtViewPortEnd ),
// m_vDateTime( rhs.m_vDateTime ), m_vChartTime( rhs.m_vChartTime )
//{
// m_plfTimeDouble = new lfTimeDouble_t;
//}
ChartEntryTime::~ChartEntryTime() {
Clear();
}
ChartEntryTime::ChartEntryTime( ChartEntryTime&& rhs )
: ChartEntryBase( std::move( rhs ) )
, m_rangeViewPort( std::move( rhs.m_rangeViewPort ) )
, m_vDateTime( std::move( rhs.m_vDateTime ) )
, m_vChartTime( std::move( rhs.m_vChartTime ) )
, m_queue( std::move( rhs.m_queue ) )
{
}
void ChartEntryTime::Reserve( size_type nSize ) {
m_vDateTime.reserve( nSize );
m_vChartTime.reserve( nSize );
}
void ChartEntryTime::Append( boost::posix_time::ptime dt ) {
m_queue.Append( dt );
}
double ChartEntryTime::Convert( boost::posix_time::ptime dt ) {
const boost::gregorian::date date = dt.date();
const boost::posix_time::time_duration time = dt.time_of_day();
static const double divisor( time.ticks_per_second() );
double dfrac = time.fractional_seconds();
dfrac /= divisor;
const double converted = Chart::chartTime(
date.year(), date.month(), date.day(),
time.hours(), time.minutes(), time.seconds() )
+ dfrac
;
return converted;
}
// runs in thread of main? What does this do?
void ChartEntryTime::AppendFg( boost::posix_time::ptime dt ) {
bool bOk( true );
// this is maybe done on the fly and not correct here.
// lotsa extra stuff to track random breakage
// end result, pOrder generation using non-available quote for datetime source
try {
if ( boost::posix_time::special_values::not_a_date_time == dt ) {
BOOST_LOG_TRIVIAL(debug) << m_sName << " ChartEntryTime::AppendFg not a date time?";
bOk = false;
}
if ( boost::posix_time::special_values::max_date_time == dt ) {
BOOST_LOG_TRIVIAL(debug) << m_sName << " ChartEntryTime::AppendFg max date time?";
bOk = false;
}
if ( boost::posix_time::special_values::min_date_time == dt ) {
BOOST_LOG_TRIVIAL(debug) << m_sName << " ChartEntryTime::AppendFg min date time?";
bOk = false;
}
if ( boost::posix_time::special_values::neg_infin == dt ) {
BOOST_LOG_TRIVIAL(debug) << m_sName << " ChartEntryTime::AppendFg neg infin date time?";
bOk = false;
}
if ( boost::posix_time::special_values::pos_infin == dt ) {
BOOST_LOG_TRIVIAL(debug) << m_sName << " ChartEntryTime::AppendFg pos infin date time?";
bOk = false;
}
if ( boost::posix_time::special_values::not_special == dt ) {
BOOST_LOG_TRIVIAL(debug) << m_sName << " ChartEntryTime::AppendFg not special";
bOk = false;
}
if ( bOk ) {
//BOOST_LOG_TRIVIAL(debug) << m_sName << dt;
const double converted( Convert( dt ) );
// do these go in with IncCntElements instead?
assert( boost::posix_time::not_a_date_time!= dt ); // validate for TODO below
m_vDateTime.push_back( dt );
m_vChartTime.push_back( converted );
if ( ( boost::posix_time::not_a_date_time != m_rangeViewPort.dtEnd ) && ( dt > m_rangeViewPort.dtEnd ) ) {
// don't append any more values to visible area
}
else {
IncCntElements();
}
}
}
catch(...) {
BOOST_LOG_TRIVIAL(debug) << m_sName << " ChartEntryTime::AppendFg catch (bad datetime)";
}
}
// called from WinChartView::ThreadDrawChart1 -> ChartDataView::SetViewPort
void ChartEntryTime::SetViewPort( boost::posix_time::ptime dtBegin, boost::posix_time::ptime dtEnd ) {
SetViewPort( range_t( dtBegin, dtEnd ) );
}
void ChartEntryTime::SetViewPort( const range_t& range ) {
// record the viewport
m_rangeViewPort = range;
// initialize viewport values
SetIxStart( 0 );
SetCntElements( 0 );
// calculate new viewport values
// todo: what happens when nothing is within the range, should have zero elements listed
// should this be here or not?
//ClearQueue(); // should this be here?
// TODO: why would there be not_a_date_time in the vector?
// need to test for insertions like this?
if ( 0 != m_vDateTime.size() ) {
vDateTime_t::const_iterator citerBegin( m_vDateTime.begin() );
vDateTime_t::const_iterator citerEnd( m_vDateTime.end() );
//const auto diff( citerEnd - citerBegin ); // created for diagnostic use
if ( boost::posix_time::not_a_date_time != m_rangeViewPort.dtBegin ) {
citerBegin = std::lower_bound( m_vDateTime.begin(), m_vDateTime.end(), m_rangeViewPort.dtBegin );
}
if ( m_vDateTime.end() != citerBegin ) {
if ( boost::posix_time::not_a_date_time != m_rangeViewPort.dtEnd ) {
citerEnd = std::upper_bound( citerBegin, m_vDateTime.cend(), m_rangeViewPort.dtEnd );
}
}
SetIxStart( citerBegin - m_vDateTime.begin() );
SetCntElements( citerEnd - citerBegin );
if ( m_vDateTime.end() == citerBegin ) {
//std::stringstream ssbegin;
//ssbegin << range.dtBegin << "," << range.dtEnd;
//std::string sbegin = ssbegin.str();
//iterBegin = m_vDateTime.begin();
//iterEnd = m_vDateTime.end();
//SetIxStart( iterBegin - m_vDateTime.begin() );
//SetCntElements( iterEnd - iterBegin );
}
}
}
ChartEntryTime::range_t ChartEntryTime::GetExtents() const {
if ( 0 == m_vDateTime.size() ) {
return range_t( boost::posix_time::not_a_date_time, boost::posix_time::not_a_date_time );
}
else {
return range_t( m_vDateTime.front(), m_vDateTime.back() );
}
}
boost::posix_time::ptime ChartEntryTime::GetExtentBegin() const {
return 0 == m_vDateTime.size() ? boost::posix_time::not_a_date_time : m_vDateTime.front();
}
boost::posix_time::ptime ChartEntryTime::GetExtentEnd() const {
return 0 == m_vDateTime.size() ? boost::posix_time::not_a_date_time : m_vDateTime.back();
}
// there are out-of-order issues or loss-of-data issues if m_bUseThreadSafety is changed while something is in the Queue
void ChartEntryTime::ClearQueue( void ) {
namespace args = std::placeholders;
m_queue.Sync( std::bind( &ChartEntryTime::AppendFg, this, args::_1 ) );
}
void ChartEntryTime::Clear( void ) {
//ChartEntryBase::Clear();
m_vDateTime.clear();
m_vChartTime.clear();
//ChartEntryBase::Clear();
}
} // namespace ou