File: Types.h

package info (click to toggle)
libgdf 0.1.3-11.1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,348 kB
  • sloc: cpp: 7,096; makefile: 61; sh: 49
file content (104 lines) | stat: -rw-r--r-- 2,741 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//
// This file is part of libGDF.
//
// libGDF is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
//
// libGDF is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with libGDF.  If not, see <http://www.gnu.org/licenses/>.
//
// Copyright 2010 Martin Billinger

#ifndef __TYPES_H_INCLUDED__
#define __TYPES_H_INCLUDED__

#include "Exceptions.h"
#include <boost/cstdint.hpp>
#include <boost/predef/other/endian.h>
#include <iostream>

namespace gdf
{
    typedef boost::int8_t int8;
    typedef boost::uint8_t uint8;
    typedef boost::int16_t int16;
    typedef boost::uint16_t uint16;
    typedef boost::int32_t int32;
    typedef boost::uint32_t uint32;
    typedef boost::int64_t int64;
    typedef boost::uint64_t uint64;
    typedef float float32;
    typedef double float64;

    enum type_id
    {
        INVALID_TYPE = 0,
        INT8 = 1,
        UINT8 = 2,
        INT16 = 3,
        UINT16 = 4,
        INT32 = 5,
        UINT32 = 6,
        INT64 = 7,
        UINT64 = 8,

        FLOAT32 = 16,
        FLOAT64 = 17
    };

    size_t datatype_size( uint32 t );

    template<typename T>
    T switch_endian( const T &source )
    {
        T dest;
        const char *A = reinterpret_cast<const char*>(&source);
        char *B = reinterpret_cast<char*>(&dest);

        int a = 0;
        int b = sizeof(T)-1;

        while( b >= 0 )
        {
            B[b] = A[a];
        }
    }

    template<typename T>
    void writeLittleEndian( std::ostream &out, T item )
    {
#if BOOST_ENDIAN_LITTLE_BYTE
        out.write( reinterpret_cast<const char*>(&item), sizeof(item) );
#elif BOOST_ENDIAN_BIG_BYTE
        const char* p = reinterpret_cast<const char*>(&item)   sizeof(item)-1;
        for( size_t i=0; i<sizeof(item); i   )
            out.write( p--, 1 );
#else
    #error "Unable to determine system endianness."
#endif
    }

    template<typename T>
    void readLittleEndian( std::istream &in, T &item )
    {
#if BOOST_ENDIAN_LITTLE_BYTE
        in.read( reinterpret_cast<char*>(&item), sizeof(item) );
#elif BOOST_ENDIAN_BIG_BYTE
        char* p = reinterpret_cast<char*>(&item)   sizeof(item)-1;
        for( size_t i=0; i<sizeof(item); i   )
    in.read( p--, 1 );
#else
    #error "Unable to determine system endianness."
#endif
    }

}

#endif