5

現在、natvis で日付型のビジュアライザーを作成しています。日付型は 1970 年以降の秒数を通常の UNIX の方法で格納しますが、そこから年、月、日を導出するのは、一時変数を使用しないと非常に長くなります。正常な方法でビジュアライザーを評価するために、作業変数を保存できるようにしたいと考えています。これは可能ですか?

私がたどり着いた解決策は次のようになります。

struct SLowResAbsoluteTime
{
    SLowResAbsoluteTime() : mTime(0) { calcDOE(); }
    SLowResAbsoluteTime(int year, SDate::EMonth m, SDate::EDayOfWeek day, UINT8 hour, UINT8 minute, UINT8 seconds);
    SLowResAbsoluteTime(const SDate &date);
    SLowResAbsoluteTime(unsigned long long time) : mTime(time) { calcDOE(); }
    SLowResAbsoluteTime(const SLowResAbsoluteTime &other) : mTime(other.mTime) { calcDOE(); }

    SDate getDate() const; //calculate date object from given time
    UINT32 getHour() const; 
    UINT32 getMinutes() const;
    UINT32 getSeconds() const;

    bool operator < (const SLowResAbsoluteTime &other) const { return mTime < other.mTime; }
    bool operator > (const SLowResAbsoluteTime &other) const { return mTime > other.mTime; }
    bool operator <= (const SLowResAbsoluteTime &other) const { return mTime <= other.mTime; }
    bool operator >= (const SLowResAbsoluteTime &other) const { return mTime >= other.mTime; }
    bool operator == (const SLowResAbsoluteTime &other) const { return mTime == other.mTime; }
    bool operator != (const SLowResAbsoluteTime &other) const { return mTime != other.mTime; }
    SLowResAbsoluteTime operator -(const SLowResAbsoluteTime &time) const { return SLowResAbsoluteTime(mTime - time.mTime); }
    SLowResAbsoluteTime operator +(const SLowResAbsoluteTime &time) const { return SLowResAbsoluteTime(mTime + time.mTime); }
    const SLowResAbsoluteTime &operator -=(const SLowResAbsoluteTime &time) { mTime -= time.mTime; return *this; }
    const SLowResAbsoluteTime &operator +=(const SLowResAbsoluteTime &time) { mTime += time.mTime; return *this; }
    unsigned long long mTime;
    void invalidate() { mTime = -1; }
    bool isValid() const {return mTime != UINT64(-1); }
    operator unsigned long() const { return (long)mTime; }
    void calcDOE();
#ifdef USING_DEBUG_TIMER_DOE
    struct { UINT16 y; UINT8 m; UINT8 d; } mDOE;
#endif
};

「USING_DEBUG_TIMER_DOE」セクションに注意してください。それは次のように計算されます。

void SLowResAbsoluteTime::calcDOE()
{
#ifdef USING_DEBUG_TIMER_DOE
    int ts = mTime / (60 * 60 * 24);
    int z = ts + 719468;
    int doe = (z - ((z >= 0 ? z : z - 146096) / 146097) * 146097);
    int yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;  // [0, 399]
    int era = (z >= 0 ? z : z - 146096) / 146097;
    int y = (yoe) + era * 400;
    int doy = doe - (365 * yoe + yoe / 4 - yoe / 100);                // [0, 365]
    int mp = (5 * doy + 2) / 153;                                   // [0, 11]
    int d = doy - (153 * mp + 2) / 5 + 1;                             // [1, 31]
    int m = mp + (mp < 10 ? 3 : -9);                            // [1, 12]
    mDOE.y = y + (m <= 2);
    mDOE.m = m;
    mDOE.d = d; 
#endif  
}

これらを視覚化するための natvis は次のとおりです。

<Type Name="SLowResAbsoluteTime">

    <DisplayString>{{time = { (mTime / (60 * 60)) % 24  }:{(mTime / 60) % 60}:{mTime % 60} } day-1970: {mTime / (60 * 60 * 24)} }</DisplayString>

    <Expand>        
        <Item Name="month">(int)mDOE.m</Item>
        <Item Name="day">(int)mDOE.d</Item>-->
        <Item Name="secs since 1/1/1970"> mTime</Item>  
    </Expand>
</Type>
4

1 に答える 1