token.h
2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#ifndef OSDEV_COMPONENTS_MQTT_TOKEN_H
#define OSDEV_COMPONENTS_MQTT_TOKEN_H
// std
#include <ostream>
#include <string>
// paho
#include <MQTTAsync.h>
namespace osdev {
namespace components {
namespace mqtt {
/*!
* \brief The Token class defines an operation token
*/
class Token
{
public:
/*! @brief Construct an invalid token.
* The token number is -1 in that case. The client is undefined, in this case empty.
*/
Token();
/*! @brief Constructs token for an operation originating from specific client wrapper.
* @param clientId - Identifies the client wrapper
* @param tokenNr - Identifies the operation done on that client.
*/
Token( const std::string &clientId, std::int32_t tokenNr );
/*! @return True when token has a valid token number, false otherwise. */
bool isValid() const { return -1 == m_token; }
/*! @return The operation token */
const std::string& clientId() const { return m_clientId; }
/*! @return The operation token */
std::int32_t token() const { return m_token; }
/*! @return True if Tokens have the same clientId and token number, false otherwise. */
bool equals( const Token &rhs ) const;
/*!
* @brief Token is ordered.
* First on lexical test of clientId and with same clientId on token number.
*/
bool smallerThan( const Token &rhs ) const;
private:
std::string m_clientId; ///< Identified the client
std::int32_t m_token; ///< Identifies the operation on that client.
};
/**
* @return True if Tokens have the same clientId and token number, false otherwise.
*/
inline bool operator==( const Token &lhs, const Token &rhs )
{
return lhs.equals( rhs );
}
inline bool operator==( const Token &lhs, std::int32_t rhs )
{
return lhs.token() == rhs;
}
inline bool operator==( std::int32_t lhs, const Token &rhs )
{
return lhs == rhs;
}
template <typename TLeft, typename TRight>
inline bool operator!=( const TLeft &lhs, const TRight &rhs )
{
return !( lhs == rhs );
}
/*!
* @return True if Token lhs is smaller than token rhs
*/
inline bool operator<( const Token &lhs, std::int32_t rhs )
{
return lhs.token() < rhs;
}
inline bool operator<( std::int32_t lhs, const Token &rhs )
{
return lhs < rhs.token();
}
inline bool operator<( const Token &lhs, const Token &rhs )
{
return lhs.smallerThan( rhs );
}
/*!
* @brief Stream operator for a Token
*/
std::ostream& operator<<( std::ostream &os, const Token &rhs );
} // End namespace mqtt
} // End namespace components
} // End namespace osdev
#endif // OSDEV_COMPONENTS_MQTT_TOKEN_H