aboutsummaryrefslogtreecommitdiff
path: root/src/Platform.cpp
blob: f92e1eab5e249ba7760ded1cc2eed074ed88b1fa (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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "../include/Platform.hpp"
#include <cassert>
#include <algorithm>

namespace sibs
{
    static std::unordered_map<Platform, const char*> platformNames;
    static bool platformNamesSet = false;
    static int countSetBits(u64 value)
    {
        int count = 0;
        while(value)
        {
            count += (value & 1ULL);
            value >>= 1ULL;
        }
        return count;
    }

    // Returns -1 if no bit is set
    static u64 getLeastSignificantBitSetPosition(u64 value)
    {
        for(u64 i = 0; i < sizeof(u64); ++i)
        {
            if(value & 1ULL)
                return i;
            value >>= 1ULL;
        }
        return -1ULL;
    }

    bool containsPlatform(const std::vector<Platform> &platforms, Platform platform)
    {
        for (Platform vecPlatform : platforms)
        {
            if(platform & vecPlatform)
                return true;
        }
        return false;
    }

    const char* asString(Platform platform)
    {
        if(!platformNamesSet)
        {
            platformNamesSet = true;
            for(auto &it : PLATFORM_BY_NAME)
            {
                platformNames[it.second] = it.first.data;
            }
        }

        auto it = platformNames.find(platform);
        if(it != platformNames.end())
            return it->second;
        return nullptr;
    }

    Platform getPlatformByName(StringView name)
    {
        auto it = PLATFORM_BY_NAME.find(name);
        if(it != PLATFORM_BY_NAME.end())
            return it->second;
        return PLATFORM_INVALID;
    }

    static std::string join(const std::vector<std::string> &strings)
    {
        std::string result;
        ssize size = strings.size();
        ssize i = 0;
        for(const std::string &str: strings)
        {
            result += str;
            if(i == size - 2)
                result += " and ";
            else if(i < size - 2)
                result += ", ";
            ++i;
        }
        return result;
    }

    std::string getPlatformListFormatted()
    {
        std::vector<std::string> platformsSorted;
        for(auto &it: PLATFORM_BY_NAME)
        {
            platformsSorted.push_back({ it.first.data, it.first.size });
        }
        std::sort(platformsSorted.begin(), platformsSorted.end());
        return join(platformsSorted);
    }

    bool isSamePlatformFamily(Platform a, Platform b)
    {
        return a & b;
    }

    bool isBaseForPlatform(Platform base, Platform platform)
    {
        return base == PLATFORM_ANY || base == platform || (isSamePlatformFamily(platform, base) && countSetBits(base) < countSetBits(platform));
    }

    Platform getPlatformGenericType(Platform platform)
    {
        if(platform == PLATFORM_INVALID || platform == PLATFORM_ANY)
            return PLATFORM_INVALID;
        return (Platform)(1ULL << (u64)getLeastSignificantBitSetPosition(platform));
    }
}