Jump to content

Question regarding extension detection


Recommended Posts

Hello,

This may have been asked earlier but I cant find it so I am asking again. If it is already asked, please let me know the link

I have two possible extensions A and B (for simplicity only). Both are derived from tlm_extension

I have a module name MOD (for simplicity again).  In this I am checking if extension A is attached or not.

I am not checking the extension B in MOD. It is just for tracking purposes. 

Extension A may or may not be present.

Therefore, i am making check like this

A *extA;

req->get_extension(ext); // tlm::tlm_generic_payload *req is the declaration

if (extA == nullptr) {

do ActionX

} else {

do ActionY

}

Now what is happening is

while sending a request to MOD, I attach extension B. There is not extension A attached. 

Therefore, I think that the check

if (extA == nullptr)

should return true. However, this is not the case. gdb is showing extA is actually B when doing p *extA.

I think this is because  both A and B are derived from tlm_extension.

Is there anyway, I can do a correct check so that if extension A is not present, it should extA as nullptr especially when A is not present but B is present.?

Regards

Moreshwar

Link to comment
Share on other sites

  • 2 weeks later...

The problem is that the pointer is not initialized. So :

A *extA{nullptr};
req->get_extension(ext); // tlm::tlm_generic_payload *req is the declaration
if (extA == nullptr) {
    do ActionX
} else {
    do ActionY
} 

should do the trick. BTW , a better soultion would be:

auto* extA = req->get_extension<A>();
if (extA == nullptr) {
    do ActionX
} else {
    do ActionY
} 

or even more concise:

if (auto* extA = req->get_extension<A>()) {
    do ActionY
} else {
    do ActionX
} 

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...