13#define VRPN_PI (3.14159265358979323846)
14#define VRPN_INCHES_TO_METERS (2.54/100.0)
15#define VRPN_DEGREES_TO_RADIANS (VRPN_PI/180.0)
24#if defined(__ANDROID__)
40#undef VRPN_USE_WINSOCK_SOCKETS
45#if defined(_WIN32) && \
46 (!defined(__CYGWIN__) || defined(VRPN_CYGWIN_USES_WINSOCK_SOCKETS))
47#define VRPN_USE_WINSOCK_SOCKETS
48#define vrpn_SOCKET SOCKET
51#ifndef VRPN_USE_WINSOCK_SOCKETS
53#define INVALID_SOCKET -1
54#define vrpn_SOCKET int
57#if !(defined(_WIN32) && defined(VRPN_USE_WINSOCK_SOCKETS))
58#include <sys/select.h>
59#include <netinet/in.h>
63#define perror(x) fprintf(stderr, "%s\n", x);
74#define VRPN_USE_WINDOWS_GETHOSTBYNAME_HACK
95#if (!defined(VRPN_USE_WINSOCK_SOCKETS))
100#ifndef VRPN_USE_STD_CHRONO
101 #define vrpn_gettimeofday gettimeofday
118#ifndef WIN32_LEAN_AND_MEAN
119#define WIN32_LEAN_AND_MEAN
123#include <sys/timeb.h>
125#ifdef VRPN_USE_WINSOCK2
142#if defined(VRPN_EXPORT_GETTIMEOFDAY)
145#define gettimeofday vrpn_gettimeofday
166 const struct timeval &tv2);
168 const struct timeval &tv2);
174 struct timeval startT);
179 struct timeval startT);
182 const struct timeval &tv2);
184 const struct timeval &tv2);
207static const int vrpn_int_data_for_endian_test = 1;
208static const char *vrpn_char_data_for_endian_test =
209 static_cast<const char*
>(
static_cast<const void *
>((&vrpn_int_data_for_endian_test)));
210static const bool vrpn_big_endian = (vrpn_char_data_for_endian_test[0] != 1);
214 const char *
string, vrpn_int32 length);
272 inline vrpn_uint8
hton(vrpn_uint8 hostval) {
return hostval; }
275 inline vrpn_uint8
ntoh(vrpn_uint8 netval) {
return netval; }
278 inline vrpn_uint16
hton(vrpn_uint16 hostval) {
return htons(hostval); }
281 inline vrpn_uint16
ntoh(vrpn_uint16 netval) {
return ntohs(netval); }
284 inline vrpn_uint32
hton(vrpn_uint32 hostval) {
return htonl(hostval); }
287 inline vrpn_uint32
ntoh(vrpn_uint32 netval) {
return ntohl(netval); }
297 template <
typename T>
inline T
hton(T input)
303 inVal.asInput = input;
304 outVal.asInt =
hton(inVal.asInt);
305 return outVal.asInput;
310 template <
typename T>
inline T
ntoh(T input)
316 inVal.asInput = input;
317 outVal.asInt =
ntoh(inVal.asInt);
318 return outVal.asInput;
340#ifdef VRPN_USE_STATIC_ASSERTIONS
343#if defined(__GXX_EXPERIMENTAL_CXX0X__) || \
344 (defined(_MSC_VER) && (_MSC_VER >= 1600))
345#define VRPN_STATIC_ASSERT(CONDITION, MESSAGE) \
346 static_assert(CONDITION, #MESSAGE)
348#define VRPN_STATIC_ASSERT(CONDITION, MESSAGE) \
349 (void)(::vrpn_detail::vrpn_static_assert<CONDITION>::MESSAGE)
354#define VRPN_STATIC_ASSERT(CONDITION, MESSAGE) assert((CONDITION) && #MESSAGE)
361template <
typename T,
typename ByteT>
362static inline T vrpn_unbuffer_from_little_endian(ByteT *&input)
370 typename ::vrpn_detail::remove_const<ByteT>::type bytes[
sizeof(T)];
375 for (
unsigned int i = 0, j =
sizeof(T) - 1; i <
sizeof(T); ++i, --j) {
376 value.bytes[i] = input[j];
383 return ntoh(value.typed);
398 typename ::vrpn_detail::remove_const<ByteT>::type bytes[
sizeof(T)];
403 memcpy(value.bytes, input,
sizeof(T));
409 return ntoh(value.typed);
416template <
typename T,
typename ByteT>
423 if ((insertPt == NULL) || (buflen == NULL)) {
424 fprintf(stderr,
"vrpn_buffer: NULL pointer\n");
428 if (
sizeof(T) >
static_cast<size_t>(*buflen)) {
429 fprintf(stderr,
"vrpn_buffer: buffer not large enough\n");
435 typename ::vrpn_detail::remove_const<ByteT>::type bytes[
sizeof(T)];
440 value.typed =
hton(inVal);
443 for (
unsigned int i = 0, j =
sizeof(T) - 1; i <
sizeof(T); ++i, --j) {
444 (*insertPt)[i] = value.bytes[j];
448 *insertPt +=
sizeof(T);
450 *buflen -=
sizeof(T);
459template <
typename T,
typename ByteT>
460inline int vrpn_buffer(ByteT **insertPt, vrpn_int32 *buflen,
const T inVal)
466 if ((insertPt == NULL) || (buflen == NULL)) {
467 fprintf(stderr,
"vrpn_buffer: NULL pointer\n");
471 if (
sizeof(T) >
static_cast<size_t>(*buflen)) {
472 fprintf(stderr,
"vrpn_buffer: buffer not large enough\n");
478 typename ::vrpn_detail::remove_const<ByteT>::type bytes[
sizeof(T)];
483 value.typed =
hton(inVal);
486 memcpy(*insertPt, value.bytes,
sizeof(T));
489 *insertPt +=
sizeof(T);
491 *buflen -=
sizeof(T);
496template <
typename T,
typename ByteT>
514 for (i = 0; i < size - 1 && src[i]; i++) {
528template <
size_t charCount>
void vrpn_strcpy(
char (&to)[charCount],
const char* pSrc)
532 strncpy_s(to, pSrc, charCount);
534 strncpy(to, pSrc, charCount - 1);
537 to[charCount - 1] = 0;
560 m_size = m_allocated = s;
563 : m_size(from.
size()), m_data(0), m_allocated(from.
size()) {
564 m_data =
new T[m_size];
565 for (
size_t i = 0; i < m_size; i++) {
566 m_data[i] = from.m_data[i];
569 template<
class InputIt >
570 vrpn_vector( InputIt first, InputIt last ) : m_size(0), m_data(0), m_allocated(0) {
571 for (InputIt it = first; it != last; ++it) {
577 m_size = m_allocated = 0;
582 m_size = m_allocated = from.
size();
583 m_data =
new T[m_size];
584 for (
size_t i = 0; i < m_size; i++) {
585 m_data[i] = from.m_data[i];
591 bool empty()
const {
return m_size == 0; }
595 if (s <= m_allocated) {
600 T* newData =
new T[s];
601 for (
size_t i = 0; i < m_size; i++) {
602 newData[i] = m_data[i];
606 m_size = m_allocated = s;
611 m_data[m_size-1] = val;
617 return m_data[m_size-1];
632 template<
class InputIt >
633 void assign( InputIt first, InputIt last ) {
635 for (InputIt it = first; it != last; it++) {
643 return &m_data[m_size];
650 return &m_data[m_size];
const pointer * const_iterator
vrpn_vector & operator=(const vrpn_vector &from)
const_iterator end() const
void push_back(const T &val)
vrpn_vector(InputIt first, InputIt last)
T & operator[](size_type pos)
void assign(InputIt first, InputIt last)
void assign(size_type count, const T &value)
const T & operator[](size_type pos) const
const_iterator begin() const
vrpn_vector(const vrpn_vector &from)
Internal header providing unbuffering facilities for a number of types.
vrpn_uint8 hton(vrpn_uint8 hostval)
host to network byte order for 8-bit uints is a no-op
vrpn_uint8 ntoh(vrpn_uint8 netval)
network to host byte order for 8-bit uints is a no-op
Traits class to get the uint type of a given size.
@ SIZE_OF_BUFFER_ITEM_IS_NOT_ONE_BYTE
timeval vrpn_TimevalScale(const timeval &tv, double scale)
vrpn_float64 vrpn_ntohd(vrpn_float64 d)
vrpn_float64 vrpn_htond(vrpn_float64 d)
timeval vrpn_TimevalDiff(const timeval &tv1, const timeval &tv2)
timeval vrpn_TimevalNormalize(const timeval &in_tv)
timeval vrpn_MsecsTimeval(const double dMsecs)
timeval vrpn_TimevalSum(const timeval &tv1, const timeval &tv2)
VRPN_API int vrpn_buffer(char **insertPt, vrpn_int32 *buflen, const char *string, vrpn_int32 length)
Utility routine for placing a character string of given length into a buffer that is to be sent as a ...
void vrpn_strcpy(char(&to)[charCount], const char *pSrc)
Null-terminated-string copy function that both guarantees not to overrun the buffer and guarantees th...
VRPN_API unsigned long vrpn_TimevalDuration(struct timeval endT, struct timeval startT)
Return number of microseconds between startT and endT.
VRPN_API vrpn_float64 vrpn_htond(vrpn_float64 d)
bool vrpn_test_pack_unpack(void)
bool vrpn_test_vrpn_vector(void)
#define VRPN_STATIC_ASSERT(CONDITION, MESSAGE)
Fall back to normal asserts.
VRPN_API vrpn_float64 vrpn_ntohd(vrpn_float64 d)
VRPN_API void vrpn_SleepMsecs(double dMilliSecs)
VRPN_API int vrpn_unbuffer(const char **buffer, char *string, vrpn_int32 length)
Utility routine for taking a string of specified length from a buffer that was sent as a message.
VRPN_API double vrpn_TimevalMsecs(const struct timeval &tv1)
#define vrpn_gettimeofday
int vrpn_buffer_to_little_endian(ByteT **insertPt, vrpn_int32 *buflen, const T inVal)
Function template to buffer values to a buffer stored in little- endian order. Specify the type to bu...
VRPN_API double vrpn_TimevalDurationSeconds(struct timeval endT, struct timeval startT)
Return the number of seconds between startT and endT as a floating-point value.
char * vrpn_strncpynull(char *dst, const char *src, size_t size)
Version of strncpy that ensures the resulting string is alyways NULL terminated. It also only writes ...
VRPN_API bool vrpn_TimevalEqual(const struct timeval &tv1, const struct timeval &tv2)
VRPN_API bool vrpn_TimevalGreater(const struct timeval &tv1, const struct timeval &tv2)
Header containing vrpn_Thread, vrpn_Semaphore (formerly in vrpn_Shared.h), as well as a lock-guard cl...