~aleteoryx/muditaos

ref: a56e8dbe8e95ca9bea2eae7a5039ffcd876f3331 muditaos/module-db/Database/Field.cpp -rw-r--r-- 2.1 KiB
a56e8dbe — Lefucjusz [MOS-361] Fix improper fonts in navbar 2 years ago
                                                                                
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "Field.hpp"
#include <log/log.hpp>
#include <functional>

const char *Field::getCString() const
{
    return mValue.c_str();
}

template <class T>
static T lget(T &val, const std::string &mValue, std::function<T()> foo)
{
    val = 0;
    if (mValue.empty()) {
        return val;
    }
    try {
        return foo();
    }
    catch (...) {
        LOG_FATAL("Can't convert value to: %s", typeid(T).name());
    }
    return val;
}

float Field::getFloat() const
{
    float ret;
    return lget<float>(ret, mValue, [=]() -> float { return std::stof(mValue); });
}

bool Field::getBool() const
{
    bool ret;
    return lget<bool>(ret, mValue, [=]() -> bool { return std::stoi(mValue) > 0; });
}

double Field::getDouble() const
{
    double ret;
    return lget<double>(ret, mValue, [=]() -> double { return std::stod(mValue); });
}

int8_t Field::getInt8() const
{
    return !mValue.empty() ? static_cast<int8_t>(atol(mValue.c_str())) : int8_t(0);
}

int32_t Field::getInt32() const
{
    return !mValue.empty() ? static_cast<int32_t>(atol(mValue.c_str())) : int32_t(0);
}

uint8_t Field::getUInt8() const
{
    return !mValue.empty() ? static_cast<uint8_t>(atol(mValue.c_str())) : uint8_t(0);
}

uint16_t Field::getUInt16() const
{
    return !mValue.empty() ? static_cast<uint16_t>(atol(mValue.c_str())) : uint16_t(0);
}

int16_t Field::getInt16() const
{
    return !mValue.empty() ? static_cast<int16_t>(atol(mValue.c_str())) : int16_t(0);
}

uint32_t Field::getUInt32() const
{
    uint32_t ret;
    return lget<uint32_t>(ret, mValue, [=]() -> unsigned long { return std::stoul(mValue); });
}

uint64_t Field::getUInt64() const
{
    uint64_t ret;
    return lget<uint64_t>(ret, mValue, [=]() -> unsigned long long { return std::stoull(mValue); });
}

int64_t Field::getInt64() const
{
    int64_t ret;
    return lget<int64_t>(ret, mValue, [=]() -> long long { return std::stoll(mValue); });
}

void Field::setValue(const char *value)
{
    mValue = value;
}