CCA
Tick.cpp
Go to the documentation of this file.
00001 /* ============================================================
00002  *
00003  * This file is a part of CCA project
00004  *
00005  * Copyright (C) 2014 by Arne Nordmann <anordman at cor-lab dot uni-bielefeld dot de>
00006  *
00007  * This file may be licensed under the terms of the
00008  * GNU Lesser General Public License Version 3 (the ``LGPL''),
00009  * or (at your option) any later version.
00010  *
00011  * Software distributed under the License is distributed
00012  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
00013  * express or implied. See the LGPL for the specific language
00014  * governing rights and limitations.
00015  *
00016  * You should have received a copy of the LGPL along with this
00017  * program. If not, go to http://www.gnu.org/licenses/lgpl.html
00018  * or write to the Free Software Foundation, Inc.,
00019  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  * The development of this software was supported by:
00022  *   CoR-Lab, Research Institute for Cognition and Robotics
00023  *     Bielefeld University
00024  *
00025  * ============================================================ */
00026 
00027 #include "Tick.h"
00028 
00029 #include <limits>
00030 #include <sstream>
00031 
00032 #include <boost/format.hpp>
00033 #include <boost/algorithm/string.hpp>
00034 
00035 #include <rsc/misc/langutils.h>
00036 
00037 using namespace std;
00038 using namespace boost;
00039 using namespace boost::algorithm;
00040 
00041 namespace cca {
00042 namespace timing {
00043 
00044 Tick::Tick(boost::uint64_t seq) :
00045         DataTransferObject(), sequenceNumber(seq),
00046                 timestamp(rsc::misc::currentTimeMicros()), timestampVirtual(0),
00047                 timestep(0), timestepVirtual(0), factor(0), stepFactor(0) {
00048 }
00049 
00050 Tick::~Tick() {
00051 }
00052 
00053 void Tick::setSequenceNumber(boost::uint64_t seq) {
00054     sequenceNumber = seq;
00055 }
00056 
00057 boost::uint64_t Tick::getSequenceNumber() {
00058     return sequenceNumber;
00059 }
00060 
00061 void Tick::setTimeStamp(boost::uint64_t ts) {
00062     timestamp = ts;
00063 }
00064 
00065 boost::uint64_t Tick::getTimeStamp() {
00066     return timestamp;
00067 }
00068 
00069 void Tick::setVirtualTimeStamp(boost::uint64_t ts) {
00070     timestampVirtual = ts;
00071 }
00072 
00073 boost::uint64_t Tick::getVirtualTimeStamp() {
00074     return timestampVirtual;
00075 }
00076 
00077 void Tick::setTimeStep(boost::uint64_t ts) {
00078     timestep = ts;
00079 }
00080 
00081 boost::uint64_t Tick::getTimeStep() {
00082     return timestep;
00083 }
00084 
00085 void Tick::setVirtualTimeStep(boost::uint64_t ts) {
00086     timestepVirtual = ts;
00087 }
00088 
00089 boost::uint64_t Tick::getVirtualTimeStep() {
00090     return timestepVirtual;
00091 }
00092 
00093 void Tick::setFactor(double f) {
00094     factor = f;
00095 }
00096 
00097 double Tick::getFactor() {
00098     if (factor < 10 * numeric_limits<double>::epsilon()) {
00099         // Factor is null
00100         if ((timestamp > 10 * numeric_limits<double>::epsilon())
00101                 && (timestampVirtual > 10 * numeric_limits<double>::epsilon())) {
00102             // Timestamp and virtual timestamp set, so we can calculate
00103             return timestampVirtual / timestamp;
00104         }
00105     }
00106 
00107     return factor;
00108 }
00109 
00110 void Tick::setStepFactor(double f) {
00111     stepFactor = f;
00112 }
00113 
00114 double Tick::getStepFactor() {
00115     if (stepFactor < 10 * numeric_limits<double>::epsilon()) {
00116         // Factor is null
00117         if ((timestep > 10 * numeric_limits<double>::epsilon())
00118                 && (timestepVirtual > 10 * numeric_limits<double>::epsilon())) {
00119             // Timestamp and virtual timestamp set, so we can calculate
00120             return timestepVirtual / timestep;
00121         }
00122     }
00123 
00124     return stepFactor;
00125 }
00126 
00127 std::string Tick::print() const {
00128     return str(
00129             format(
00130                     "<Tick (sequenceNumber=%d, timestamp=%d, "
00131                     "timestampVirtual=%d, timestep=%d, timestepVirtual=%d)>")
00132                     % sequenceNumber
00133                     % timestamp
00134                     % timestampVirtual
00135                     % timestep
00136                     % timestepVirtual);
00137 }
00138 
00139 }
00140 }