API Reference

The following section outlines the API of LibOS.

Macros

There are 2 ways to write OS-specific code:

OS_UNIX

Defined if OS is Unix-like.

OS_LINUX

Defined if OS is Linux.

OS_WINDOWS

Defined if OS is Windows.

Example:

#if defined(OS_WINDOWS)
     /* Some Windows-only code */
#endif

Warning

The following is incorrect usage:

#if OS_WINDOWS
    /* Some Windows-only code */
#endif

This will cause compilation error on non-Windows OS, because this macro won’t be defined. Use IS_OS_WINDOWS instead.

IS_OS_UNIX

Set to 1 if OS is Unix-like, 0 otherwise.

IS_OS_LINUX

Set to 1 if OS is Linux, 0 otherwise.

IS_OS_WINDOWS

Set to 1 if OS is Windows, 0 otherwise.

Tip

If your code is valid for any platform, you may want to use os::type() with if constexpr.

Example:

#if IS_OS_WINDOWS
     /* Some Windows-only code */
#endif

Warning

The following is incorrect usage:

#if defined(IS_OS_WINDOWS)
    /* Some Windows-only code */
#endif

This will be always true, as this macro always defined. Use OS_WINDOWS instead.

Semantic Version

struct version

Struct to represent semantic versioning.

Public Functions

inline constexpr version(unsigned major = 0, unsigned minor = 0, unsigned patch = 0) noexcept

Construct version from major, minor and patch numbers.

inline explicit constexpr version(std::string_view str) noexcept

Construct version from string.

Note

If string format is wrong, no exception is thrown. All successfully read values will be saved.

Parameters

str – String with format “major.minor.patch”

inline constexpr bool operator==(const version &rhs) const noexcept

Check version for equality.

inline constexpr bool operator<(const version &rhs) const noexcept

Check if version is less.

inline constexpr bool operator<=(const version &rhs) const noexcept

Check if version is less or equal.

inline constexpr bool operator>(const version &rhs) const noexcept

Check if version is greater.

inline constexpr bool operator>=(const version &rhs) const noexcept

Check if version is greater or equal.

inline constexpr bool operator!=(const version &rhs) const noexcept

Check if version is not equal.

inline std::string str() const

Get version string in format “major.minor.patch”.

Public Members

unsigned major = 0

Major version.

unsigned minor = 0

Minor version.

unsigned patch = 0

Patch or build version.

OS Info

enum os::type_t

Possible OS types.

Values:

enumerator undefined
enumerator linux
enumerator windows
enumerator macos
constexpr type_t os::type() noexcept

Get type of OS at compile time.

Usage:

if constexpr (os::type() == os::linux)
{
    /* Code for linux (but must compile on every OS) */
}
std::string os::name()

Get OS name.

Returns

  • Linux:

    • Distributive’s name (e.g. "Ubuntu")

    • "Linux", if /etc/os-release not found

  • MacOS: "macOS"

  • Windows: "Windows"

std::string os::pretty_name()

Get OS name with version.

std::string os::codename()

Get OS codename.

Returns

  • Linux: codename, if present

  • MacOS: codename

  • Windows: ""

::version os::version()

Get OS major, minor and patch version as integers.

std::string os::version_string()

Get OS version as string.

struct os::info_t

Full OS info.

Public Members

type_t type = os::undefined

OS type.

std::string name

OS name.

std::string pretty_name

OS name with version.

std::string codename

OS codename (if exists)

::version version

OS major, minor and patch version as integers.

std::string version_string

OS version as string.

const info_t &os::info()

Get full OS info.

Obtaining OS info is very expensive. Hence, it’s statically allocated and read exactly once.

Returns

const info_t& Ref to OS info

Kernel Info

std::string os::kernel::name()

Get OS Kernel name.

Returns

  • Linux: "Linux"

  • MacOS: "Darwin"

  • Windows: "Windows NT"

::version os::kernel::version()

Get OS Kernel major, minor and patch version as integers.

std::string os::kernel::version_string()

Get OS Kernel version as string.

struct os::kernel::info_t

Full OS Kernel info.

Public Members

std::string name

OS Kernel name.

::version version

OS Kernel major, minor and patch version as integers.

std::string version_string

OS Kernel version as string.

const info_t &os::kernel::info()

Get full OS Kernel info.

Obtaining OS Kernel info is very expensive. Hence, it’s statically allocated and read exactly once.

Returns

const info_t& Ref to OS Kernel info

Keyboard Input

enum os::keyboard::vk

Virtual keys.

Virtual-key code is a device-independent value defined by the system that identifies the purpose of a key. The output of sending a virtual key is affected by current keyboard layout and mapping.

Note

Some enum values are different on different OS.

Values:

enumerator Key_0
enumerator Key_1
enumerator Key_2
enumerator Key_3
enumerator Key_4
enumerator Key_5
enumerator Key_6
enumerator Key_7
enumerator Key_8
enumerator Key_9
enumerator A
enumerator B
enumerator C
enumerator D
enumerator E
enumerator F
enumerator G
enumerator H
enumerator I
enumerator J
enumerator K
enumerator L
enumerator M
enumerator N
enumerator O
enumerator P
enumerator Q
enumerator R
enumerator S
enumerator T
enumerator U
enumerator V
enumerator W
enumerator X
enumerator Y
enumerator Z
enumerator Shift
enumerator Control
enumerator Ctrl
enumerator Caps
enumerator Enter
enumerator Esc
enumerator Del

Tip

You can shorten enum name with using os::keyboard::vk.

Tip

You can make combination of virtual keys with vk::Shift + vk::A.

struct os::keyboard::combination

Combination of keys.

Public Functions

inline combination(vk key)

Make combination from virtual keys.

inline combination(std::initializer_list<vk> keys = {})

Make combination from list of virtual keys.

inline combination &operator+=(const combination &combo)

Append a combination.

inline combination operator+(const combination &combo) const

Concatinate 2 combinations.

Public Members

std::unordered_set<vk> keys

Array of virtual keys.

bool os::keyboard::is_pressed(const combination &combo)

Check if every key in combination is pressed.

combination os::keyboard::pressed_keys()

Get combination of all pressed keys on a keyboard.

void os::keyboard::press(const combination &combo)

Press combination of keys (until release())

void os::keyboard::release(const combination &combo)

Release combination of keys.

inline void os::keyboard::click(const combination &combo)

press() and release() combination of keys