gfx/graphite2/src/inc/Endian.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /*  GRAPHITE2 LICENSING
     3     Copyright 2011, SIL International
     4     All rights reserved.
     6     This library is free software; you can redistribute it and/or modify
     7     it under the terms of the GNU Lesser General Public License as published
     8     by the Free Software Foundation; either version 2.1 of License, or
     9     (at your option) any later version.
    11     This program is distributed in the hope that it will be useful,
    12     but WITHOUT ANY WARRANTY; without even the implied warranty of
    13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    14     Lesser General Public License for more details.
    16     You should also have received a copy of the GNU Lesser General Public
    17     License along with this library in the file named "LICENSE".
    18     If not, write to the Free Software Foundation, 51 Franklin Street,
    19     Suite 500, Boston, MA 02110-1335, USA or visit their web page on the
    20     internet at http://www.fsf.org/licenses/lgpl.html.
    22 Alternatively, the contents of this file may be used under the terms of the
    23 Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public
    24 License, as published by the Free Software Foundation, either version 2
    25 of the License or (at your option) any later version.
    26 */
    28 /*
    29 Description:
    30     A set of fast template based decoders for decoding values of any C integer
    31     type up to long int size laid out with most significant byte first or least
    32     significant byte first (aka big endian or little endian).  These are CPU
    33     byte order agnostic and will function the same regardless of the CPUs native
    34     byte order.
    36     Being template based means if the either le or be class is not used then
    37     template code of unused functions will not be instantiated by the compiler
    38     and thus shouldn't cause any overhead.
    39 */
    41 #include <cstddef>
    43 #pragma once
    46 class be
    47 {
    48     template<int S>
    49     inline static unsigned long int _peek(const unsigned char * p) {
    50         return _peek<S/2>(p) << (S/2)*8 | _peek<S/2>(p+S/2);
    51     }
    52 public:
    53     template<typename T>
    54     inline static T peek(const void * p) {
    55         return T(_peek<sizeof(T)>(static_cast<const unsigned char *>(p)));
    56     }
    58     template<typename T>
    59     inline static T read(const unsigned char * &p) {
    60         const T r = T(_peek<sizeof(T)>(p)); 
    61         p += sizeof r;
    62         return r;
    63     }
    65     template<typename T>
    66     inline static T swap(const T x) {
    67         return T(_peek<sizeof(T)>(reinterpret_cast<const unsigned char *>(&x)));
    68     }
    70     template<typename T>
    71     inline static void skip(const unsigned char * &p, size_t n=1) {
    72         p += sizeof(T)*n;
    73     }
    74 };
    76 template<>
    77 inline unsigned long int be::_peek<1>(const unsigned char * p) { return *p; }
    80 class le 
    81 {
    82     template<int S>
    83     inline static unsigned long int _peek(const unsigned char * p) {
    84         return _peek<S/2>(p) | _peek<S/2>(p+S/2)  << (S/2)*8;
    85     }
    86 public:
    87     template<typename T>
    88     inline static T peek(const void * p) {
    89         return T(_peek<sizeof(T)>(static_cast<const unsigned char *>(p)));
    90     }
    92     template<typename T>
    93     inline static T read(const unsigned char * &p) {
    94         const T r = T(_peek<sizeof(T)>(p)); 
    95         p += sizeof r;
    96         return r;
    97     }
    99     template<typename T>
   100     inline static T swap(const T x) {
   101         return T(_peek<sizeof(T)>(reinterpret_cast<const unsigned char *>(&x)));
   102     }
   104     template<typename T>
   105     inline static void skip(const unsigned char * &p, size_t n=1) {
   106         p += sizeof(T)*n;
   107     }
   108 };
   110 template<>
   111 inline unsigned long int le::_peek<1>(const unsigned char * p) { return *p; }

mercurial