Skip to content

Commit 4e75689

Browse files
authored
Merge pull request #6 from j-ulrich/feature/pr5_toInt
{C++11} Implements toInt() conversion function
2 parents 9a6ec25 + 15d90d0 commit 4e75689

File tree

8 files changed

+58
-21
lines changed

8 files changed

+58
-21
lines changed

.appveyor.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ environment:
2020
APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2013
2121
VCVARS_COMMANDLINE: '"C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat" x86'
2222

23+
cache:
24+
- C:\.hunter -> .appveyor.yml, **\CMakeLists.txt, **\*.cmake
25+
2326
before_build:
2427
- call %VCVARS_COMMANDLINE%
2528
- set Path=%QT_DIR%\bin;%Path%

CHANGELOG.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,19 @@ This changelog follows the [Keep a Changelog](http://keepachangelog.com) format.
1010
---
1111

1212

13-
## [1.1.1] - 2018-11-14 ##
13+
## [1.2.0] - 2019-01-06 ##
14+
15+
### Added ###
16+
- [#5]{C++11} `toInt()` conversion function.
17+
18+
19+
---
20+
21+
22+
## [1.1.1] - 2018-11-14 ##
1423

1524
### Fixed ###
16-
- Some QNetworkReply::NetworkError codes were not available in Qt before 5.3.
25+
- {Qt} Some QNetworkReply::NetworkError codes were not available in Qt before 5.3.
1726

1827

1928
---
@@ -26,7 +35,7 @@ Adds missing status codes from the IANA registry.
2635

2736

2837
### Fixed ###
29-
- Removed redundant "HttpStatus_XXX" documentation comments in C variant.
38+
- {C} Removed redundant "HttpStatus_XXX" documentation comments.
3039

3140

3241
---
@@ -40,11 +49,12 @@ Initial (actually unversioned) release.
4049
- Status codes
4150
- Category/class tests (`isXXX()` functions)
4251
- Reason phrases
43-
- `QNetworkReply::NetworkError` mapping
52+
- {Qt} `QNetworkReply::NetworkError` mapping
4453

4554

4655
---
4756

4857

58+
[1.2.0]: https://github.com/j-ulrich/http-status-codes-cpp/releases/tag/1.2.0
4959
[1.1.1]: https://github.com/j-ulrich/http-status-codes-cpp/releases/tag/1.1.1
5060
[1.1.0]: https://github.com/j-ulrich/http-status-codes-cpp/releases/tag/1.1.0

HttpStatusCodes_C++.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* https://github.com/j-ulrich/http-status-codes-cpp
66
*
7-
* \version 1.1.1
7+
* \version 1.2.0
88
* \author Jochen Ulrich <[email protected]>
99
* \copyright Licensed under Creative Commons CC0 (http://creativecommons.org/publicdomain/zero/1.0/)
1010
*/

HttpStatusCodes_C++11.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* https://github.com/j-ulrich/http-status-codes-cpp
66
*
7-
* \version 1.1.1
7+
* \version 1.2.0
88
* \author Jochen Ulrich <[email protected]>
99
* \copyright Licensed under Creative Commons CC0 (http://creativecommons.org/publicdomain/zero/1.0/)
1010
*/
@@ -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.

HttpStatusCodes_C.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* https://github.com/j-ulrich/http-status-codes-cpp
66
*
7-
* \version 1.1.1
7+
* \version 1.2.0
88
* \author Jochen Ulrich <[email protected]>
99
* \copyright Licensed under Creative Commons CC0 (http://creativecommons.org/publicdomain/zero/1.0/)
1010
*/

HttpStatusCodes_Qt.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* https://github.com/j-ulrich/http-status-codes-cpp
66
*
7-
* \version 1.1.1
7+
* \version 1.2.0
88
* \author Jochen Ulrich <[email protected]>
99
* \copyright Licensed under Creative Commons CC0 (http://creativecommons.org/publicdomain/zero/1.0/)
1010
*/

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)