stamp.cpp

来自「一个语言识别引擎」· C++ 代码 · 共 75 行

CPP
75
字号
// -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-

/*
 * Copyright (C) 2006 Paul Fitzpatrick
 * CopyPolicy: Released under the terms of the GNU GPL v2.0.
 *
 */


#include <yarp/os/Stamp.h>
#include <yarp/os/Bottle.h>
#include <yarp/os/Time.h>

#include <yarp/IOException.h>

using namespace yarp::os;
using namespace yarp;

bool Stamp::read(ConnectionReader& connection) {
    try {
        connection.convertTextMode();
        int header = connection.expectInt();
        if (header!=BOTTLE_TAG_LIST) { return false; }
        int len = connection.expectInt();
        if (len!=2) { return false; }
        int code;
        code = connection.expectInt();
        if (code!=BOTTLE_TAG_INT) { return false; }
        sequenceNumber = connection.expectInt();
        code = connection.expectInt();
        if (code!=BOTTLE_TAG_DOUBLE) { return false; }
        timeStamp = connection.expectDouble();
        return true;
    } catch (IOException e) {
        sequenceNumber = -1;
        timeStamp = 0;
        return false;
    }
}

bool Stamp::write(ConnectionWriter& connection) {
    try {
        connection.appendInt(BOTTLE_TAG_LIST); // nested structure
        connection.appendInt(2);               // with two elements
        connection.appendInt(BOTTLE_TAG_INT);
        connection.appendInt(sequenceNumber);
        connection.appendInt(BOTTLE_TAG_DOUBLE);
        connection.appendDouble(timeStamp);
        connection.convertTextMode();
        return true;
    } catch (IOException e) {
        return false;
    }
}



int Stamp::getMaxCount() {
    // a very conservative maximum
    return 32767;
}


void Stamp::update() {
    double now = Time::now();
    if (sequenceNumber<0) {
        timeZero = now;
    }
    sequenceNumber++;
    if (sequenceNumber>getMaxCount()||sequenceNumber<0) {
        sequenceNumber = 0;
    }
    timeStamp = now-timeZero;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?