Help to remove reinterpret_cast from c++ code
Hi, I have been reading a class that I found online that creates an
object to represent a joystick. Now i recall reading that any use of
'reinterpret cast' is dangerous as it is highly machine dependent. The
code I found unfortunately uses this and i was hoping to improve it,
before attempting to use the class in my program. Note, I have not
tested the class as yet - even on the assumption it works on my machine,
i need it to be as general as possible as my program will run on other
machines as well.
This is the offending member function definition:
//loop function attempts to read an event from the device, and, if an
//event is waiting to be processed,
//updates the state structure based on the event type.
void* cJoystick::loop(void *obj)
{
while (reinterpret_cast<cJoystick *>(obj)->active)
reinterpret_cast<cJoystick *>(obj)->readEv();
}
- so how can this be re-written so there are no re-interpret casts?
This is the complete class & member function definitions -if you need to
test it.
----------------------- CODE--------------------------
#ifndef JOYSTICK_H
#define JOYSTICK_H
#include <unistd.h>
class cJoystick {
public:
cJoystick();
~cJoystick();
static void* loop(void* obj);
void readEv();
joystick_position joystickPosition(int n);
bool buttonPressed(int n);
private:
pthread_t thread;
bool active;
int joystick_fd;
js_event *joystick_ev;
joystick_state *joystick_st;
__u32 version;
__u8 axes;
__u8 buttons;
char name[256];
};
/*constructor -
When a joystick is connected to a USB port, udev, the device manager for
the Linux kernel, presents
device nodes at "/dev/input/js*". constructor attempts to
1. access a device node
2. query the device for the number of buttons and axes
3. reserve space in our state structure for the buttons and axes
4. finally, create a thread to populate the state structure. */
cJoystick::cJoystick() {
active = false;
joystick_fd = 0;
joystick_ev = new js_event();
joystick_st = new joystick_state();
joystick_fd = open(JOYSTICK_DEV, O_RDONLY | O_NONBLOCK);
if (joystick_fd > 0) {
ioctl(joystick_fd, JSIOCGNAME(256), name);
ioctl(joystick_fd, JSIOCGVERSION, &version);
ioctl(joystick_fd, JSIOCGAXES, &axes);
ioctl(joystick_fd, JSIOCGBUTTONS, &buttons);
std::cout << " Name: " << name << std::endl;
std::cout << "Version: " << version << std::endl;
std::cout << " Axes: " << (int)axes << std::endl;
std::cout << "Buttons: " << (int)buttons << std::endl;
joystick_st->axis.reserve(axes);
joystick_st->button.reserve(buttons);
active = true;
pthread_create(&thread, 0, &cJoystick::loop, this);
}
}
void cJoystick::readEv() {
int bytes = read(joystick_fd, joystick_ev, sizeof(*joystick_ev));
if (bytes > 0) {
joystick_ev->type &= ~JS_EVENT_INIT;
if (joystick_ev->type & JS_EVENT_BUTTON) {
joystick_st->button[joystick_ev->number] = joystick_ev->value;
}
if (joystick_ev->type & JS_EVENT_AXIS) {
joystick_st->axis[joystick_ev->number] = joystick_ev->value;
}
}
}
// function to query the state of a button.
bool cJoystick::buttonPressed(int n) {
return n > -1 && n < buttons ? joystick_st->button[n] : 0;
}
// function to to query the state of an analog stick
joystick_position cJoystick::joystickPosition(int n) {
joystick_position pos;
if (n > -1 && n < buttons) {
int i0 = n*2, i1 = n*2+1;
float x0 = joystick_st->axis[i0]/32767.0f, y0 =
-joystick_st->axis[i1]/32767.0f;
float x = x0 * sqrt(1 - pow(y0, 2)/2.0f), y = y0 * sqrt(1 -
pow(x0, 2)/2.0f);
pos.x = x0;
pos.y = y0;
pos.theta = atan2(y, x);
pos.r = sqrt(pow(y, 2) + pow(x, 2));
} else {
pos.theta = pos.r = pos.x = pos.y = 0.0f;
}
return pos;
}
// destructor.
cJoystick::~cJoystick() {
if (joystick_fd > 0) {
active = false;
pthread_join(thread, 0);
close(joystick_fd);
}
delete joystick_st;
delete joystick_ev;
joystick_fd = 0;
}
#endif
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]