gfx/graphite2/src/inc/Endian.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/graphite2/src/inc/Endian.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,112 @@
     1.4 +/*  GRAPHITE2 LICENSING
     1.5 +
     1.6 +    Copyright 2011, SIL International
     1.7 +    All rights reserved.
     1.8 +
     1.9 +    This library is free software; you can redistribute it and/or modify
    1.10 +    it under the terms of the GNU Lesser General Public License as published
    1.11 +    by the Free Software Foundation; either version 2.1 of License, or
    1.12 +    (at your option) any later version.
    1.13 +
    1.14 +    This program is distributed in the hope that it will be useful,
    1.15 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.16 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.17 +    Lesser General Public License for more details.
    1.18 +
    1.19 +    You should also have received a copy of the GNU Lesser General Public
    1.20 +    License along with this library in the file named "LICENSE".
    1.21 +    If not, write to the Free Software Foundation, 51 Franklin Street,
    1.22 +    Suite 500, Boston, MA 02110-1335, USA or visit their web page on the
    1.23 +    internet at http://www.fsf.org/licenses/lgpl.html.
    1.24 +
    1.25 +Alternatively, the contents of this file may be used under the terms of the
    1.26 +Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public
    1.27 +License, as published by the Free Software Foundation, either version 2
    1.28 +of the License or (at your option) any later version.
    1.29 +*/
    1.30 +
    1.31 +/*
    1.32 +Description:
    1.33 +    A set of fast template based decoders for decoding values of any C integer
    1.34 +    type up to long int size laid out with most significant byte first or least
    1.35 +    significant byte first (aka big endian or little endian).  These are CPU
    1.36 +    byte order agnostic and will function the same regardless of the CPUs native
    1.37 +    byte order.
    1.38 +
    1.39 +    Being template based means if the either le or be class is not used then
    1.40 +    template code of unused functions will not be instantiated by the compiler
    1.41 +    and thus shouldn't cause any overhead.
    1.42 +*/
    1.43 +
    1.44 +#include <cstddef>
    1.45 +
    1.46 +#pragma once
    1.47 +
    1.48 +
    1.49 +class be
    1.50 +{
    1.51 +    template<int S>
    1.52 +    inline static unsigned long int _peek(const unsigned char * p) {
    1.53 +        return _peek<S/2>(p) << (S/2)*8 | _peek<S/2>(p+S/2);
    1.54 +    }
    1.55 +public:
    1.56 +    template<typename T>
    1.57 +    inline static T peek(const void * p) {
    1.58 +        return T(_peek<sizeof(T)>(static_cast<const unsigned char *>(p)));
    1.59 +    }
    1.60 +
    1.61 +    template<typename T>
    1.62 +    inline static T read(const unsigned char * &p) {
    1.63 +        const T r = T(_peek<sizeof(T)>(p)); 
    1.64 +        p += sizeof r;
    1.65 +        return r;
    1.66 +    }
    1.67 +    
    1.68 +    template<typename T>
    1.69 +    inline static T swap(const T x) {
    1.70 +        return T(_peek<sizeof(T)>(reinterpret_cast<const unsigned char *>(&x)));
    1.71 +    }
    1.72 +
    1.73 +    template<typename T>
    1.74 +    inline static void skip(const unsigned char * &p, size_t n=1) {
    1.75 +        p += sizeof(T)*n;
    1.76 +    }
    1.77 +};
    1.78 +
    1.79 +template<>
    1.80 +inline unsigned long int be::_peek<1>(const unsigned char * p) { return *p; }
    1.81 +
    1.82 +
    1.83 +class le 
    1.84 +{
    1.85 +    template<int S>
    1.86 +    inline static unsigned long int _peek(const unsigned char * p) {
    1.87 +        return _peek<S/2>(p) | _peek<S/2>(p+S/2)  << (S/2)*8;
    1.88 +    }
    1.89 +public:
    1.90 +    template<typename T>
    1.91 +    inline static T peek(const void * p) {
    1.92 +        return T(_peek<sizeof(T)>(static_cast<const unsigned char *>(p)));
    1.93 +    }
    1.94 +
    1.95 +    template<typename T>
    1.96 +    inline static T read(const unsigned char * &p) {
    1.97 +        const T r = T(_peek<sizeof(T)>(p)); 
    1.98 +        p += sizeof r;
    1.99 +        return r;
   1.100 +    }
   1.101 +    
   1.102 +    template<typename T>
   1.103 +    inline static T swap(const T x) {
   1.104 +        return T(_peek<sizeof(T)>(reinterpret_cast<const unsigned char *>(&x)));
   1.105 +    }
   1.106 +
   1.107 +    template<typename T>
   1.108 +    inline static void skip(const unsigned char * &p, size_t n=1) {
   1.109 +        p += sizeof(T)*n;
   1.110 +    }
   1.111 +};
   1.112 +
   1.113 +template<>
   1.114 +inline unsigned long int le::_peek<1>(const unsigned char * p) { return *p; }
   1.115 +

mercurial