aboutsummaryrefslogtreecommitdiff
path: root/src/Platform.cpp
blob: dd816678b6dc3aac42be0c0ae9f8850a210d1427 (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
#include "../include/Platform.hpp"
#include <cassert>

namespace sibs
{
    static int countSetBits(u32 value)
    {
        int count = 0;
        while(value)
        {
            count += (value & 1U);
            value >>= 1U;
        }
        return count;
    }

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

    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)
    {
        switch (platform)
        {
            case PLATFORM_ANY:          return "any";
            case PLATFORM_LINUX:        return "linux";
            case PLATFORM_LINUX32:      return "linux32";
            case PLATFORM_LINUX64:      return "linux64";
            case PLATFORM_WIN:          return "win";
            case PLATFORM_WIN32:        return "win32";
            case PLATFORM_WIN64:        return "win64";
            case PLATFORM_MACOS:        return "macos";
            case PLATFORM_MACOS32:      return "macos32";
            case PLATFORM_MACOS64:      return "macos64";
            case PLATFORM_BSD:          return "bsd";
            case PLATFORM_OPENBSD:      return "openbsd";
            case PLATFORM_OPENBSD32:    return "openbsd32";
            case PLATFORM_OPENBSD64:    return "openbsd64";
            case PLATFORM_HAIKU:        return "haiku";
            case PLATFORM_HAIKU32:      return "haiku32";
            case PLATFORM_HAIKU64:      return "haiku64";
            default:                    assert(false); 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;
    }

    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)(1U << (u32)getLeastSignificantBitSetPosition(platform));
    }
}