1/*
2 * Legal Notice
3 *
4 * This document and associated source code (the "Work") is a part of a
5 * benchmark specification maintained by the TPC.
6 *
7 * The TPC reserves all right, title, and interest to the Work as provided
8 * under U.S. and international laws, including without limitation all patent
9 * and trademark rights therein.
10 *
11 * No Warranty
12 *
13 * 1.1 TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THE INFORMATION
14 * CONTAINED HEREIN IS PROVIDED "AS IS" AND WITH ALL FAULTS, AND THE
15 * AUTHORS AND DEVELOPERS OF THE WORK HEREBY DISCLAIM ALL OTHER
16 * WARRANTIES AND CONDITIONS, EITHER EXPRESS, IMPLIED OR STATUTORY,
17 * INCLUDING, BUT NOT LIMITED TO, ANY (IF ANY) IMPLIED WARRANTIES,
18 * DUTIES OR CONDITIONS OF MERCHANTABILITY, OF FITNESS FOR A PARTICULAR
19 * PURPOSE, OF ACCURACY OR COMPLETENESS OF RESPONSES, OF RESULTS, OF
20 * WORKMANLIKE EFFORT, OF LACK OF VIRUSES, AND OF LACK OF NEGLIGENCE.
21 * ALSO, THERE IS NO WARRANTY OR CONDITION OF TITLE, QUIET ENJOYMENT,
22 * QUIET POSSESSION, CORRESPONDENCE TO DESCRIPTION OR NON-INFRINGEMENT
23 * WITH REGARD TO THE WORK.
24 * 1.2 IN NO EVENT WILL ANY AUTHOR OR DEVELOPER OF THE WORK BE LIABLE TO
25 * ANY OTHER PARTY FOR ANY DAMAGES, INCLUDING BUT NOT LIMITED TO THE
26 * COST OF PROCURING SUBSTITUTE GOODS OR SERVICES, LOST PROFITS, LOSS
27 * OF USE, LOSS OF DATA, OR ANY INCIDENTAL, CONSEQUENTIAL, DIRECT,
28 * INDIRECT, OR SPECIAL DAMAGES WHETHER UNDER CONTRACT, TORT, WARRANTY,
29 * OR OTHERWISE, ARISING IN ANY WAY OUT OF THIS OR ANY OTHER AGREEMENT
30 * RELATING TO THE WORK, WHETHER OR NOT SUCH AUTHOR OR DEVELOPER HAD
31 * ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.
32 *
33 * Contributors
34 * - Doug Johnson
35 */
36
37/******************************************************************************
38 * Description: Implementation of the CWheelTime class.
39 * See WheelTime.h for a high level description.
40 ******************************************************************************/
41
42#include "main/WheelTime.h"
43
44using namespace TPCE;
45
46CWheelTime::CWheelTime(PWheelConfig pWheelConfig) : m_pWheelConfig(pWheelConfig), m_Cycles(0), m_Index(0) {
47}
48
49CWheelTime::CWheelTime(PWheelConfig pWheelConfig, INT32 cycles, INT32 index)
50 : m_pWheelConfig(pWheelConfig), m_Cycles(cycles), m_Index(index) {
51}
52
53CWheelTime::CWheelTime(PWheelConfig pWheelConfig, CDateTime &Base, CDateTime &Now, INT32 offset)
54 : m_pWheelConfig(pWheelConfig) {
55 Set(Base, Now);
56 Add(offset);
57}
58
59CWheelTime::~CWheelTime(void) {
60}
61
62void CWheelTime::Add(INT32 Interval) {
63 // DJ - should throw error if Interval >= m_pWheelConfig->WheelSize?
64 m_Cycles += Interval / m_pWheelConfig->WheelSize;
65 m_Index += Interval % m_pWheelConfig->WheelSize;
66 if (m_Index >= m_pWheelConfig->WheelSize) {
67 // Handle wrapping in the wheel - assume we don't allow multi-cycle
68 // intervals
69 m_Cycles++;
70 m_Index -= m_pWheelConfig->WheelSize;
71 }
72}
73
74INT32 CWheelTime::Offset(const CWheelTime &Time) {
75 INT32 Interval;
76
77 Interval = (m_Cycles - Time.m_Cycles) * m_pWheelConfig->WheelSize;
78 Interval += (m_Index - Time.m_Index);
79 return (Interval);
80}
81
82void CWheelTime::Set(INT32 cycles, INT32 index) {
83 m_Cycles = cycles;
84 m_Index = index; // DJ - should throw error if Index >= m_pWheelConfig->WheelSize
85}
86
87// Set is overloaded. This version is used by the timer wheel.
88void CWheelTime::Set(CDateTime &Base, CDateTime &Now) {
89 INT32 offset; // offset from BaseTime in milliseconds
90
91 // DJ - If Now < Base, then we should probably throw an exception
92
93 offset = Now.DiffInMilliSeconds(Base) / m_pWheelConfig->WheelResolution; // convert based on wheel resolution
94 m_Cycles = offset / m_pWheelConfig->WheelSize;
95 m_Index = offset % m_pWheelConfig->WheelSize;
96}
97
98// Set is overloaded. This version is used by the event wheel.
99void CWheelTime::Set(CDateTime *pBase, CDateTime *pNow) {
100 INT32 offset; // offset from BaseTime in milliseconds
101
102 // DJ - If Now < Base, then we should probably throw an exception
103
104 offset = pNow->DiffInMilliSeconds(pBase) / m_pWheelConfig->WheelResolution; // convert based on wheel resolution
105 m_Cycles = offset / m_pWheelConfig->WheelSize;
106 m_Index = offset % m_pWheelConfig->WheelSize;
107}
108
109bool CWheelTime::operator<(const CWheelTime &Time) {
110 return (m_Cycles == Time.m_Cycles) ? (m_Index < Time.m_Index) : (m_Cycles < Time.m_Cycles);
111}
112
113CWheelTime &CWheelTime::operator=(const CWheelTime &Time) {
114 m_pWheelConfig = Time.m_pWheelConfig;
115 m_Cycles = Time.m_Cycles;
116 m_Index = Time.m_Index;
117
118 return *this;
119}
120
121CWheelTime &CWheelTime::operator+=(const INT32 &Interval) {
122 Add(Interval);
123 return *this;
124}
125
126CWheelTime CWheelTime::operator++(INT32) {
127 Add(1);
128 return *this;
129}
130