SNode.C
Loading...
Searching...
No Matches
State.cpp
Go to the documentation of this file.
1/*
2 * SNode.C - a slim toolkit for network communication
3 * Copyright (C) Volker Christian <me@vchrist.at>
4 * 2020, 2021, 2022, 2023, 2024, 2025
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "core/socket/State.h"
21
22#ifndef DOXYGEN_SHOULD_SKIP_THIS
23
24#include <cerrno>
25#include <cstring>
26#include <string>
27
28#endif // DOXYGEN_SHOULD_SKIP_THIS
29
30namespace core::socket {
31
32 State::State(const int& state, const std::string& file, const int& line)
33 : state(state)
34 , file(file)
35 , line(line)
36 , errnum(errno)
37 , errstr(errnum != 0 ? std::strerror(errnum) : "") {
38 }
39
40 State::State(const int& state, const std::string& file, const int& line, int errnum, const std::string& errstr)
41 : state(state)
42 , file(file)
43 , line(line)
44 , errnum(errnum)
45 , errstr(errnum != 0 ? errstr : "") {
46 }
47
48 State::operator int() const {
49 return state;
50 }
51
52 bool State::operator==(const int& state) const {
53 return this->state == state;
54 }
55
56 State& State::operator=(int state) {
57 this->state = state;
58
59 return *this;
60 }
61
62 State& State::operator|=(int state) {
63 this->state |= state;
64
65 return *this;
66 }
67
68 State& State::operator&=(int state) {
69 this->state &= state;
70
71 return *this;
72 }
73
74 State& State::operator^=(int state) {
75 this->state ^= state;
76
77 return *this;
78 }
79
80 State State::operator|(int state) {
81 return State(this->state | state, this->file, this->line, this->errnum, this->errstr);
82 }
83
84 State State::operator&(int state) {
85 return State(this->state & state, this->file, this->line, this->errnum, this->errstr);
86 }
87
88 State State::operator^(int state) {
89 return State(this->state ^ state, this->file, this->line, this->errnum, this->errstr);
90 }
91
92 std::string State::what() const {
93 std::string stateString;
94
95 switch (state & ~NO_RETRY) {
96 case OK:
97 stateString = "OK";
98 break;
99 case DISABLED:
100 stateString = "DISABLED";
101 break;
102 case ERROR:
103 [[fallthrough]];
104 case FATAL:
105 stateString = errstr;
106 break;
107 }
108
109 return stateString.append(" [").append(std::to_string(errnum)).append("]");
110 }
111
112 std::string State::where() const {
113 return file + "(" + std::to_string(line) + ")";
114 }
115
116} // namespace core::socket
State & operator^=(int state)
Definition State.cpp:74
State & operator&=(int state)
Definition State.cpp:68
operator int() const
Definition State.cpp:48
bool operator==(const int &state) const
Definition State.cpp:52
State operator&(int state)
Definition State.cpp:84
State & operator=(int state)
Definition State.cpp:56
static constexpr int ERROR
Definition State.h:35
std::string what() const
Definition State.cpp:92
State(const int &state, const std::string &file, const int &line)
Definition State.cpp:32
State operator^(int state)
Definition State.cpp:88
State(const int &state, const std::string &file, const int &line, int errnum, const std::string &errstr)
Definition State.cpp:40
std::string where() const
Definition State.cpp:112
static constexpr int NO_RETRY
Definition State.h:37
State operator|(int state)
Definition State.cpp:80
State & operator|=(int state)
Definition State.cpp:62