Qpid Proton C++ API 0.41.1
Loading...
Searching...
No Matches
message_id.hpp
Go to the documentation of this file.
1#ifndef PROTON_MESSAGE_ID_HPP
2#define PROTON_MESSAGE_ID_HPP
3
4/*
5 *
6 * Licensed to the Apache Software Foundation (ASF) under one
7 * or more contributor license agreements. See the NOTICE file
8 * distributed with this work for additional information
9 * regarding copyright ownership. The ASF licenses this file
10 * to you under the Apache License, Version 2.0 (the
11 * "License"); you may not use this file except in compliance
12 * with the License. You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing,
17 * software distributed under the License is distributed on an
18 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19 * KIND, either express or implied. See the License for the
20 * specific language governing permissions and limitations
21 * under the License.
22 *
23 */
24
25#include "./binary.hpp"
26#include "./scalar_base.hpp"
27#include "./uuid.hpp"
28
29#include <proton/type_compat.h>
30
31#include <string>
32
35
36namespace proton {
37
47class message_id : public scalar_base {
48 public:
50 message_id() = default;
51
53 template <class T> message_id(const T& x) { *this = x; }
54
59 message_id& operator=(uint64_t x) { put_(x); return *this; }
60 message_id& operator=(const uuid& x) { put_(x); return *this; }
61 message_id& operator=(const binary& x) { put_(x); return *this; }
62 message_id& operator=(const std::string& x) { put_(x); return *this; }
63 message_id& operator=(const char* x) { put_(x); return *this; }
65
66 private:
67 message_id(const pn_atom_t& a): scalar_base(a) {}
68
70 friend class message;
71 friend class codec::decoder;
73};
74
77template <class T> T get(const message_id& x);
79
81template<> inline uint64_t get<uint64_t>(const message_id& x) { return internal::get<uint64_t>(x); }
83template<> inline uuid get<uuid>(const message_id& x) { return internal::get<uuid>(x); }
85template<> inline binary get<binary>(const message_id& x) { return internal::get<binary>(x); }
87template<> inline std::string get<std::string>(const message_id& x) { return internal::get<std::string>(x); }
88
91template<class T> T coerce(const message_id& x) { return internal::coerce<T>(x); }
92
93} // proton
94
96template <> struct std::hash<proton::message_id> {
97 std::size_t operator()(const proton::message_id& s) const {
98 std::size_t h;
99 auto type = s.type();
100 switch(type) {
101 case proton::ULONG: h = std::hash<uint64_t>{}(proton::internal::get<uint64_t>(s)); break;
102 case proton::UUID: h = std::hash<proton::uuid>{}(proton::internal::get<proton::uuid>(s)); break;
103 case proton::BINARY: h = std::hash<proton::binary>{}(proton::internal::get<proton::binary>(s)); break;
104 case proton::STRING: h = std::hash<std::string>{}(proton::internal::get<std::string>(s)); break;
105 default: throw proton::conversion_error("invalid message_id type "+type_name(s.type()));
106 }
107 auto type_hash = std::hash<proton::type_id>{}(type);
108 // Combine the two hashes - see https://stackoverflow.com/a/5889238
109 return h ^ (type_hash + 0x9e3779b9 + (h << 6) + (h >> 2));
110}
111
112};
113
114#endif // PROTON_MESSAGE_ID_HPP
Arbitrary binary data.
Arbitrary binary data.
Definition binary.hpp:40
An AMQP message ID.
Definition message_id.hpp:47
message_id(const T &x)
Construct from any type that can be assigned.
Definition message_id.hpp:53
T coerce(const message_id &x)
Coerce the contained value to type T.
Definition message_id.hpp:91
message_id()=default
An empty message_id.
message_id & operator=(const char *x)
Treated as amqp::STRING.
Definition message_id.hpp:63
The base class for scalar types.
Definition scalar_base.hpp:60
type_id type() const
AMQP type of data stored in the scalar.
A 16-byte universally unique identifier.
Definition uuid.hpp:37
The main Proton namespace.
Definition annotation_key.hpp:33
uuid get< uuid >(const message_id &x)
Get the uuid value or throw conversion_error.
Definition message_id.hpp:83
T get(const scalar &s)
Get a contained value of type T.
Definition scalar.hpp:60
@ ULONG
Unsigned 64-bit integer.
Definition type_id.hpp:47
@ UUID
16-byte UUID.
Definition type_id.hpp:55
@ BINARY
Variable-length sequence of bytes.
Definition type_id.hpp:56
@ STRING
Variable-length utf8-encoded string.
Definition type_id.hpp:57
binary get< binary >(const message_id &x)
Get the binary value or throw conversion_error.
Definition message_id.hpp:85
std::string get< std::string >(const message_id &x)
Get the std::string value or throw conversion_error.
Definition message_id.hpp:87
uint64_t get< uint64_t >(const annotation_key &x)
Get the uint64_t value or throw conversion_error.
Definition annotation_key.hpp:72
The base class for scalar types.
An error converting between AMQP and C++ data.
Definition error.hpp:56
A 16-byte universally unique identifier.