Path: blob/master/thirdparty/graphite/src/inc/Endian.h
9906 views
// SPDX-License-Identifier: MIT OR MPL-2.0 OR LGPL-2.1-or-later OR GPL-2.0-or-later1// Copyright 2011, SIL International, All rights reserved.2/*3Description:4A set of fast template based decoders for decoding values of any C integer5type up to long int size laid out with most significant byte first or least6significant byte first (aka big endian or little endian). These are CPU7byte order agnostic and will function the same regardless of the CPUs native8byte order.910Being template based means if the either le or be class is not used then11template code of unused functions will not be instantiated by the compiler12and thus shouldn't cause any overhead.13*/1415#include <cstddef>1617#pragma once181920class be21{22template<int S>23inline static unsigned long int _peek(const unsigned char * p) {24return _peek<S/2>(p) << (S/2)*8 | _peek<S/2>(p+S/2);25}26public:27template<typename T>28inline static T peek(const void * p) {29return T(_peek<sizeof(T)>(static_cast<const unsigned char *>(p)));30}3132template<typename T>33inline static T read(const unsigned char * &p) {34const T r = T(_peek<sizeof(T)>(p));35p += sizeof r;36return r;37}3839template<typename T>40inline static T swap(const T x) {41return T(_peek<sizeof(T)>(reinterpret_cast<const unsigned char *>(&x)));42}4344template<typename T>45inline static void skip(const unsigned char * &p, size_t n=1) {46p += sizeof(T)*n;47}48};4950template<>51inline unsigned long int be::_peek<1>(const unsigned char * p) { return *p; }525354class le55{56template<int S>57inline static unsigned long int _peek(const unsigned char * p) {58return _peek<S/2>(p) | _peek<S/2>(p+S/2) << (S/2)*8;59}60public:61template<typename T>62inline static T peek(const void * p) {63return T(_peek<sizeof(T)>(static_cast<const unsigned char *>(p)));64}6566template<typename T>67inline static T read(const unsigned char * &p) {68const T r = T(_peek<sizeof(T)>(p));69p += sizeof r;70return r;71}7273template<typename T>74inline static T swap(const T x) {75return T(_peek<sizeof(T)>(reinterpret_cast<const unsigned char *>(&x)));76}7778template<typename T>79inline static void skip(const unsigned char * &p, size_t n=1) {80p += sizeof(T)*n;81}82};8384template<>85inline unsigned long int le::_peek<1>(const unsigned char * p) { return *p; }868788