use std::optional to control access to Packet data
This commit is contained in:
parent
a26f6f0843
commit
6b82f0eda4
|
@ -27,6 +27,7 @@
|
|||
#include "config.h"
|
||||
#include "bufferstream.h"
|
||||
#include "../../esta/rdm/uid.h"
|
||||
#include <optional>
|
||||
#include <vector>
|
||||
|
||||
namespace ARTNET {
|
||||
|
@ -553,27 +554,12 @@ struct Packet
|
|||
* @return
|
||||
*/
|
||||
template<class T>
|
||||
std::shared_ptr<T> data()
|
||||
auto data() const
|
||||
{
|
||||
#ifdef RTTI_ENABLED
|
||||
return std::dynamic_pointer_cast<T>(_data);
|
||||
#else
|
||||
return std::static_pointer_cast<T>(_data);
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief createData
|
||||
* @param stream
|
||||
*/
|
||||
template<class T>
|
||||
void createData(std::shared_ptr<bufferstream> stream = nullptr)
|
||||
{
|
||||
if (_data)
|
||||
return; // already has a data segment
|
||||
_data = std::make_shared<T>();
|
||||
if (stream && stream->good())
|
||||
_data->iStream(stream);
|
||||
static_assert(std::is_base_of<packet_data, T>::value,
|
||||
"type parameter of data must derive from ARTNET::packet_data");
|
||||
return _data ? std::optional<std::shared_ptr<T>>{std::static_pointer_cast<T>(_data)}
|
||||
: std::nullopt;
|
||||
}
|
||||
|
||||
size_t streamSize() const override;
|
||||
|
|
Loading…
Reference in New Issue