cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cmnDataFormat.h
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */
3 
4 /*
5 
6  Author(s): Anton Deguet
7  Created on: 2011-06-27
8 
9  (C) Copyright 2011-2013 Johns Hopkins University (JHU), All Rights
10  Reserved.
11 
12 --- begin cisst license - do not edit ---
13 
14 This software is provided "as is" under an open source license, with
15 no warranty. The complete license can be found in license.txt and
16 http://www.cisst.org/cisst/license.txt.
17 
18 --- end cisst license ---
19 
20 */
21 
22 #pragma once
23 #ifndef _cmnDataFormat_h
24 #define _cmnDataFormat_h
25 
27 #include <cisstCommon/cmnThrow.h>
28 
29 // Always include last
30 #include <cisstCommon/cmnExport.h>
31 
33 {
34  cmnDataFormat(const cmnDataFormat & other);
35  friend class cmnDataTest;
36 
37 public:
38  typedef enum {CMN_DATA_32_BITS, CMN_DATA_64_BITS} WordSize;
39  typedef enum {CMN_DATA_LITTLE_ENDIAN, CMN_DATA_BIG_ENDIAN} Endianness;
40  typedef enum {CMN_DATA_SIZE_T_SIZE_32, CMN_DATA_SIZE_T_SIZE_64} SizeTSize;
41 
45  cmnDataFormat(void);
46 
47  inline const WordSize & GetWordSize(void) const {
48  return this->WordSizeMember;
49  }
50 
51  inline const Endianness & GetEndianness(void) const {
52  return this->EndiannessMember;
53  }
54 
55  inline const SizeTSize & GetSizeTSize(void) const {
56  return this->SizeTSizeMember;
57  }
58 
59 private:
60  WordSize WordSizeMember;
61  Endianness EndiannessMember;
62  SizeTSize SizeTSizeMember;
63 };
64 
65 
66 template <class _elementType, size_t _sizeInBytes>
68 {
69  // this method is private to make sure a compilation error will
70  // happen if ones try to swap bytes on unsupported sizes
71  inline static void Execute(_elementType & CMN_UNUSED(data)) throw (std::runtime_error) {
72  cmnThrow("cmnDataByteSwap: a partial specialization should be called!");
73  }
74 };
75 
76 template <class _elementType>
77 class cmnDataByteSwapClass<_elementType, 1>
78 {
79  // this method is private to make sure a compilation error will
80  // happen if ones try to swap bytes on a one byte object
81  inline static void Execute(_elementType & CMN_UNUSED(data)) throw (std::runtime_error) {
82  }
83 };
84 
85 template <class _elementType>
86 class cmnDataByteSwapClass<_elementType, 2>
87 {
88 public:
89  inline static void Execute(_elementType & data) throw (std::runtime_error) {
90  *(unsigned short *)&(data) = ( ((*(unsigned short *)&(data) & 0xff) << 8) |
91  (*(unsigned short *)&(data) >> 8) );
92  }
93 };
94 
95 template <class _elementType>
96 class cmnDataByteSwapClass<_elementType, 4>
97 {
98 public:
99  inline static void Execute(_elementType & data) throw (std::runtime_error) {
100  *(unsigned int *)&(data) = ( ((*(unsigned int *)&(data) & 0xff000000) >> 24) |
101  ((*(unsigned int *)&(data) & 0x00ff0000) >> 8) |
102  ((*(unsigned int *)&(data) & 0x0000ff00) << 8) |
103  ((*(unsigned int *)&(data) & 0x000000ff) << 24) );
104  }
105 };
106 
107 template <class _elementType>
108 class cmnDataByteSwapClass<_elementType, 8>
109 {
110 public:
111  inline static void Execute(_elementType & data) throw (std::runtime_error) {
112  *(unsigned long long int *)&(data) = ( ((*(unsigned long long int *)&(data) & 0xff00000000000000ULL) >> 56) |
113  ((*(unsigned long long int *)&(data) & 0x00ff000000000000ULL) >> 40) |
114  ((*(unsigned long long int *)&(data) & 0x0000ff0000000000ULL) >> 24) |
115  ((*(unsigned long long int *)&(data) & 0x000000ff00000000ULL) >> 8) |
116  ((*(unsigned long long int *)&(data) & 0x00000000ff000000ULL) << 8) |
117  ((*(unsigned long long int *)&(data) & 0x0000000000ff0000ULL) << 24) |
118  ((*(unsigned long long int *)&(data) & 0x000000000000ff00ULL) << 40) |
119  ((*(unsigned long long int *)&(data) & 0x00000000000000ffULL) << 56) );
120  }
121 };
122 
123 template <class _elementType>
124 void cmnDataByteSwap(_elementType & data) {
126 }
127 
128 #endif // _cmnDataFormat_h
#define CISST_EXPORT
Definition: cmnExportMacros.h:50
Definition: cmnDataFormat.h:32
WordSize
Definition: cmnDataFormat.h:38
Portability across compilers and operating systems tools.
#define CMN_UNUSED(argument)
Definition: cmnPortability.h:479
Definition: cmnDataFormat.h:67
const SizeTSize & GetSizeTSize(void) const
Definition: cmnDataFormat.h:55
static void Execute(_elementType &data)
Definition: cmnDataFormat.h:99
Endianness
Definition: cmnDataFormat.h:39
void cmnDataByteSwap(_elementType &data)
Definition: cmnDataFormat.h:124
SizeTSize
Definition: cmnDataFormat.h:40
Macros to export the symbols of cisstCommon (in a Dll).
static void Execute(_elementType &data)
Definition: cmnDataFormat.h:111
#define cmnThrow(a)
Definition: MinimalCmn.h:4
static void Execute(_elementType &data)
Definition: cmnDataFormat.h:89
const Endianness & GetEndianness(void) const
Definition: cmnDataFormat.h:51
Declaration of the template function cmnThrow.
const WordSize & GetWordSize(void) const
Definition: cmnDataFormat.h:47