Keyple Util C++ Library 2.3.0.5-SNAPSHOT
Reference Terminal Reader API for C++
System.h
Go to the documentation of this file.
1/**************************************************************************************************
2 * Copyright (c) 2021 Calypso Networks Association https://calypsonet.org/ *
3 * *
4 * See the NOTICE file(s) distributed with this work for additional information regarding *
5 * copyright ownership. *
6 * *
7 * This program and the accompanying materials are made available under the terms of the Eclipse *
8 * Public License 2.0 which is available at http://www.eclipse.org/legal/epl-2.0 *
9 * *
10 * SPDX-License-Identifier: EPL-2.0 *
11 **************************************************************************************************/
12
13#pragma once
14
15#include <math.h>
16#include <vector>
17#include <memory>
18
19#if defined(_WIN32)
20#include <windows.h>
21#endif
22
23/* Keyple Core Util */
25
26namespace keyple {
27namespace core {
28namespace util {
29namespace cpp {
30
32
33class System {
34public:
38 static unsigned long long nanoTime()
39 {
40#if defined(WIN32)
41 SYSTEMTIME time;
42 GetSystemTime(&time);
43 return static_cast<long long>((time.wHour * 3600 * 1000 +
44 time.wMinute * 60 * 1000 +
45 time.wSecond * 1000 +
46 time.wMilliseconds) * pow(10, 6));
47#else
48 timespec ts;
49 // clock_gettime(CLOCK_MONOTONIC, &ts); // Works on FreeBSD
50 clock_gettime(CLOCK_REALTIME, &ts);
51 return ts.tv_sec * pow(10, 9) + ts.tv_nsec;
52#endif
53 }
54
58 static void arraycopy(const std::vector<char>& src, size_t srcPos,
59 std::vector<char>& dest, size_t destPos, size_t length)
60 {
61 for (size_t i = 0; i < length; i++) {
62 dest[destPos + i] = src[srcPos + i];
63 }
64 }
65
69 static void arraycopy(const std::vector<uint8_t>& src, size_t srcPos,
70 std::vector<char>& dest, size_t destPos, size_t length)
71 {
72 if ((srcPos + length) >= src.size()) {
73 throw ArrayIndexOutOfBoundsException("pos + offset > src size");
74 }
75
76 if ((srcPos + length) >= src.size()) {
77 throw ArrayIndexOutOfBoundsException("pos + offset > dest size");
78 }
79
80 for (size_t i = 0; i < length; i++) {
81 dest[destPos + i] = static_cast<char>(src[srcPos + i]);
82 }
83 }
84
85 static void arraycopy(const std::vector<uint8_t>& src, size_t srcPos,
86 std::vector<uint8_t>& dest, size_t destPos, size_t length)
87 {
88 if ((srcPos + length) > src.size()) {
89 throw ArrayIndexOutOfBoundsException("pos + offset > src size");
90 }
91
92 if ((destPos + length) > dest.size()) {
93 throw ArrayIndexOutOfBoundsException("pos + offset > dest size");
94 }
95
96 for (size_t i = 0; i < length; i++) {
97 dest[destPos + i] = src[srcPos + i];
98 }
99 }
100
104 static unsigned long long currentTimeMillis()
105 {
106 return (unsigned long long)(nanoTime() / pow(10, 6));
107 }
108
112 template<typename T>
113 static unsigned int identityHashCode(const std::shared_ptr<T> t)
114 {
115 if (t == nullptr) {
116 return 0;
117 }
118
119 return static_cast<int>(reinterpret_cast<unsigned long long>(t.get()));
120 }
121};
122
123}
124}
125}
126}
static unsigned int identityHashCode(const std::shared_ptr< T > t)
Definition: System.h:113
static void arraycopy(const std::vector< uint8_t > &src, size_t srcPos, std::vector< char > &dest, size_t destPos, size_t length)
Definition: System.h:69
static unsigned long long nanoTime()
Definition: System.h:38
static void arraycopy(const std::vector< uint8_t > &src, size_t srcPos, std::vector< uint8_t > &dest, size_t destPos, size_t length)
Definition: System.h:85
static void arraycopy(const std::vector< char > &src, size_t srcPos, std::vector< char > &dest, size_t destPos, size_t length)
Definition: System.h:58
static unsigned long long currentTimeMillis()
Definition: System.h:104