| 1 |
#ifndef TYPES_INTERNAL_HPP |
| 2 |
#define TYPES_INTERNAL_HPP |
| 3 |
/* |
| 4 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 5 |
* or more contributor license agreements. See the NOTICE file |
| 6 |
* distributed with this work for additional information |
| 7 |
* regarding copyright ownership. The ASF licenses this file |
| 8 |
* to you under the Apache License, Version 2.0 (the |
| 9 |
* "License"); you may not use this file except in compliance |
| 10 |
* with the License. You may obtain a copy of the License at |
| 11 |
* |
| 12 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 13 |
* |
| 14 |
* Unless required by applicable law or agreed to in writing, |
| 15 |
* software distributed under the License is distributed on an |
| 16 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 17 |
* KIND, either express or implied. See the License for the |
| 18 |
* specific language governing permissions and limitations |
| 19 |
* under the License. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "proton/internal/type_traits.hpp" |
| 23 |
#include "proton/error.hpp" |
| 24 |
#include "proton/binary.hpp" |
| 25 |
#include <sstream> |
| 26 |
|
| 27 |
///@file |
| 28 |
/// Inline helpers for encode/decode/type conversion/ostream operators. |
| 29 |
|
| 30 |
namespace proton { |
| 31 |
|
| 32 |
/// Byte copy between two objects, only enabled if their sizes are equal. |
| 33 |
template <class T, class U> |
| 34 |
typename std::enable_if<sizeof(T) == sizeof(U)>::type byte_copy(T &to, const U &from) { |
| 35 |
const char *p = reinterpret_cast<const char*>(&from); |
| 36 |
std::copy(p, p + sizeof(T), reinterpret_cast<char*>(&to)); |
| 37 |
} |
| 38 |
|
| 39 |
inline conversion_error |
| 40 |
make_conversion_error(type_id want, type_id got, const std::string& msg=std::string()) { |
| 41 |
std::ostringstream s; |
| 42 |
s << "unexpected type, want: " << want << " got: " << got; |
| 43 |
if (!msg.empty()) s << ": " << msg; |
| 44 |
return conversion_error(s.str()); |
| 45 |
} |
| 46 |
|
| 47 |
/// Convert std::string to pn_bytes_t |
| 48 |
inline pn_bytes_t pn_bytes(const std::string& s) { |
| 49 |
pn_bytes_t b = { s.size(), s.empty() ? 0 : const_cast<char*>(&s[0]) }; |
| 50 |
return b; |
| 51 |
} |
| 52 |
|
| 53 |
inline pn_bytes_t pn_bytes(const binary& s) { |
| 54 |
pn_bytes_t b = { s.size(), s.empty() ? 0 : reinterpret_cast<const char*>(&s[0]) }; |
| 55 |
return b; |
| 56 |
} |
| 57 |
|
| 58 |
inline std::string str(const pn_bytes_t& b) { return std::string(b.start, b.size); } |
| 59 |
inline binary bin(const pn_bytes_t& b) { return binary(b.start, b.start+b.size); } |
| 60 |
|
| 61 |
// Save all stream format state, restore in destructor. |
| 62 |
struct ios_guard { |
| 63 |
std::ios &guarded; |
| 64 |
std::ios old; |
| 65 |
ios_guard(std::ios& x) : guarded(x), old(0) { old.copyfmt(guarded); } |
| 66 |
~ios_guard() { guarded.copyfmt(old); } |
| 67 |
}; |
| 68 |
|
| 69 |
// Convert a char (signed or unsigned) into an unsigned 1 byte integer that will ostream |
| 70 |
// as a numeric byte value, not a character and will not get sign-extended. |
| 71 |
inline unsigned int printable_byte(uint8_t byte) { return byte; } |
| 72 |
|
| 73 |
} |
| 74 |
#endif // TYPES_INTERNAL_HPP |