cisst-saw
Loading...
Searching...
No Matches
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 Author(s): Anton Deguet
6 Created on: 2011-06-27
7
8 (C) Copyright 2011-2018 Johns Hopkins University (JHU), All Rights Reserved.
9
10--- begin cisst license - do not edit ---
11
12This software is provided "as is" under an open source license, with
13no warranty. The complete license can be found in license.txt and
14http://www.cisst.org/cisst/license.txt.
15
16--- end cisst license ---
17*/
18
19#pragma once
20#ifndef _cmnDataFormat_h
21#define _cmnDataFormat_h
22
25
26// Always include last
28
29class CISST_EXPORT cmnDataFormat
30{
31 cmnDataFormat(const cmnDataFormat & other);
32 friend class cmnDataTest;
33
34public:
35 typedef enum {CMN_DATA_32_BITS, CMN_DATA_64_BITS} WordSize;
36 typedef enum {CMN_DATA_LITTLE_ENDIAN, CMN_DATA_BIG_ENDIAN} Endianness;
38
43
44 inline const WordSize & GetWordSize(void) const {
45 return this->WordSizeMember;
46 }
47
48 inline const Endianness & GetEndianness(void) const {
49 return this->EndiannessMember;
50 }
51
52 inline const SizeTSize & GetSizeTSize(void) const {
53 return this->SizeTSizeMember;
54 }
55
56private:
57 WordSize WordSizeMember;
58 Endianness EndiannessMember;
59 SizeTSize SizeTSizeMember;
60};
61
62
63template <class _elementType, size_t _sizeInBytes>
65{
66 // this method is private to make sure a compilation error will
67 // happen if ones try to swap bytes on unsupported sizes
68 inline static void Execute(_elementType & CMN_UNUSED(data)) CISST_THROW(std::runtime_error) {
69 cmnThrow("cmnDataByteSwap: a partial specialization should be called!");
70 }
71};
72
73template <class _elementType>
74class cmnDataByteSwapClass<_elementType, 1>
75{
76 // this method is private to make sure a compilation error will
77 // happen if ones try to swap bytes on a one byte object
78 inline static void Execute(_elementType & CMN_UNUSED(data)) CISST_THROW(std::runtime_error) {
79 }
80};
81
82template <class _elementType>
83class cmnDataByteSwapClass<_elementType, 2>
84{
85public:
86 inline static void Execute(_elementType & data) CISST_THROW(std::runtime_error) {
87 *(unsigned short *)&(data) = ( ((*(unsigned short *)&(data) & 0xff) << 8) |
88 (*(unsigned short *)&(data) >> 8) );
89 }
90};
91
92template <class _elementType>
93class cmnDataByteSwapClass<_elementType, 4>
94{
95public:
96 inline static void Execute(_elementType & data) CISST_THROW(std::runtime_error) {
97 *(unsigned int *)&(data) = ( ((*(unsigned int *)&(data) & 0xff000000) >> 24) |
98 ((*(unsigned int *)&(data) & 0x00ff0000) >> 8) |
99 ((*(unsigned int *)&(data) & 0x0000ff00) << 8) |
100 ((*(unsigned int *)&(data) & 0x000000ff) << 24) );
101 }
102};
103
104template <class _elementType>
105class cmnDataByteSwapClass<_elementType, 8>
106{
107public:
108 inline static void Execute(_elementType & data) CISST_THROW(std::runtime_error) {
109 *(unsigned long long int *)&(data) = ( ((*(unsigned long long int *)&(data) & 0xff00000000000000ULL) >> 56) |
110 ((*(unsigned long long int *)&(data) & 0x00ff000000000000ULL) >> 40) |
111 ((*(unsigned long long int *)&(data) & 0x0000ff0000000000ULL) >> 24) |
112 ((*(unsigned long long int *)&(data) & 0x000000ff00000000ULL) >> 8) |
113 ((*(unsigned long long int *)&(data) & 0x00000000ff000000ULL) << 8) |
114 ((*(unsigned long long int *)&(data) & 0x0000000000ff0000ULL) << 24) |
115 ((*(unsigned long long int *)&(data) & 0x000000000000ff00ULL) << 40) |
116 ((*(unsigned long long int *)&(data) & 0x00000000000000ffULL) << 56) );
117 }
118};
119
120template <class _elementType>
121void cmnDataByteSwap(_elementType & data) {
122 cmnDataByteSwapClass<_elementType, sizeof(_elementType)>::Execute(data);
123}
124
125#endif // _cmnDataFormat_h
static void Execute(_elementType &data) CISST_THROW(std
Definition cmnDataFormat.h:86
static void Execute(_elementType &data) CISST_THROW(std
Definition cmnDataFormat.h:96
static void Execute(_elementType &data) CISST_THROW(std
Definition cmnDataFormat.h:108
Definition cmnDataFormat.h:65
friend class cmnDataTest
Definition cmnDataFormat.h:32
Endianness
Definition cmnDataFormat.h:36
@ CMN_DATA_BIG_ENDIAN
Definition cmnDataFormat.h:36
@ CMN_DATA_LITTLE_ENDIAN
Definition cmnDataFormat.h:36
const WordSize & GetWordSize(void) const
Definition cmnDataFormat.h:44
cmnDataFormat(void)
WordSize
Definition cmnDataFormat.h:35
@ CMN_DATA_32_BITS
Definition cmnDataFormat.h:35
@ CMN_DATA_64_BITS
Definition cmnDataFormat.h:35
SizeTSize
Definition cmnDataFormat.h:37
@ CMN_DATA_SIZE_T_SIZE_64
Definition cmnDataFormat.h:37
@ CMN_DATA_SIZE_T_SIZE_32
Definition cmnDataFormat.h:37
const Endianness & GetEndianness(void) const
Definition cmnDataFormat.h:48
const SizeTSize & GetSizeTSize(void) const
Definition cmnDataFormat.h:52
void cmnDataByteSwap(_elementType &data)
Definition cmnDataFormat.h:121
Macros to export the symbols of cisstCommon (in a Dll).
#define CISST_EXPORT
Definition cmnExportMacros.h:50
Portability across compilers and operating systems tools.
#define CMN_UNUSED(argument)
Definition cmnPortability.h:497
#define CISST_THROW(exceptionParameter)
Somewhat portable compilation warning message. This works with very recent versions of gcc (4....
Definition cmnPortability.h:559
Declaration of the template function cmnThrow.
void cmnThrow(const _exceptionType &except, cmnLogLevel lod=CMN_LOG_LEVEL_INIT_ERROR)
Definition cmnThrow.h:76