Seastar
High performance C++ framework for concurrent servers
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
bool_class.hh
1/*
2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
6 *
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
16 * under the License.
17 */
18/*
19 * Copyright (C) 2016 ScyllaDB.
20 */
21
22#pragma once
23
24#include <ostream>
25#include <fmt/core.h>
26
27namespace seastar {
28
31
57template<typename Tag>
59 bool _value;
60public:
61 static const bool_class yes;
62 static const bool_class no;
63
65 constexpr bool_class() noexcept : _value(false) { }
66
68 constexpr explicit bool_class(bool v) noexcept : _value(v) { }
69
71 explicit operator bool() const noexcept { return _value; }
72
75 return bool_class(x._value || y._value);
76 }
77
80 return bool_class(x._value && y._value);
81 }
82
84 friend bool_class operator!(bool_class x) noexcept {
85 return bool_class(!x._value);
86 }
87
89 friend bool operator==(bool_class x, bool_class y) noexcept = default;
90
92 friend std::ostream& operator<<(std::ostream& os, bool_class v) {
93 return os << (v._value ? "true" : "false");
94 }
95};
96
97template<typename Tag>
98const bool_class<Tag> bool_class<Tag>::yes { true };
99template<typename Tag>
100const bool_class<Tag> bool_class<Tag>::no { false };
101
103
104}
105
106template<typename Tag>
107struct fmt::formatter<seastar::bool_class<Tag>> {
108 constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
109 template <typename FormatContext>
110 auto format(seastar::bool_class<Tag> v, FormatContext& ctx) const{
111 return fmt::format_to(ctx.out(), "{}", bool(v));
112 }
113};
Type-safe boolean.
Definition: bool_class.hh:58
friend bool_class operator&&(bool_class x, bool_class y) noexcept
Logical AND.
Definition: bool_class.hh:79
constexpr bool_class() noexcept
Constructs a bool_class object initialised to false.
Definition: bool_class.hh:65
friend bool operator==(bool_class x, bool_class y) noexcept=default
Equal-to and not-equal-to operators.
friend std::ostream & operator<<(std::ostream &os, bool_class v)
Prints bool_class value to an output stream.
Definition: bool_class.hh:92
friend bool_class operator!(bool_class x) noexcept
Logical NOT.
Definition: bool_class.hh:84
friend bool_class operator||(bool_class x, bool_class y) noexcept
Logical OR.
Definition: bool_class.hh:74
constexpr bool_class(bool v) noexcept
Constructs a bool_class object initialised to v.
Definition: bool_class.hh:68
Seastar API namespace.
Definition: abort_on_ebadf.hh:26