libcaf  0.13.2
Classes | Public Types | Public Member Functions | Static Public Member Functions | Related Functions | List of all members
caf::message Class Reference

Describes a fixed-length, copy-on-write, type-erased tuple with elements of any type. More...

#include <message.hpp>

Classes

struct  cli_arg
 Stores the name of a command line option ("<long name>[,<short name>]") along with a description and a callback. More...
 
struct  cli_res
 Stores the result of message::extract_opts. More...
 

Public Types

using help_factory = std::function< std::string(const std::vector< cli_arg > &)>
 

Public Member Functions

 message ()=default
 Creates an empty message.
 
 message (message &&)
 Move constructor.
 
 message (const message &)=default
 Copy constructor.
 
messageoperator= (message &&)
 Move assignment.
 
messageoperator= (const message &)=default
 Copy assignment.
 
size_t size () const
 Returns the size of this message.
 
message drop (size_t n) const
 Creates a new message with all but the first n values.
 
message drop_right (size_t n) const
 Creates a new message with all but the last n values.
 
message take (size_t n) const
 Creates a new message from the first n values.
 
message take_right (size_t n) const
 Creates a new message from the last n values.
 
message slice (size_t p, size_t n) const
 Creates a new message of size n starting at the element at position p.
 
void * mutable_at (size_t p)
 Returns a mutable pointer to the element at position p.
 
const void * at (size_t p) const
 Returns a const pointer to the element at position p.
 
const char * uniform_name_at (size_t p) const
 Returns the uniform type name for the element at position p.
 
bool equals (const message &other) const
 Returns true if `*this == other, otherwise false.
 
bool empty () const
 Returns true if `size() == 0, otherwise false.
 
template<class T >
const T & get_as (size_t p) const
 Returns the value at position p as const reference of type T.
 
template<class T >
T & get_as_mutable (size_t p)
 Returns the value at position p as mutable reference of type T.
 
optional< messageapply (message_handler handler)
 Returns handler(*this).
 
message extract (message_handler handler) const
 Filters this message by applying slices of it to handler and returns the remaining elements of this operation. More...
 
cli_res extract_opts (std::vector< cli_arg > xs, help_factory f=nullptr) const
 A simplistic interface for using extract to parse command line options. More...
 
template<class T >
bool match_element (size_t p) const
 Queries whether the element at position p is of type T.
 
template<class... Ts>
bool match_elements () const
 Queries whether the types of this message are Ts....
 

Static Public Member Functions

template<class... Ts>
static message concat (const Ts &...xs)
 Concatinate messages.
 

Related Functions

(Note that these are not member functions.)

bool operator== (const message &lhs, const message &rhs)
 
bool operator!= (const message &lhs, const message &rhs)
 
message operator+ (const message &lhs, const message &rhs)
 
template<class V , class... Ts>
std::enable_if< !std::is_same< message, typename std::decay< V >::type >::value||(sizeof...(Ts) >) message::type make_message (V &&x, Ts &&...xs)
 Returns a new message containing the values (x, xs...).
 
message make_message (message other)
 Returns a copy of other.
 
std::string to_string (const message &what)
 

Detailed Description

Describes a fixed-length, copy-on-write, type-erased tuple with elements of any type.

Member Function Documentation

message caf::message::extract ( message_handler  handler) const

Filters this message by applying slices of it to handler and returns the remaining elements of this operation.

Slices are generated in the sequence [0, size), [0, size-1), ... , [1, size-1), ..., [size-1, size). Whenever a slice matches, it is removed from the message and the next slice starts at the same index on the reduced message.

For example:

auto msg = make_message(1, 2.f, 3.f, 4);
// extract float and integer pairs
auto msg2 = msg.extract({
[](float, float) { },
[](int, int) { }
});
assert(msg2 == make_message(1, 4));

Step-by-step explanation:

  • Slice 1: (1, 2.f, 3.f, 4), no match
  • Slice 2: (1, 2.f, 3.f), no match
  • Slice 3: (1, 2.f), no match
  • Slice 4: (1), no match
  • Slice 5: (2.f, 3.f, 4), no match
  • Slice 6: (2.f, 3.f), match; new message is (1, 4)
  • Slice 7: (4), no match

Slice 7 is (4), i.e., does not contain the first element, because the match on slice 6 occurred at index position 1. The function extract iterates a message only once, from left to right.

cli_res caf::message::extract_opts ( std::vector< cli_arg xs,
help_factory  f = nullptr 
) const

A simplistic interface for using extract to parse command line options.

Usage example:

int main(int argc, char** argv) {
uint16_t port;
string host = "localhost";
auto res = message_builder(argv + 1, argv + argc).extract_opts({
{"port,p", "set port", port},
{"host,H", "set host (default: localhost)", host},
{"verbose,v", "enable verbose mode"}
});
if (!res.error.empty()) {
cerr << res.error << endl;
return 1;
}
if (res.opts.count("help") > 0) {
// CLI arguments contained "-h", "--help", or "-?" (builtin);
cout << res.helptext << endl;
return 0;
}
if (!res.remainder.empty()) {
// ... extract did not consume all CLI arguments ...
}
if (res.opts.count("verbose") > 0) {
// ... enable verbose mode ...
}
// ...
}
Parameters
xsList of argument descriptors.
fOptional factory function to generate help text (overrides the default generator).
Returns
A struct containing remainder (i.e. unmatched elements), a set containing the names of all used arguments, and the generated help text.
Exceptions
std::invalid_argumentif no name or more than one long name is set

Friends And Related Function Documentation

bool operator!= ( const message lhs,
const message rhs 
)
related
message operator+ ( const message lhs,
const message rhs 
)
related
bool operator== ( const message lhs,
const message rhs 
)
related
std::string to_string ( const message what)
related

The documentation for this class was generated from the following files: