#ifndef PROTON_BITS_HPP #define PROTON_BITS_HPP /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ #include #include #include #include #include "contexts.hpp" /**@file * * Assorted internal proton utilities. */ struct pn_error_t; struct pn_data_t; struct pn_transport_t; struct pn_sasl_t; struct pn_ssl_t; struct pn_connection_t; struct pn_session_t; struct pn_link_t; struct pn_delivery_t; struct pn_condition_t; struct pn_acceptor_t; struct pn_terminus_t; struct pn_reactor_t; struct pn_record_t; namespace proton { namespace internal { class data; } class transport; class sasl; class ssl; class connection; class session; class link; class sender; class receiver; class transfer; class tracker; class delivery; class error_condition; class acceptor; class terminus; class source; class target; class reactor; class messaging_handler; std::string error_str(long code); /** Print the error string from pn_error_t, or from code if pn_error_t has no error. */ std::string error_str(pn_error_t*, long code=0); /** Make a void* inspectable via operator <<. */ struct inspectable { void* value; inspectable(void* o) : value(o) {} }; /** Stream a proton object via pn_inspect. */ std::ostream& operator<<(std::ostream& o, const inspectable& object); void set_error_condition(const error_condition&, pn_condition_t*); /// Convert a const char* to std::string, convert NULL to the empty string. inline std::string str(const char* s) { return s ? s : std::string(); } namespace internal { // These traits relate the wrapped and wrapper classes for the templated factories below template struct wrapped {}; template <> struct wrapped { typedef pn_data_t type; }; template <> struct wrapped { typedef pn_transport_t type; }; template <> struct wrapped { typedef pn_connection_t type; }; template <> struct wrapped { typedef pn_session_t type; }; template <> struct wrapped { typedef pn_link_t type; }; template <> struct wrapped { typedef pn_link_t type; }; template <> struct wrapped { typedef pn_link_t type; }; template <> struct wrapped { typedef pn_delivery_t type; }; template <> struct wrapped { typedef pn_delivery_t type; }; template <> struct wrapped { typedef pn_delivery_t type; }; template <> struct wrapped { typedef pn_condition_t type; }; template <> struct wrapped { typedef pn_terminus_t type; }; template <> struct wrapped { typedef pn_terminus_t type; }; template <> struct wrapped { typedef pn_terminus_t type; }; template struct wrapper {}; template <> struct wrapper { typedef internal::data type; }; template <> struct wrapper { typedef transport type; }; template <> struct wrapper { typedef connection type; }; template <> struct wrapper { typedef session type; }; template <> struct wrapper { typedef link type; }; template <> struct wrapper { typedef transfer type; }; template <> struct wrapper { typedef error_condition type; }; template <> struct wrapper { typedef terminus type; }; // Factory for wrapper types template class factory { public: static T wrap(typename wrapped::type* t) { return t; } static typename wrapped::type* unwrap(const T& t) { return t.pn_object(); } }; template struct context {}; template <> struct context {typedef link_context type; }; template <> struct context {typedef link_context type; }; template <> struct context {typedef link_context type; }; template <> struct context {typedef session_context type; }; template <> struct context {typedef connection_context type; }; template inline void set_messaging_handler(T t, messaging_handler* mh) { context::type::get(factory::unwrap(t)).handler = mh; } template inline messaging_handler* get_messaging_handler(T* t) { return context::type>::type::get(t).handler; } class returned_factory { public: template static returned make(typename internal::wrapped::type* pn) { return returned(pn); } }; } // namespace internal template typename internal::wrapper::type make_wrapper(T* t) { return internal::factory::type>::wrap(t); } template U make_wrapper(typename internal::wrapped::type* t) { return internal::factory::wrap(t); } template typename internal::wrapped::type* unwrap(const T& t) { return internal::factory::unwrap(t); } template returned make_returned(typename internal::wrapped::type* pn) { return internal::returned_factory::make(pn); } // Get an AMQP "multiple" field from a value. A "multiple" field can be encoded as a single // value or as an array. This function always extracts it as a sequence, a sequence of one // if it is encoded as a single value. // // T should be a valid sequence type for proton::get() with a T::value_type typedef. // template void get_multiple(const value& v, T& x) { if (v.empty()) { x.clear(); } else if (v.type() == ARRAY) { proton::get(v,x); } else { x.resize(1); proton::get(v,x[0]); } } // Same as previous but returns the value. template T get_multiple(const value& v) { T x; get_multiple(v, x); return x; } } // namespace proton #endif // PROTON_BITS_HPP