Skip to content

Commit 10dc785

Browse files
committed
{C++11} Implements toInt() conversion function
1 parent 9a6ec25 commit 10dc785

File tree

3 files changed

+37
-13
lines changed

3 files changed

+37
-13
lines changed

HttpStatusCodes_C++11.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,16 @@ enum class Code
111111
NetworkAuthenticationRequired = 511 //!< Indicates that the client needs to authenticate to gain network access.
112112
};
113113

114+
/*! Converts a Code to its corresponding integer value.
115+
* \param code The code to be converted.
116+
* \return The numeric value of \p code.
117+
* \since 1.2.0
118+
*/
119+
inline int toInt(Code code)
120+
{
121+
return static_cast<int>(code);
122+
}
123+
114124
inline bool isInformational(int code) { return (code >= 100 && code < 200); } //!< \returns \c true if the given \p code is an informational code.
115125
inline bool isSuccessful(int code) { return (code >= 200 && code < 300); } //!< \returns \c true if the given \p code is a successful code.
116126
inline bool isRedirection(int code) { return (code >= 300 && code < 400); } //!< \returns \c true if the given \p code is a redirectional code.

README.md

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Initially, the data was taken from [for-GET/know-your-http-well](https://github.
2020
> - [Status Codes Enum](#status-codes-enum)
2121
> - [Category/Class Tests](#categoryclass-tests)
2222
> - [Reason Phrases](#reason-phrases)
23-
> - [QNetworkReply::NetworkError Mapping](#qnetworkreplynetworkerror-mapping)
23+
> - [Conversion Functions](#conversion-functions)
2424
> - [License](#license)
2525
2626

@@ -65,7 +65,7 @@ void printReplyStatus(MyHttpReplyClass reply)
6565
6666
For the complete list of defined enums, see one of the header files.
6767
68-
#### C Variant ####
68+
##### C Variant #####
6969
```c
7070
enum HttpStatus_Code
7171
{
@@ -75,7 +75,7 @@ enum HttpStatus_Code
7575
};
7676
```
7777

78-
#### C++11 Variant ####
78+
##### C++11 Variant #####
7979
```c++
8080
namespace HttpStatus {
8181
enum class Code
@@ -87,7 +87,7 @@ enum class Code
8787
}
8888
```
8989

90-
#### Other Variants ####
90+
##### Other Variants #####
9191
```c++
9292
namespace HttpStatus {
9393
enum Code
@@ -102,7 +102,7 @@ enum Code
102102

103103
### Category/Class Tests ###
104104

105-
#### C Variant ####
105+
##### C Variant #####
106106
```c
107107
char HttpStatus_isInformational(int code);
108108
char HttpStatus_isSuccessful(int code);
@@ -120,7 +120,7 @@ Returns `1` if the given _code_ is either a client error, a server error or any
120120
Non-standard error codes are status codes with a value of 600 or higher.
121121
Returns `0` otherwise.
122122

123-
#### Other Variants ####
123+
##### Other Variants #####
124124
> **Note:** The C++11 variant also provides overloads for `HttpStatus::Code`. So there is no need to cast.
125125
126126
```c++
@@ -145,31 +145,36 @@ Returns `false` otherwise.
145145

146146
### Reason Phrases ###
147147

148-
#### C Variant ####
148+
##### C Variant #####
149149
```c
150150
const char* HttpStatus_reasonPhrase(int code);
151151
```
152152
Returns the HTTP reason phrase string corresponding to the given _code_.
153153
154-
#### C++/C++11 Variants ####
154+
##### C++/C++11 Variants #####
155155
> **Note:** The C++11 variant also provides an overload for `HttpStatus::Code`. So there is no need to cast.
156156
```c++
157157
std::string HttpStatus::reasonPhrase(int code);
158158
```
159159
Returns the HTTP reason phrase string corresponding to the given _code_.
160160

161-
#### Qt Variant ####
161+
##### Qt Variant #####
162162
```c++
163163
QString HttpStatus::reasonPhrase(int code);
164164
```
165165
Returns the HTTP reason phrase string corresponding to the given _code_.
166166
167167
168-
### QNetworkReply::NetworkError Mapping ###
168+
### Conversion Functions ###
169169
170-
> **Note:** Available only in the Qt variant and if the Qt::Network module is available.
170+
##### C++11 Variant #####
171+
```c++
172+
int HttpStatus::toInt(HttpStatus::Code code);
173+
```
174+
Returns the integer value corresponding to a given a _code_.
175+
This is a convenience function as replacement for a `static_cast<int>()`.
171176

172-
#### Qt Variant ####
177+
##### Qt Variant #####
173178
```c++
174179
int HttpStatus::networkErrorToStatusCode(QNetworkReply::NetworkError error);
175180
```
@@ -180,7 +185,7 @@ Otherwise, `-1` is returned.
180185
QNetworkReply::NetworkError HttpStatus::statusCodeToNetworkError(int code);
181186
```
182187
Returns the `QNetworkReply::NetworkError` corresponding to the given _code_ if there is one.
183-
For codes where there is no exact match, the best matchnig "catch all" code (`QNetworkReply::NoError`,
188+
For codes where there is no exact match, the best matching "catch all" code (`QNetworkReply::NoError`,
184189
`QNetworkReply::UnknownContentError`, `QNetworkReply::UnknownServerError` or `QNetworkReply::ProtocolFailure`)
185190
is returned.
186191

tests/C++11VariantTest.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,14 @@ CategoryTesterParams(HttpStatus::Code::HTTPVersionNotSupported, false, fals
4343
));
4444

4545

46+
//####### Conversion Function Test #######
47+
48+
TEST(ConversionFunctionTest, testToInt)
49+
{
50+
ASSERT_EQ(HttpStatus::toInt(HttpStatus::Code::SwitchingProtocols), static_cast<int>(HttpStatus::Code::SwitchingProtocols));
51+
ASSERT_EQ(HttpStatus::toInt(HttpStatus::Code::OK), static_cast<int>(HttpStatus::Code::OK));
52+
ASSERT_EQ(HttpStatus::toInt(HttpStatus::Code::NotExtended), static_cast<int>(HttpStatus::Code::NotExtended));
53+
}
54+
4655

4756
} // namespace Cpp11VariantTests

0 commit comments

Comments
 (0)