nidas v1.2.3
EndianConverter.h
Go to the documentation of this file.
1// -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 4; -*-
2// vim: set shiftwidth=4 softtabstop=4 expandtab:
3/*
4 ********************************************************************
5 ** NIDAS: NCAR In-situ Data Acquistion Software
6 **
7 ** 2006, Copyright University Corporation for Atmospheric Research
8 **
9 ** This program is free software; you can redistribute it and/or modify
10 ** it under the terms of the GNU General Public License as published by
11 ** the Free Software Foundation; either version 2 of the License, or
12 ** (at your option) any later version.
13 **
14 ** This program is distributed in the hope that it will be useful,
15 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ** GNU General Public License for more details.
18 **
19 ** The LICENSE.txt file accompanying this software contains
20 ** a copy of the GNU General Public License. If it is not found,
21 ** write to the Free Software Foundation, Inc.,
22 ** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 **
24 ********************************************************************
25*/
26
27#ifndef NIDAS_UTIL_ENDIANCONVERTER_H
28#define NIDAS_UTIL_ENDIANCONVERTER_H
29
30#include <cstring> // memcpy
31#include <stdint.h>
32
33#include "ThreadSupport.h"
34
35namespace nidas { namespace util {
36
42inline double flipDoubleIn(const void* p)
43{
44 union {
45 double v;
46 char b[8];
47 } u;
48 const char* cp = (const char*)p;
49 for (int i = 7; i >= 0; i--) u.b[i] = *cp++;
50 return u.v;
51}
52
53inline double flipDouble(const double& p)
54{
55 return flipDoubleIn(&p);
56}
57
63inline float flipFloatIn(const void* p)
64{
65 union {
66 float v;
67 char b[4];
68 } u;
69 const char* cp = (const char*)p;
70 for (int i = 3; i >= 0; i--) u.b[i] = *cp++;
71 return u.v;
72}
73inline float flipFloat(const float& p)
74{
75 return flipFloatIn(&p);
76}
77
78
84inline int64_t flipInt64In(const void* p)
85{
86 union {
87 int64_t v;
88 char b[8];
89 } u;
90 const char* cp = (const char*)p;
91 for (int i = 7; i >= 0; i--) u.b[i] = *cp++;
92 return u.v;
93}
94inline int64_t flipInt64(const int64_t& p)
95{
96 return flipInt64In(&p);
97}
98
99
105inline int32_t flipInt32In(const void* p)
106{
107 union {
108 int32_t v;
109 char b[4];
110 } u;
111 const char* cp = (const char*)p;
112 for (int i = 3; i >= 0; i--) u.b[i] = *cp++;
113 return u.v;
114}
115inline int32_t flipInt32(const int32_t& p)
116{
117 return flipInt32In(&p);
118}
119
120
126inline uint32_t flipUint32In(const void* p)
127{
128 union {
129 uint32_t v;
130 char b[4];
131 } u;
132 const char* cp = (const char*)p;
133 for (int i = 3; i >= 0; i--) u.b[i] = *cp++;
134 return u.v;
135}
136inline uint32_t flipUint32(const uint32_t& p)
137{
138 return flipUint32In(&p);
139}
140
141
147inline int16_t flipInt16In(const void* p)
148{
149 union {
150 int16_t v;
151 char b[2];
152 } u;
153 u.b[1] = ((const char*)p)[0];
154 u.b[0] = ((const char*)p)[1];
155 return u.v;
156}
157inline int16_t flipInt16(const int16_t& p)
158{
159 return flipInt16In(&p);
160}
161
162
168inline uint16_t flipUint16In(const void* p)
169{
170 union {
171 uint16_t v;
172 char b[2];
173 } u;
174 u.b[1] = ((const char*)p)[0];
175 u.b[0] = ((const char*)p)[1];
176 return u.v;
177}
178inline uint16_t flipUint16(const uint16_t& p)
179{
180 return flipUint16In(&p);
181}
182
183
189inline void flipDoubleOut(const double& v,void* p)
190{
191 union {
192 double v;
193 char b[8];
194 } u;
195 u.v = v;
196 char* cp = (char*) p;
197 for (int i = 7; i >= 0; i--) *cp++ = u.b[i];
198}
199
205inline void flipFloatOut(const float& v,void* p)
206{
207 union {
208 float v;
209 char b[4];
210 } u;
211 u.v = v;
212 char* cp = (char*) p;
213 for (int i = 3; i >= 0; i--) *cp++ = u.b[i];
214}
215
221inline void flipInt64Out(const int64_t& v,void* p)
222{
223 union {
224 int64_t v;
225 char b[8];
226 } u;
227 u.v = v;
228 char* cp = (char*) p;
229 for (int i = 7; i >= 0; i--) *cp++ = u.b[i];
230}
231
237inline void flipInt32Out(const int32_t& v,void* p)
238{
239 union {
240 int32_t v;
241 char b[4];
242 } u;
243 u.v = v;
244 char* cp = (char*) p;
245 for (int i = 3; i >= 0; i--) *cp++ = u.b[i];
246}
247
252inline void flipUint32Out(const uint32_t& v,void* p)
253{
254 union {
255 uint32_t v;
256 char b[4];
257 } u;
258 u.v = v;
259 char* cp = (char*) p;
260 for (int i = 3; i >= 0; i--) *cp++ = u.b[i];
261}
262
267inline void flipInt16Out(const int16_t &v,void* p)
268{
269 union {
270 int16_t v;
271 char b[2];
272 } u;
273 u.v = v;
274 char* cp = (char*) p;
275 cp[0] = u.b[1];
276 cp[1] = u.b[0];
277}
278
283inline void flipUint16Out(const uint16_t& v,void *p)
284{
285 union {
286 uint16_t v;
287 char b[2];
288 } u;
289 u.v = v;
290 char* cp = (char*) p;
291 cp[0] = u.b[1];
292 cp[1] = u.b[0];
293}
294
305public:
306
307 virtual ~EndianConverter() {}
308
310
311 // typedef enum endianness endianness_t;
312
314
319
327 static const EndianConverter* getConverter(endianness input, endianness output);
328
337 static const EndianConverter* getConverter(endianness input);
338
343 virtual double doubleValue(const void* ) const = 0;
344
345 virtual double doubleValue(const double& ) const = 0;
346
351 virtual float floatValue(const void* ) const = 0;
352
353 virtual float floatValue(const float& ) const = 0;
354
359 virtual int32_t int32Value(const void* ) const = 0;
360
361 virtual int32_t int32Value(const int32_t& ) const = 0;
362
367 virtual int64_t int64Value(const void* ) const = 0;
368
369 virtual int64_t int64Value(const int64_t& ) const = 0;
370
375 virtual uint32_t uint32Value(const void* ) const = 0;
376
377 virtual uint32_t uint32Value(const uint32_t& ) const = 0;
378
379 virtual int16_t int16Value(const void* ) const = 0;
380
381 virtual int16_t int16Value(const int16_t& ) const = 0;
382
383 virtual uint16_t uint16Value(const void* ) const = 0;
384
385 virtual uint16_t uint16Value(const uint16_t& ) const = 0;
386
391 virtual void doubleCopy(const double&,void* ) const = 0;
392
397 virtual void floatCopy(const float&,void* ) const = 0;
398
403 virtual void int32Copy(const int32_t&,void* ) const = 0;
404
409 virtual void int64Copy(const int64_t&,void* ) const = 0;
410
415 virtual void uint32Copy(const uint32_t&, void* ) const = 0;
416
417 virtual void int16Copy(const int16_t&,void* ) const = 0;
418
419 virtual void uint16Copy(const uint16_t&, void* ) const = 0;
420
421private:
423
425
427
429};
430
436public:
437
438 virtual ~FlipConverter() {}
439
440 double doubleValue(const void* p) const
441 {
442 return flipDoubleIn(p);
443 }
444
445 double doubleValue(const double& p) const
446 {
447 return flipDouble(p);
448 }
449
450 float floatValue(const void* p) const
451 {
452 return flipFloatIn(p);
453 }
454
455 float floatValue(const float& p) const
456 {
457 return flipFloat(p);
458 }
459
460 int64_t int64Value(const void* p) const
461 {
462 return flipInt64In(p);
463 }
464
465 int64_t int64Value(const int64_t& p) const
466 {
467 return flipInt64(p);
468 }
469
470 int32_t int32Value(const void* p) const
471 {
472 return flipInt32In(p);
473 }
474
475 int32_t int32Value(const int32_t& p) const
476 {
477 return flipInt32(p);
478 }
479
480 uint32_t uint32Value(const void* p) const
481 {
482 return flipUint32In(p);
483 }
484
485 uint32_t uint32Value(const uint32_t& p) const
486 {
487 return flipUint32(p);
488 }
489
490 int16_t int16Value(const void* p) const
491 {
492 return flipInt16In(p);
493 }
494
495 int16_t int16Value(const int16_t& p) const
496 {
497 return flipInt16(p);
498 }
499
500 uint16_t uint16Value(const void* p) const
501 {
502 return flipUint16In(p);
503 }
504
505 uint16_t uint16Value(const uint16_t& p) const
506 {
507 return flipUint16(p);
508 }
509
510 void doubleCopy(const double& v,void* p) const
511 {
512 return flipDoubleOut(v,p);
513 }
514
515 void floatCopy(const float& v,void* p) const
516 {
517 return flipFloatOut(v,p);
518 }
519
520 void int64Copy(const int64_t& v,void* p) const
521 {
522 return flipInt64Out(v,p);
523 }
524
525 void int32Copy(const int32_t& v,void* p) const
526 {
527 return flipInt32Out(v,p);
528 }
529
530 void uint32Copy(const uint32_t& v,void* p) const
531 {
532 return flipUint32Out(v,p);
533 }
534
535 void int16Copy(const int16_t &v,void* p) const
536 {
537 return flipInt16Out(v,p);
538 }
539
540 void uint16Copy(const uint16_t& v,void *p) const
541 {
542 return flipUint16Out(v,p);
543 }
544};
545
550public:
551 virtual ~NoFlipConverter() {}
552
553 double doubleValue(const void* p) const
554 {
555 double v;
556 memcpy(&v,p,sizeof(double));
557 return v;
558 }
559
560 double doubleValue(const double& p) const
561 {
562 return p;
563 }
564
565 float floatValue(const void* p) const
566 {
567 float v;
568 memcpy(&v,p,sizeof(float));
569 return v;
570 }
571
572 float floatValue(const float& p) const
573 {
574 return p;
575 }
576
577 int64_t int64Value(const void* p) const
578 {
579 int64_t v;
580 memcpy(&v,p,sizeof(int64_t));
581 return v;
582 }
583
584 int64_t int64Value(const int64_t& p) const
585 {
586 return p;
587 }
588
589 int32_t int32Value(const void* p) const
590 {
591 int32_t v;
592 memcpy(&v,p,sizeof(int32_t));
593 return v;
594 }
595
596 int32_t int32Value(const int32_t& p) const
597 {
598 return p;
599 }
600
601 uint32_t uint32Value(const void* p) const
602 {
603 uint32_t v;
604 memcpy(&v,p,sizeof(uint32_t));
605 return v;
606 }
607
608 uint32_t uint32Value(const uint32_t& p) const
609 {
610 return p;
611 }
612
613 int16_t int16Value(const void* p) const
614 {
615 union {
616 int16_t v;
617 char b[2];
618 } u;
619 u.b[0] = ((const char*)p)[0];
620 u.b[1] = ((const char*)p)[1];
621 return u.v;
622 }
623
624 int16_t int16Value(const int16_t& p) const
625 {
626 return p;
627 }
628
629 uint16_t uint16Value(const void* p) const
630 {
631 union {
632 uint16_t v;
633 char b[2];
634 } u;
635 u.b[0] = ((const char*)p)[0];
636 u.b[1] = ((const char*)p)[1];
637 return u.v;
638 }
639
640 uint16_t uint16Value(const uint16_t& p) const
641 {
642 return p;
643 }
644
645 void doubleCopy(const double& v,void* p) const
646 {
647 memcpy(p,&v,sizeof(double));
648 }
649
650 void floatCopy(const float& v,void* p) const
651 {
652 memcpy(p,&v,sizeof(float));
653 }
654
655 void int64Copy(const int64_t& v,void* p) const
656 {
657 memcpy(p,&v,sizeof(int64_t));
658 }
659
660 void int32Copy(const int& v,void* p) const
661 {
662 memcpy(p,&v,sizeof(int32_t));
663 }
664
665 void uint32Copy(const uint32_t& v,void* p) const
666 {
667 memcpy(p,&v,sizeof(uint32_t));
668 }
669
670 void int16Copy(const int16_t &v,void* p) const
671 {
672 memcpy(p,&v,sizeof(int16_t));
673#ifdef BOZO
674 union {
675 int16_t v;
676 char b[2];
677 } u;
678 u.v = v;
679 ((char*)p)[0] = u.b[1];
680 ((char*)p)[1] = u.b[0];
681#endif
682 }
683
684 void uint16Copy(const uint16_t& v,void* p) const {
685 memcpy(p,&v,sizeof(uint16_t));
686#ifdef BOZO
687 union {
688 uint16_t v;
689 char b[2];
690 } u;
691 u.v = v;
692 ((char*)p)[0] = u.b[1];
693 ((char*)p)[1] = u.b[0];
694#endif
695 }
696};
697}} // namespace nidas namespace util
698#endif
699
Virtual base class declaring methods for converting numeric values between little-endian and big-endi...
Definition EndianConverter.h:304
virtual void int16Copy(const int16_t &, void *) const =0
static EndianConverter * flipConverter
Definition EndianConverter.h:424
virtual uint16_t uint16Value(const uint16_t &) const =0
virtual void int64Copy(const int64_t &, void *) const =0
Copy 8 byte int64_t to the given address, doing endian conversion.
static endianness getHostEndianness()
Return endianness value for this host.
Definition EndianConverter.cc:73
virtual int32_t int32Value(const int32_t &) const =0
endianness
Definition EndianConverter.h:309
@ EC_LITTLE_ENDIAN
Definition EndianConverter.h:309
@ EC_BIG_ENDIAN
Definition EndianConverter.h:309
@ EC_UNKNOWN_ENDIAN
Definition EndianConverter.h:309
virtual int16_t int16Value(const void *) const =0
static endianness privGetHostEndianness()
Definition EndianConverter.cc:49
virtual int16_t int16Value(const int16_t &) const =0
virtual int32_t int32Value(const void *) const =0
Get 4 byte int32 at address, do endian conversion.
virtual float floatValue(const float &) const =0
virtual double doubleValue(const void *) const =0
Get 8 byte double at address, do endian conversion.
virtual void floatCopy(const float &, void *) const =0
Copy 4 byte float to the given address, doing endian conversion.
virtual ~EndianConverter()
Definition EndianConverter.h:307
virtual uint16_t uint16Value(const void *) const =0
virtual float floatValue(const void *) const =0
Get 4 byte float at address, do endian conversion.
virtual uint32_t uint32Value(const void *) const =0
Get 4 byte unsigned int32_t at address, do endian conversion.
static endianness hostEndianness
Definition EndianConverter.h:313
virtual uint32_t uint32Value(const uint32_t &) const =0
virtual double doubleValue(const double &) const =0
virtual void int32Copy(const int32_t &, void *) const =0
Copy 4 byte int to the given address, doing endian conversion.
virtual void uint32Copy(const uint32_t &, void *) const =0
Copy 4 byte unsigned int to the given address, doing endian conversion.
static Mutex staticInitMutex
Definition EndianConverter.h:428
virtual int64_t int64Value(const int64_t &) const =0
virtual void doubleCopy(const double &, void *) const =0
Copy 8 byte double to the given address, doing endian conversion.
virtual int64_t int64Value(const void *) const =0
Get 8 byte int64_t at address, do endian conversion.
static const EndianConverter * getConverter(endianness input, endianness output)
Return an EndianConverter for converting from one endian to another.
Definition EndianConverter.cc:84
static EndianConverter * noflipConverter
Definition EndianConverter.h:426
virtual void uint16Copy(const uint16_t &, void *) const =0
EndianConverter that flips bytes, used for conversion of little-to-big and big-to-little.
Definition EndianConverter.h:435
virtual ~FlipConverter()
Definition EndianConverter.h:438
void doubleCopy(const double &v, void *p) const
Copy 8 byte double to the given address, doing endian conversion.
Definition EndianConverter.h:510
float floatValue(const void *p) const
Get 4 byte float at address, do endian conversion.
Definition EndianConverter.h:450
void int32Copy(const int32_t &v, void *p) const
Copy 4 byte int to the given address, doing endian conversion.
Definition EndianConverter.h:525
uint32_t uint32Value(const uint32_t &p) const
Definition EndianConverter.h:485
void uint16Copy(const uint16_t &v, void *p) const
Definition EndianConverter.h:540
void int64Copy(const int64_t &v, void *p) const
Copy 8 byte int64_t to the given address, doing endian conversion.
Definition EndianConverter.h:520
void int16Copy(const int16_t &v, void *p) const
Definition EndianConverter.h:535
void uint32Copy(const uint32_t &v, void *p) const
Copy 4 byte unsigned int to the given address, doing endian conversion.
Definition EndianConverter.h:530
double doubleValue(const double &p) const
Definition EndianConverter.h:445
double doubleValue(const void *p) const
Get 8 byte double at address, do endian conversion.
Definition EndianConverter.h:440
int32_t int32Value(const void *p) const
Get 4 byte int32 at address, do endian conversion.
Definition EndianConverter.h:470
void floatCopy(const float &v, void *p) const
Copy 4 byte float to the given address, doing endian conversion.
Definition EndianConverter.h:515
uint16_t uint16Value(const uint16_t &p) const
Definition EndianConverter.h:505
int16_t int16Value(const int16_t &p) const
Definition EndianConverter.h:495
int16_t int16Value(const void *p) const
Definition EndianConverter.h:490
uint32_t uint32Value(const void *p) const
Get 4 byte unsigned int32_t at address, do endian conversion.
Definition EndianConverter.h:480
float floatValue(const float &p) const
Definition EndianConverter.h:455
int64_t int64Value(const int64_t &p) const
Definition EndianConverter.h:465
int32_t int32Value(const int32_t &p) const
Definition EndianConverter.h:475
int64_t int64Value(const void *p) const
Get 8 byte int64_t at address, do endian conversion.
Definition EndianConverter.h:460
uint16_t uint16Value(const void *p) const
Definition EndianConverter.h:500
A C++ wrapper for a POSIX mutex.
Definition ThreadSupport.h:161
EndianConverter that doesn't flip bytes.
Definition EndianConverter.h:549
float floatValue(const float &p) const
Definition EndianConverter.h:572
void int32Copy(const int &v, void *p) const
Definition EndianConverter.h:660
void int64Copy(const int64_t &v, void *p) const
Copy 8 byte int64_t to the given address, doing endian conversion.
Definition EndianConverter.h:655
void floatCopy(const float &v, void *p) const
Copy 4 byte float to the given address, doing endian conversion.
Definition EndianConverter.h:650
double doubleValue(const double &p) const
Definition EndianConverter.h:560
double doubleValue(const void *p) const
Get 8 byte double at address, do endian conversion.
Definition EndianConverter.h:553
int16_t int16Value(const void *p) const
Definition EndianConverter.h:613
int32_t int32Value(const void *p) const
Get 4 byte int32 at address, do endian conversion.
Definition EndianConverter.h:589
uint32_t uint32Value(const void *p) const
Get 4 byte unsigned int32_t at address, do endian conversion.
Definition EndianConverter.h:601
virtual ~NoFlipConverter()
Definition EndianConverter.h:551
int32_t int32Value(const int32_t &p) const
Definition EndianConverter.h:596
int16_t int16Value(const int16_t &p) const
Definition EndianConverter.h:624
void uint16Copy(const uint16_t &v, void *p) const
Definition EndianConverter.h:684
int64_t int64Value(const int64_t &p) const
Definition EndianConverter.h:584
uint32_t uint32Value(const uint32_t &p) const
Definition EndianConverter.h:608
void int16Copy(const int16_t &v, void *p) const
Definition EndianConverter.h:670
float floatValue(const void *p) const
Get 4 byte float at address, do endian conversion.
Definition EndianConverter.h:565
void uint32Copy(const uint32_t &v, void *p) const
Copy 4 byte unsigned int to the given address, doing endian conversion.
Definition EndianConverter.h:665
int64_t int64Value(const void *p) const
Get 8 byte int64_t at address, do endian conversion.
Definition EndianConverter.h:577
uint16_t uint16Value(const void *p) const
Definition EndianConverter.h:629
void doubleCopy(const double &v, void *p) const
Copy 8 byte double to the given address, doing endian conversion.
Definition EndianConverter.h:645
uint16_t uint16Value(const uint16_t &p) const
Definition EndianConverter.h:640
float flipFloatIn(const void *p)
Function for reading 4 bytes from an address, flipping the bytes, and returning the float value.
Definition EndianConverter.h:63
double flipDouble(const double &p)
Definition EndianConverter.h:53
float flipFloat(const float &p)
Definition EndianConverter.h:73
double flipDoubleIn(const void *p)
Function for reading 8 bytes from an address, flipping the bytes, and returning the double value.
Definition EndianConverter.h:42
void flipInt32Out(const int32_t &v, void *p)
Function for writing a 4 byte, 32 bit int value to an address, flipping the bytes.
Definition EndianConverter.h:237
uint16_t flipUint16In(const void *p)
Function for reading 2 bytes from an address, flipping the bytes, and returning the unsigned 16 bit i...
Definition EndianConverter.h:168
int16_t flipInt16In(const void *p)
Function for reading 2 bytes from an address, flipping the bytes, and returning the 16 bit int value.
Definition EndianConverter.h:147
int32_t flipInt32In(const void *p)
Function for reading 4 bytes from an address, flipping the bytes, and returning the 32 bit int value.
Definition EndianConverter.h:105
uint32_t flipUint32(const uint32_t &p)
Definition EndianConverter.h:136
int32_t flipInt32(const int32_t &p)
Definition EndianConverter.h:115
uint32_t flipUint32In(const void *p)
Function for reading 4 bytes from an address, flipping the bytes, and returning the unsigned 32 bit i...
Definition EndianConverter.h:126
void flipDoubleOut(const double &v, void *p)
Function for writing an 8 byte double value to an address, flipping the bytes.
Definition EndianConverter.h:189
int16_t flipInt16(const int16_t &p)
Definition EndianConverter.h:157
void flipUint16Out(const uint16_t &v, void *p)
Function for writing a 2 byte unsigned 16 bit int value to an address, flipping the bytes.
Definition EndianConverter.h:283
void flipInt16Out(const int16_t &v, void *p)
Function for writing a 2 byte 16 bit int value to an address, flipping the bytes.
Definition EndianConverter.h:267
void flipUint32Out(const uint32_t &v, void *p)
Function for writing a 4 byte unsigned 32 bit int value to an address, flipping the bytes.
Definition EndianConverter.h:252
int64_t flipInt64(const int64_t &p)
Definition EndianConverter.h:94
void flipInt64Out(const int64_t &v, void *p)
Function for writing an 8 byte, 64 bit int value to an address, flipping the bytes.
Definition EndianConverter.h:221
void flipFloatOut(const float &v, void *p)
Function for writing a 4 byte float value to an address, flipping the bytes.
Definition EndianConverter.h:205
int64_t flipInt64In(const void *p)
Function for reading 8 bytes from an address, flipping the bytes, and returning the int64_t value.
Definition EndianConverter.h:84
uint16_t flipUint16(const uint16_t &p)
Definition EndianConverter.h:178
Root namespace for the NCAR In-Situ Data Acquisition Software.
Definition A2DConverter.h:31