HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
audio_device_id.hpp
1// Copyright Take Vos 2021.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "../pickle.hpp"
8#include "../exception.hpp"
9#include "../check.hpp"
10#include <array>
11
12namespace hi::inline v1 {
13
15public:
16 static constexpr char none = 0;
17 static constexpr char win32 = 1;
18 static constexpr char macos = 2;
19 static constexpr char asio = 3;
20
21 audio_device_id() noexcept : _v{} {}
22
23 audio_device_id(char type, wchar_t const *id) noexcept;
24 constexpr audio_device_id(audio_device_id const &) noexcept = default;
25 constexpr audio_device_id(audio_device_id &&) noexcept = default;
26 constexpr audio_device_id &operator=(audio_device_id const &) noexcept = default;
27 constexpr audio_device_id &operator=(audio_device_id &&) noexcept = default;
28
29 explicit operator bool() const noexcept
30 {
31 return _v[0] != none;
32 }
33
34 [[nodiscard]] friend bool operator==(audio_device_id const &lhs, audio_device_id const &rhs) noexcept = default;
35
36private:
38
39 friend struct pickle<audio_device_id>;
40};
41
42template<>
44 [[nodiscard]] datum encode(audio_device_id const &rhs) const noexcept
45 {
46 auto r = std::string{};
47
48 auto t = rhs._v[0];
49 if (t == audio_device_id::none) {
50 return datum{std::move(r)};
51 } else if (t == audio_device_id::win32) {
52 r += 'w';
53 for (auto i = 1_uz; i != size(rhs._v); ++i) {
54 if (auto c = rhs._v[i]) {
55 r += c;
56 } else {
57 break;
58 }
59 }
60 return datum{std::move(r)};
61
62 } else {
63 hi_not_implemented();
64 }
65 }
66
67 [[nodiscard]] audio_device_id decode(std::string const &rhs) const
68 {
69 auto r = audio_device_id{};
70 if (rhs.empty()) {
71 return r;
72
73 } else {
74 auto t = rhs[0];
75 if (t == 'w') {
76 hi_parse_check(size(rhs) <= size(r._v), "win32-audio_device_id pickle size to large {}", rhs);
77
78 r._v[0] = audio_device_id::win32;
79 auto i = 1_uz;
80 for (; i != size(rhs); ++i) {
81 r._v[i] = rhs[i];
82 }
83 if (i != size(r._v)) {
84 r._v[i] = 0;
85 }
86 return r;
87
88 } else {
89 throw parse_error(std::format("audio_device_id pickle unknown type {}", t));
90 }
91 }
92 }
93
94 [[nodiscard]] audio_device_id decode(datum rhs) const
95 {
96 if (auto *s = get_if<std::string>(rhs)) {
97 return decode(*s);
98
99 } else {
100 throw parse_error(std::format("audio_device_id must be encoded as a string, got {}", rhs));
101 }
102 }
103};
104
105} // namespace hi::inline v1
Definition audio_device_id.hpp:14
A dynamic data type.
Definition datum.hpp:209
Exception thrown during parsing on an error.
Definition exception.hpp:25
Encode and decode a type to and from a UTF-8 string.
Definition pickle.hpp:22
T empty(T... args)
T move(T... args)