TimestampVariant.h
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package
19 * @author
20 * @date
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24#pragma once
25
26#include <chrono>
27
28#include <IceUtil/Time.h>
29
32#include <ArmarXCore/interface/observers/Timestamp.h>
34
35namespace armarx::VariantType
36{
37 // Variant types
38 const VariantTypeId Timestamp = Variant::addTypeName("::armarx::TimestampBase");
39} // namespace armarx::VariantType
40
41namespace armarx
42{
43 class TimestampVariant;
45
46 /**
47 * @class TimestampVariant
48 * @ingroup VariantsGrp
49 * Implements a Variant type for timestamps. The timestamp value is interpreted as microseconds since the Unix Epoch.
50 * Internally the class bases on the IceUtil::Time functionality of Ice.
51 *
52 * For information on how to use this type, refer to Variant.
53 */
54 class ARMARXCORE_IMPORT_EXPORT TimestampVariant : virtual public TimestampBase
55 {
56 public:
58
59 /**
60 * Construct a timestamp Variant from an initialization value.
61 *
62 * @param timestamp Initialization timestamp in microseconds since the Unix Epoch
63 */
65 TimestampVariant(IceUtil::Time time);
66
67 template <class... Ts>
68 TimestampVariant(std::chrono::duration<Ts...> duration) :
70 std::chrono::duration_cast<std::chrono::microseconds>(duration).count())
71 {
72 }
73
74 template <class... Ts>
75 TimestampVariant(std::chrono::time_point<Ts...> timepoint) :
76 TimestampVariant(timepoint.time_since_epoch())
77 {
78 }
79
80 /**
81 * @brief return timestamp in microseconds since the Unix Epoch.
82 */
83 long getTimestamp();
84
85 // inherited from VariantDataClass
86 Ice::ObjectPtr
87 ice_clone() const override
88 {
89 return this->clone();
90 }
91
92 VariantDataClassPtr
93 clone(const Ice::Current& c = Ice::emptyCurrent) const override
94 {
95 return new TimestampVariant(*this);
96 }
97
98 std::string
99 output(const Ice::Current& c = Ice::emptyCurrent) const override
100 {
101 std::stringstream s;
102 s << IceUtil::Time::microSeconds(timestamp).toDateTime();
103 return s.str();
104 }
105
107 getType(const Ice::Current& c = Ice::emptyCurrent) const override
108 {
110 }
111
112 bool
113 validate(const Ice::Current& c = Ice::emptyCurrent) override
114 {
115 return true;
116 }
117
118 friend std::ostream&
119 operator<<(std::ostream& stream, const TimestampVariant& rhs)
120 {
121 stream << "TimestampVariant: " << std::endl << rhs.output() << std::endl;
122 return stream;
123 }
124
127 {
129 }
130
131 IceUtil::Time
133 {
134 return IceUtil::Time::microSeconds(timestamp);
135 }
136
137 public:
138 // serialization
139 void serialize(const armarx::ObjectSerializerBasePtr& serializer,
140 const ::Ice::Current& = Ice::emptyCurrent) const override;
141 void deserialize(const armarx::ObjectSerializerBasePtr& serializer,
142 const ::Ice::Current& = Ice::emptyCurrent) override;
143 };
144} // namespace armarx
std::ostream & operator<<(std::ostream &strm, const AbstractInterface &a)
std::string timestamp()
#define ARMARXCORE_IMPORT_EXPORT
constexpr T c
static IceUtil::Time GetTime(TimeMode timeMode=TimeMode::VirtualTime)
Get the current time.
Definition TimeUtil.cpp:42
Implements a Variant type for timestamps.
VariantTypeId getType(const Ice::Current &c=Ice::emptyCurrent) const override
std::string output(const Ice::Current &c=Ice::emptyCurrent) const override
TimestampVariant(std::chrono::time_point< Ts... > timepoint)
VariantDataClassPtr clone(const Ice::Current &c=Ice::emptyCurrent) const override
bool validate(const Ice::Current &c=Ice::emptyCurrent) override
TimestampVariant(std::chrono::duration< Ts... > duration)
Ice::ObjectPtr ice_clone() const override
static TimestampVariantPtr nowPtr()
static VariantTypeId addTypeName(const std::string &typeName)
Register a new type for the use in a Variant.
Definition Variant.cpp:869
const VariantTypeId Timestamp
This file offers overloads of toIce() and fromIce() functions for STL container types.
Ice::Int VariantTypeId
Definition Variant.h:43
IceInternal::Handle< TimestampVariant > TimestampVariantPtr