View Single Post
  #1   Spotlight this post!  
Unread 29-01-2009, 21:44
Phazonmutant's Avatar
Phazonmutant Phazonmutant is offline
Winrar
AKA: Greg Mitchell
FRC #2556 (RadioActive Roaches)
Team Role: Programmer
 
Join Date: Jan 2009
Rookie Year: 2008
Location: Niceville, FL
Posts: 17
Phazonmutant is on a distinguished road
Understanding the Source code

I'm a pretty proficient programmer (most of my experience is with C#), but I'm still finding myself stumped by some of the source code. I'm hoping that if I understand the source I can implement new classes as necessary and use proper conventions (especially where error handling is concerned).

First question: Macros.
FRC apparently (and inexplicably) has made extensive use of macros. I'm not extremely familiar with them, but they seem to serve many purposes.
The first one I came across: "START_ROBOT_CLASS(IterativeDemo);" at the bottom of the demo class. This references RobotBase.h, which has:
Code:
#define START_ROBOT_CLASS(_ClassName_) \
RobotBase *FRC_userClassFactory(void) \
{ \
	return new _ClassName_(); \
} \
extern "C" { \
	int FRC_UserProgram_StartupLibraryInit(void) \
	{ \
		RobotBase::startRobotTask((FUNCPTR)FRC_userClassFactory); \
		return 0; \
	} \
}
This makes no sense to me. Where is the main() function that would presumably instantiate the robot class? Also, what is this START_ROBOT_CLASS doing exactly?

Second question: Macros. Heh, sorry, I mean Macros that do error handling.
The next major macro confusion is with the "wpi_assert"s and "wpi_fatal"s that are sprinkled across the code. One example in RobotDrive.cpp:
Code:
void RobotDrive::SetInvertedMotor(MotorType motor, bool isInverted)
{
	if (motor < 0 || motor > 3)
	{
		wpi_fatal(InvalidMotorIndex);
		return;
	}
	m_invertedMotors[motor] = isInverted ? -1 : 1;
}
I understand that it's some sort of error code, but how exactly does it work? From the FRC C++ guide, the WPI library doesn't use C++'s exception handling system (for the rather odd reason that an uncaught exception might be thrown - which might happen anyway if there's no push to use exception handling). The only thing that has any macro definitions seems to be the WPIStatus.h file. Relevant section:
Code:
#ifdef WPI_STATUS_DEFINE_STRINGS
#define S(label, offset, message) const int wpi_v_##label = offset; const char *wpi_s_##label = message ;
#else
#define S(label, offset, message) const int wpi_v_##label = offset; extern const char *wpi_s_##label;
#endif
What does that do?? I confess, I'm not very well versed in preprocessor directives, but that doesn't even make a bit of sense to me.

Sorry if this was a lot of questions... Basically, I want to know:
-What happened to main() and what is the START_ROBOT_CLASS macro doing
-How the wpi_assert and _demands handle errors
Reply With Quote