C++20 template concepts in the comments
This commit is contained in:
parent
69cb85ef9f
commit
2fa42b1c53
80
acn/pdu.h
80
acn/pdu.h
|
@ -62,52 +62,65 @@ struct pdu_flags
|
|||
* @brief The pdu_header struct
|
||||
*/
|
||||
struct pdu_header : public pdu_stream_object {};
|
||||
|
||||
// C++20
|
||||
//template <class T>
|
||||
//concept header = std::is_base_of<pdu_header, T>::value;
|
||||
|
||||
/**
|
||||
* @brief The pdu_data struct
|
||||
*/
|
||||
struct pdu_data : public pdu_stream_object {};
|
||||
// C++20
|
||||
//template <class T>
|
||||
//concept data = std::is_base_of<pdu_data, T>::value;
|
||||
|
||||
// C++20
|
||||
//template <class T>
|
||||
//concept pdu = std::is_base_of<Pdu, T>::value;
|
||||
|
||||
/**
|
||||
* @brief PDU::pdu_data subclass that encapsulates other PDU.
|
||||
* @tparam T PDU decendant subclass
|
||||
*/
|
||||
template <typename T>
|
||||
//template<pdu T> // C++20
|
||||
template<class T>
|
||||
struct Block
|
||||
: public pdu_data
|
||||
{
|
||||
std::shared_ptr<std::vector<std::shared_ptr<T>>> pdu
|
||||
= std::shared_ptr<std::vector<std::shared_ptr<T>>>
|
||||
Block() {
|
||||
static_assert(std::is_base_of<Pdu, T>::value,
|
||||
"type parameter of ACN::PDU::Block must derive from ACN::PDU::Pdu");
|
||||
}
|
||||
std::shared_ptr<std::vector<std::shared_ptr<T>>> pdu
|
||||
= std::shared_ptr<std::vector<std::shared_ptr<T>>>
|
||||
(new std::vector<std::shared_ptr<T>>);
|
||||
void setParent(std::shared_ptr<Pdu> parent) {
|
||||
for (auto p : *pdu)
|
||||
p->setParent(parent);
|
||||
}
|
||||
size_t streamSize() const override {
|
||||
size_t s = 0;
|
||||
for (auto &child : *pdu)
|
||||
s += child->streamSize();
|
||||
return s;
|
||||
}
|
||||
void iStream(Stream s) override {
|
||||
while(s->good()) {
|
||||
std::shared_ptr<T> p(new T());
|
||||
p->iStream(s);
|
||||
if (s->fail()) // stream failed during pdu read
|
||||
break;
|
||||
if (p->stream()->fail()) // pdu buffer failed
|
||||
continue;
|
||||
if (!pdu->empty()) // set inheritee
|
||||
p->setInherit(pdu->back());
|
||||
pdu->push_back(p); // add to block
|
||||
}
|
||||
}
|
||||
void oStream(Stream s) const override {
|
||||
for ( const auto & child : *pdu )
|
||||
child->oStream(s);
|
||||
};
|
||||
void setParent(std::shared_ptr<Pdu> parent) {
|
||||
for (auto p : *pdu)
|
||||
p->setParent(parent);
|
||||
}
|
||||
size_t streamSize() const override {
|
||||
size_t s = 0;
|
||||
for (auto &child : *pdu)
|
||||
s += child->streamSize();
|
||||
return s;
|
||||
}
|
||||
void iStream(Stream s) override {
|
||||
while(s->good()) {
|
||||
std::shared_ptr<T> p(new T());
|
||||
p->iStream(s);
|
||||
if (s->fail()) // stream failed during pdu read
|
||||
break;
|
||||
if (p->stream()->fail()) // pdu buffer failed
|
||||
continue;
|
||||
if (!pdu->empty()) // set inheritee
|
||||
p->setInherit(pdu->back());
|
||||
pdu->push_back(p); // add to block
|
||||
}
|
||||
}
|
||||
void oStream(Stream s) const override {
|
||||
for ( const auto & child : *pdu )
|
||||
child->oStream(s);
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
|
@ -149,6 +162,7 @@ public:
|
|||
void setInherit(std::shared_ptr<Pdu> pdu) {inherit_ = pdu;}
|
||||
|
||||
// protocol payloads
|
||||
//template<header T> // C++20
|
||||
template<class T>
|
||||
void createHeader()
|
||||
{
|
||||
|
@ -159,6 +173,8 @@ public:
|
|||
header_->iStream(stream_);
|
||||
}
|
||||
}
|
||||
|
||||
//template<data T> // C++20
|
||||
template<class T>
|
||||
void createData()
|
||||
{
|
||||
|
@ -169,6 +185,7 @@ public:
|
|||
data_->iStream(stream_);
|
||||
}
|
||||
}
|
||||
//template<pdu T> // C++20
|
||||
template<class T>
|
||||
void createDataBlock() {
|
||||
auto block = new PDU::Block<T>();
|
||||
|
@ -194,6 +211,7 @@ protected:
|
|||
* @brief Callback that understands how to proccess a PDU type.
|
||||
* @tparam T PDU decendant subclass
|
||||
*/
|
||||
//template<pdu T> // C++20
|
||||
template <class T>
|
||||
using Handler = std::function<void(std::shared_ptr<T>)>;
|
||||
|
||||
|
|
Loading…
Reference in New Issue