paho-mqtt-cpp
MQTT C++ Client for POSIX and Windows
types.h
Go to the documentation of this file.
1 
7 /*******************************************************************************
8  * Copyright (c) 2015-2017 Frank Pagliughi <fpagliughi@mindspring.com>
9  *
10  * All rights reserved. This program and the accompanying materials
11  * are made available under the terms of the Eclipse Public License v2.0
12  * and Eclipse Distribution License v1.0 which accompany this distribution.
13  *
14  * The Eclipse Public License is available at
15  * http://www.eclipse.org/legal/epl-v20.html
16  * and the Eclipse Distribution License is available at
17  * http://www.eclipse.org/org/documents/edl-v10.php.
18  *
19  * Contributors:
20  * Frank Pagliughi - initial implementation and documentation
21  *******************************************************************************/
22 
23 #ifndef __mqtt_types_h
24 #define __mqtt_types_h
25 
26 #include <string>
27 #include <vector>
28 #include <memory>
29 #include <chrono>
30 
31 namespace mqtt {
32 
34 // Basic data types
35 
37 using byte = uint8_t;
38 
40 using string = std::string;
43 
45 using string_ptr = std::shared_ptr<const string>;
47 using binary_ptr = std::shared_ptr<const binary>;
48 
50 // General protocol enumerations
51 
57 enum ReasonCode {
58  SUCCESS = 0,
77  SERVER_BUSY = 137,
78  BANNED = 138,
97  SERVER_MOVED = 157,
103  MQTTPP_V3_CODE = 255 // This is not a protocol code; used internally by the library
104 };
105 
107 // Time functions
108 
115 template <class Rep, class Period>
116 std::chrono::seconds to_seconds(const std::chrono::duration<Rep, Period>& dur) {
117  return std::chrono::duration_cast<std::chrono::seconds>(dur);
118 }
119 
126 template <class Rep, class Period>
127 long to_seconds_count(const std::chrono::duration<Rep, Period>& dur) {
128  return (long) to_seconds(dur).count();
129 }
130 
137 template <class Rep, class Period>
138 std::chrono::milliseconds to_milliseconds(const std::chrono::duration<Rep, Period>& dur) {
139  return std::chrono::duration_cast<std::chrono::milliseconds>(dur);
140 }
141 
148 template <class Rep, class Period>
149 long to_milliseconds_count(const std::chrono::duration<Rep, Period>& dur) {
150  return (long) to_milliseconds(dur).count();
151 }
152 
154 // Misc
155 
161 inline bool to_bool(int n) { return n != 0; }
167 inline int to_int(bool b) { return b ? (!0) : 0; }
168 
175 inline string to_string(const char* cstr) {
176  return cstr ? string(cstr) : string();
177 }
178 
180 // end namespace mqtt
181 }
182 
183 #endif // __mqtt_types_h
184 
uint8_t byte
Definition: types.h:37
std::shared_ptr< const string > string_ptr
Definition: types.h:45
Definition: types.h:81
Definition: types.h:88
Definition: types.h:85
Definition: types.h:100
Definition: types.h:67
Definition: types.h:69
Definition: types.h:80
bool to_bool(int n)
Definition: types.h:161
std::string string
Definition: types.h:40
Definition: types.h:68
Definition: types.h:74
Definition: types.h:91
Definition: types.h:92
Definition: types.h:66
Definition: types.h:94
Definition: types.h:79
string to_string(const char *cstr)
Definition: types.h:175
Definition: types.h:89
long to_milliseconds_count(const std::chrono::duration< Rep, Period > &dur)
Definition: types.h:149
Definition: types.h:75
Definition: types.h:86
Definition: types.h:99
Definition: types.h:96
Definition: types.h:62
Definition: types.h:58
std::string binary
Definition: types.h:42
Definition: types.h:61
Definition: types.h:73
Definition: types.h:76
ReasonCode
Definition: types.h:57
Definition: types.h:95
std::chrono::seconds to_seconds(const std::chrono::duration< Rep, Period > &dur)
Definition: types.h:116
Definition: types.h:103
Definition: types.h:84
Definition: types.h:82
Definition: types.h:65
Definition: types.h:78
std::chrono::milliseconds to_milliseconds(const std::chrono::duration< Rep, Period > &dur)
Definition: types.h:138
Definition: types.h:64
long to_seconds_count(const std::chrono::duration< Rep, Period > &dur)
Definition: types.h:127
int to_int(bool b)
Definition: types.h:167
Definition: types.h:60
Definition: async_client.h:49
Definition: types.h:70
std::shared_ptr< const binary > binary_ptr
Definition: types.h:47
Definition: types.h:93
Definition: types.h:97
Definition: types.h:59
Definition: types.h:83
Definition: types.h:90
Definition: types.h:77
Definition: types.h:87