Checking if AmfItemPtr isn't Null
adammankowski1 opened this issue · 3 comments
Hello, i'm writing to ask if construction:
if(amf::AmfItemPtr != amf::AmfNull())
Is right? I have to verify if AmfItemPtr is not null before doing something with it.
No, amf::AmfNull()
is an instance of the AMF null type. To check if an amf::AmfItemPtr
is null, use something like if (item_ptr.get() != nullptr)
.
However, you shouldn't really be using AmfItemPtr
other than handling the return value of calls to Deserializer::deserialize
, which guarantees to always return a non-null AmfItemPtr
. What exactly is the use-case you have where a null-check for the itemptr is required?
I'm working with Qt, i see that i'm not working with your latest version of amf-cpp. First I'm working on received amf packet (read version, header count, message count etc), then i'm looking for Content (there is always another "body" array which i have to check if is null or not).
vstd::map<std::string, amf::AmfItemPtr> *data
amf::AmfItemPtr body_ptr = data->find("body")->second;
amf::AmfArray body_array;
if(body_ptr != amf::AmfNull())
body_array = body_ptr.as<amf::AmfArray>();
else
throw 0;
So if I understand you correctly, you're manually deserializing a AmfPacket
? If that's the case, I've added deserialization support for it to amf-cpp yesterday, maybe see if you can use that instead. Note that since AmfPacket
is not an AMF object type (as e.g. AmfNull
and AmfArray
are), you can't use Deserializer::deserialize
to deserialize it.
Instead, you'll have to use something like the following:
// assuming this contains your data, v8 is a typedef for std::vector<unsigned char>
// the first to bytes in this vector should be the packet version, i.e. 0x00, 0x03
amf::v8 data;
auto it = data.begin();
amf::DeserializationContext ctx;
amf::AmfPacket deserialized = amf::AmfPacket::deserialize(it, data.end(), ctx);
Afterwards, the range [it, data.end())
contains any remaining unparsed data (i.e., it
points to the first unparsed byte after the AmfPacket
). deserialized
is the deserialized AmfPacket
, which allows you to access the corresponding headers and messages through the .headers
and .messages
members (vectors of amf::AmfHeader
and amf::AmfMessage
, respectively). See src/amfpacket.hpp
for more details.
But to answer the original question: in general, you would have to use if (body_ptr.get() != nullptr)
to check if the body_ptr
is null or not.