summaryrefslogtreecommitdiff
path: root/os_wrappers/Utilities.h
blob: ff7463d0ca96ed2355695488a9130764fe7d8273 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
 * Utilities.h
 *
 * Copyright (C) ST-Ericsson SA 2011
 * Authors: Srimanta Panda <srimanta.panda@stericsson.com>,
 *          Ola Borgelin <ola.borgelin@stericsson.com>,
 *          Karin Hedlund <karin.hedlund@stericsson.com>,
 *          Markus Andersson <markus.m.andersson@stericsson.com> for ST-Ericsson.
 * License terms: 3-clause BSD license
 *
 */

/*
 * @addtogroup Utilities
 * @{
 */

#pragma once
#include <sstream>
#include <string>
#ifdef _WIN32
#include <windows.h>
#endif

using namespace std;

class Utilities
{
public:

    static void sleep(int ms) {
#ifndef _WIN32
        timespec delay;
        delay.tv_sec = 0;
        delay.tv_nsec = ms * 1000000;
        nanosleep(&delay, 0);
#else
        Sleep(ms);
#endif
    }

    template<typename T, typename F>
    static T convert(const F& fromValue) {
        stringstream stream;
        stream << fromValue;

        T toValue;
        stream >> toValue;
        return toValue;
    }

    static void trim(std::string& s) {
        static const std::string whitespace(" \t\r\n");
        size_t start = s.find_first_not_of(whitespace);

        if (std::string::npos != start) {
            s.erase(0, start);
            size_t end = s.find_last_not_of(whitespace);
            s.erase(end + 1);
        } else {
            s.clear();
        }
    }
};

/* @} */