Skip to content

Source · fairmq/

Parts.hpublic header

Defines struct fair::mq::Parts.

Described in Messages and multipart, multipart: several parts in one message, n-m: n senders to m receivers, Send your own data types, Overview.

fairmq/Parts.h97 lines · C++API: PartsGitHub ↗
1/********************************************************************************2 * Copyright (C) 2014-2023 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH  *3 *                                                                              *4 *              This software is distributed under the terms of the             *5 *              GNU Lesser General Public Licence (LGPL) version 3,             *6 *                  copied verbatim in the file "LICENSE"                       *7 ********************************************************************************/8 9#ifndef FAIR_MQ_PARTS_H10#define FAIR_MQ_PARTS_H11 12#include <algorithm>          // std::move13#include <fairmq/Message.h>   // fair::mq::MessagePtr14#include <iterator>           // std::back_inserter15#include <utility>            // std::move, std::forward16#include <vector>             // std::vector17 18namespace fair::mq {19 20/// fair::mq::Parts is a lightweight move-only convenience wrapper around a vector of unique pointers to21/// Message, used for sending multi-part messages22struct Parts23{24    using container = std::vector<MessagePtr>;25    using size_type = container::size_type;26    using reference = container::reference;27    using const_reference = container::const_reference;28    using iterator = container::iterator;29    using const_iterator = container::const_iterator;30 31    Parts() noexcept(noexcept(container())) = default;32    Parts(const Parts&) = delete;33    Parts& operator=(const Parts&) = delete;34    Parts(Parts&&) = default;35    Parts& operator=(Parts&&) = default;36    ~Parts() = default;37 38    template<typename... Ps>39    Parts(Ps&&... parts)40    {41        fParts.reserve(sizeof...(Ps));42        AddPart(std::forward<Ps>(parts)...);43    }44 45    [[deprecated("Avoid owning raw pointer args, use AddPart(MessagePtr) instead.")]]46    void AddPart(Message* msg) { fParts.push_back(MessagePtr(msg)); }47    void AddPart(MessagePtr msg) { fParts.push_back(std::move(msg)); }48 49    template<typename... Ts>50    void AddPart(MessagePtr first, Ts&&... remaining)51    {52        AddPart(std::move(first));53        AddPart(std::forward<Ts>(remaining)...);54    }55 56    void AddPart(Parts parts)57    {58        if (fParts.empty()) {59            fParts = std::move(parts.fParts);60        } else {61            fParts.reserve(parts.Size() + fParts.size());62            std::move(std::begin(parts), std::end(parts), std::back_inserter(fParts));63        }64    }65 66    Message& operator[](size_type index) { return *(fParts[index]); }67    Message const& operator[](size_type index) const { return *(fParts[index]); }68    // TODO: For consistency with the STL interfaces, operator[] should not dereference,69    //       but I have no good idea how to fix this.70    // reference operator[](size_type index) { return fParts[index]; }71    // const_reference operator[](size_type index) const { return fParts[index]; }72 73    [[deprecated("Redundant, dereference at call site e.g. '*(parts.At(index))' instead.")]]74    Message& AtRef(size_type index) { return *(fParts.at(index)); }75    reference At(size_type index) { return fParts.at(index); }76    const_reference At(size_type index) const { return fParts.at(index); }77 78    size_type Size() const noexcept { return fParts.size(); }79    bool Empty() const noexcept { return fParts.empty(); }80    void Clear() noexcept { fParts.clear(); }81 82    // range access83    iterator begin() noexcept { return fParts.begin(); }84    const_iterator begin() const noexcept { return fParts.begin(); }85    const_iterator cbegin() const noexcept { return fParts.cbegin(); }86    iterator end() noexcept { return fParts.end(); }87    const_iterator end() const noexcept { return fParts.end(); }88    const_iterator cend() const noexcept { return fParts.cend(); }89 90    container fParts;91};92 93}   // namespace fair::mq94 95using FairMQParts [[deprecated("Use fair::mq::Parts")]] = fair::mq::Parts;96 97#endif /* FAIR_MQ_PARTS_H */