Browse Source

* implemented generic argument table

* added generic arguments to 'declare_queue' method
master
bergmann 5 years ago
parent
commit
dc48454946
5 changed files with 147 additions and 4 deletions
  1. +2
    -1
      include/cppamqp.h
  2. +1
    -1
      include/cppamqp/channel.h
  3. +24
    -0
      include/cppamqp/types.h
  4. +105
    -0
      include/cppamqp/types.inl
  5. +15
    -2
      src/cppamqp/channel.cpp

+ 2
- 1
include/cppamqp.h View File

@@ -16,4 +16,5 @@
#include <cppamqp/exception.inl>
#include <cppamqp/helper.inl>
#include <cppamqp/message.inl>
#include <cppamqp/publish_options.inl>
#include <cppamqp/publish_options.inl>
#include <cppamqp/types.inl>

+ 1
- 1
include/cppamqp/channel.h View File

@@ -42,7 +42,7 @@ namespace cppamqp
inline channel_number handle () const;
inline const connection_t& connection () const;

queue_declaration declare_queue (const std::string& name, const queue_flags& flags);
queue_declaration declare_queue (const std::string& name, const queue_flags& flags, const table& args = empty_table);
void bind_queue (const std::string& queue, const std::string& exchange, const std::string& routing_key);
void publish (const std::string& exchange, const std::string& routing_key, const publish_flags& flags, const std::string& message, const publish_options* options = nullptr);
std::string consume (const std::string& queue, const std::string& consumer_tag, const consume_flags& flags);


+ 24
- 0
include/cppamqp/types.h View File

@@ -1,5 +1,6 @@
#pragma once

#include <map>
#include <cppamqp/config.h>

namespace cppamqp
@@ -14,4 +15,27 @@ namespace cppamqp

using channel_number = amqp_channel_t;

struct field_value
: private amqp_field_value_t
{
private:
template<typename T_value>
inline void set(T_value&& value);

public:
inline field_value();

template<typename T_value>
inline field_value(T_value&& value);

template<typename T_value>
inline field_value& operator=(T_value&& value);

inline const amqp_field_value_t& get() const;
};

using table = std::map<std::string, field_value>;

const table empty_table;

}

+ 105
- 0
include/cppamqp/types.inl View File

@@ -0,0 +1,105 @@
#pragma once

#include <cppamqp/types.h>

namespace cppamqp
{

template<>
inline void field_value::set<bool>(bool&& value)
{
this->kind = AMQP_FIELD_KIND_BOOLEAN;
this->value.boolean = static_cast<int>(value);
}

template<>
inline void field_value::set<int8_t>(int8_t&& value)
{
this->kind = AMQP_FIELD_KIND_I8;
this->value.i8 = value;
}

template<>
inline void field_value::set<uint8_t>(uint8_t&& value)
{
this->kind = AMQP_FIELD_KIND_U8;
this->value.u8 = value;
}

template<>
inline void field_value::set<int16_t>(int16_t&& value)
{
this->kind = AMQP_FIELD_KIND_I16;
this->value.i16 = value;
}

template<>
inline void field_value::set<uint16_t>(uint16_t&& value)
{
this->kind = AMQP_FIELD_KIND_U16;
this->value.u16 = value;
}

template<>
inline void field_value::set<int32_t>(int32_t&& value)
{
this->kind = AMQP_FIELD_KIND_I32;
this->value.i32 = value;
}

template<>
inline void field_value::set<uint32_t>(uint32_t&& value)
{
this->kind = AMQP_FIELD_KIND_U32;
this->value.u32 = value;
}

template<>
inline void field_value::set<int64_t>(int64_t&& value)
{
this->kind = AMQP_FIELD_KIND_I64;
this->value.i64 = value;
}

template<>
inline void field_value::set<uint64_t>(uint64_t&& value)
{
this->kind = AMQP_FIELD_KIND_U64;
this->value.u64 = value;
}

template<>
inline void field_value::set<float>(float&& value)
{
this->kind = AMQP_FIELD_KIND_F32;
this->value.f32 = value;
}

template<>
inline void field_value::set<double>(double&& value)
{
this->kind = AMQP_FIELD_KIND_F64;
this->value.f64 = value;
}


field_value::field_value()
{
this->kind = AMQP_FIELD_KIND_VOID;
}

template<typename T_value>
field_value::field_value(T_value&& value)
{ set(std::forward<T_value>(value)); }

template<typename T_value>
field_value& field_value::operator=(T_value&& value)
{
set(std::forward<T_value>(value));
return *this;
}

inline const amqp_field_value_t& field_value::get() const
{ return *this; }

}

+ 15
- 2
src/cppamqp/channel.cpp View File

@@ -1,3 +1,4 @@
#include <cppamqp/types.inl>
#include <cppamqp/helper.inl>
#include <cppamqp/channel.inl>
#include <cppamqp/exception.inl>
@@ -28,8 +29,20 @@ channel::internal::~internal()
true);
}

queue_declaration channel::declare_queue(const std::string& name, const queue_flags& flags)
queue_declaration channel::declare_queue(const std::string& name, const queue_flags& flags, const table& args)
{
size_t i = 0;
amqp_table_t arg_table;
amqp_table_entry_t entries[args.size()];
arg_table.num_entries = static_cast<int>(args.size());
arg_table.entries = &entries[0];
for (auto& kvp : args)
{
arg_table.entries[i].key = __impl::make_bytes(kvp.first);
arg_table.entries[i].value = kvp.second.get();
++i;
}

auto ret = amqp_queue_declare(
connection().handle(),
handle(),
@@ -38,7 +51,7 @@ queue_declaration channel::declare_queue(const std::string& name, const queue_fl
flags.is_set(queue_flag::durable) ? 1 : 0,
flags.is_set(queue_flag::exclusive) ? 1 : 0,
flags.is_set(queue_flag::auto_delete) ? 1 : 0,
amqp_empty_table);
arg_table);
__impl::check_and_raise(
amqp_get_rpc_reply(connection().handle()),
std::string("unable to declare queue '") + name + "'",


Loading…
Cancel
Save