SearchSECODatabaseAPI
Utility.h
1/*
2This program has been developed by students from the bachelor Computer Science at
3Utrecht University within the Software Project course.
4© Copyright Utrecht University (Department of Information and Computing Sciences)
5*/
6
7#pragma once
8#include <string>
9#include <vector>
10#include <functional>
11#include <math.h>
12#include <unistd.h>
13
14#define MAX_RETRIES 3
15#define RETRY_SLEEP 1000000
16
21{
22public:
35 static int safeStoi(std::string str);
36
49 static long long safeStoll(std::string str);
50
63 static long long safeStod(std::string str);
64
71 static void appendBy(std::vector<char> &result, std::string word, char delimiter);
72
81 static void appendBy(std::vector<char> &result, std::vector<std::string> words, char wordSeparator, char endChar);
82
89 static std::vector<std::string> splitStringOn(std::string str, char delimiter);
90
96 static std::string hashToUUIDString(std::string hash);
97
103 static std::string uuidStringToHash(std::string uuid);
104
108 static long long getCurrentTimeSeconds();
109
113 static long long getCurrentTimeMilliSeconds();
114
123 template <class T> static T queryWithRetry(std::function<T()> query)
124 {
125 errno = 0;
126 int retries = 0;
127 T items;
128 do
129 {
130 items = query();
131 if (errno != 0 && errno != ERANGE)
132 {
133 usleep(pow(2, retries) * RETRY_SLEEP);
134 retries++;
135 }
136 } while (errno != 0 && errno != ERANGE && retries <= MAX_RETRIES);
137 if (retries > MAX_RETRIES)
138 {
139 errno = ENETUNREACH;
140 }
141 return items;
142 };
143};
Implements generic functionality.
Definition: Utility.h:21
static T queryWithRetry(std::function< T()> query)
A general template for a query to be performed with retries.
Definition: Utility.h:123
static long long getCurrentTimeMilliSeconds()
Gets the current time since epoch in milliseconds, represented as an integer.
Definition: Utility.cpp:123
static std::vector< std::string > splitStringOn(std::string str, char delimiter)
Splits a string on a special character.
Definition: Utility.cpp:93
static void appendBy(std::vector< char > &result, std::string word, char delimiter)
Appends a char vector by a string and adds a special character at the end.
Definition: Utility.cpp:69
static long long safeStod(std::string str)
Safely attempts to convert a string to a corresponding double.
Definition: Utility.cpp:50
static long long getCurrentTimeSeconds()
Gets the current time since epoch in seconds, represented as an integer.
Definition: Utility.cpp:117
static long long safeStoll(std::string str)
Safely attempts to convert a string to a corresponding long long.
Definition: Utility.cpp:31
static std::string uuidStringToHash(std::string uuid)
Changes a string with the format of a uuid to a string with the format of a hash.
Definition: Utility.cpp:111
static std::string hashToUUIDString(std::string hash)
Changes a string with the format of a hash to a string with the format of a UUID.
Definition: Utility.cpp:105
static int safeStoi(std::string str)
Safely attempts to convert a string to a corresponding int.
Definition: Utility.cpp:12