Go to Post You can either spend lots of energy complaining about it and whining or you can try to figure out how to make a positive experience from the whole thing. - dlavery [more]
Home
Go Back   Chief Delphi > Technical > Programming > C/C++
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 05-04-2016, 15:57
Wyspar Wyspar is offline
Registered User
FRC #5132 (RoboClovers)
Team Role: Programmer
 
Join Date: Jan 2015
Rookie Year: 2015
Location: żDe donde eres?
Posts: 20
Wyspar is an unknown quantity at this point
What does SendableChooser.GetSelected() return as?

http://first.wpi.edu/FRC/roborio/rel...leChooser.html

The library says it returns as a void pointer, but what does that information return like? Does it return the name of the option that is put in with this,

autoChooser.AddObject("Lowbar Auto", new Auto);

"Lowbar Auto" is the name of the option, and can I use it in an if statement like,

if (autoChooser.GetSelected() == "Lowbar Auto") {
//Stuff for lowbar auto
}
Reply With Quote
  #2   Spotlight this post!  
Unread 05-04-2016, 16:36
heuristics heuristics is offline
Registered User
FRC #3634
Team Role: Mentor
 
Join Date: Jan 2014
Rookie Year: 2014
Location: Trumbull, CT
Posts: 21
heuristics is on a distinguished road
Re: What does SendableChooser.GetSelected() return as?

I haven't used C++ for robot programming but being familiar with C and C++ APIs, it looks like it will return the object you passed in as the second parameter to AddObject().

This code:
Code:
if (autoChooser.GetSelected() == "Lowbar Auto") {
//Stuff for lowbar auto
}
would not work because it would compare the void* (address) to the address of the "Lowbar Auto" string literal (char*, wchar_t*, etc. depending on compiler). Instead, you should cast the return value of GetSelected() back to the pointer type which you passed in. Then you can dereference it, or do whatever else you need in order to decide what auto mode to run.

Also,
Code:
autoChooser.AddObject("Lowbar Auto", new Auto);
is likely to cause a memory leak since the SendableChooser won't be able to delete the Auto as it will see it as a void*. You will probably need to manage that object yourself.
Reply With Quote
  #3   Spotlight this post!  
Unread 05-04-2016, 16:43
Wyspar Wyspar is offline
Registered User
FRC #5132 (RoboClovers)
Team Role: Programmer
 
Join Date: Jan 2015
Rookie Year: 2015
Location: żDe donde eres?
Posts: 20
Wyspar is an unknown quantity at this point
Re: What does SendableChooser.GetSelected() return as?

I knew already it didn't work, so that's why I was asking. And I found the solution anyways

autoSelected = *((std::string*)chooser->GetSelected());

if(autoSelected == autoNameCustom){
//Custom Auto goes here
} else {
//Default Auto goes here
}
Reply With Quote
  #4   Spotlight this post!  
Unread 05-04-2016, 16:46
heuristics heuristics is offline
Registered User
FRC #3634
Team Role: Mentor
 
Join Date: Jan 2014
Rookie Year: 2014
Location: Trumbull, CT
Posts: 21
heuristics is on a distinguished road
Re: What does SendableChooser.GetSelected() return as?

Out of curiosity, did you also change your second parameter to AddObject to a std::string?
Reply With Quote
  #5   Spotlight this post!  
Unread 06-04-2016, 11:36
euhlmann's Avatar
euhlmann euhlmann is offline
CTO, Programmer
AKA: Erik Uhlmann
FRC #2877 (LigerBots)
Team Role: Leadership
 
Join Date: Dec 2015
Rookie Year: 2015
Location: United States
Posts: 322
euhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud of
Re: What does SendableChooser.GetSelected() return as?

It returns a void*, which you cast to a Whatever* that you passed into AddObject (in your case, Command*)

Code:
autoChooser.AddObject("Lowbar Auto", new Auto);

// Then in autonomous init 
((Command*) autoChooser.GetSelected())->Start();
(A templated SendableChooser might solve this confusion)

It's not a memory leak since you'd never normally delete your auto commands anyway.
Reply With Quote
  #6   Spotlight this post!  
Unread 06-04-2016, 12:47
Wyspar Wyspar is offline
Registered User
FRC #5132 (RoboClovers)
Team Role: Programmer
 
Join Date: Jan 2015
Rookie Year: 2015
Location: żDe donde eres?
Posts: 20
Wyspar is an unknown quantity at this point
Re: What does SendableChooser.GetSelected() return as?

Quote:
Originally Posted by heuristics View Post
Out of curiosity, did you also change your second parameter to AddObject to a std::string?
Yes I did, but I just didn't show it.
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 13:29.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi