monotone

monotone Mtn Source Tree

Root/numeric_vocab.hh

1#ifndef __NUMERIC_VOCAB__
2#define __NUMERIC_VOCAB__
3
4// copyright (C) 2004 graydon hoare <graydon@pobox.com>
5// all rights reserved.
6// licensed to the public under the terms of the GNU GPL (>= 2)
7// see the file COPYING for details
8
9#include <limits>
10
11#include "mt-stdint.h"
12#include <boost/static_assert.hpp>
13
14typedef uint8_t u8;
15typedef uint16_t u16;
16typedef uint32_t u32;
17typedef uint64_t u64;
18
19BOOST_STATIC_ASSERT(sizeof(char) == 1);
20BOOST_STATIC_ASSERT(CHAR_BIT == 8);
21
22template <typename T, typename V>
23inline T
24widen(V const & v)
25{
26 BOOST_STATIC_ASSERT(sizeof(T) >= sizeof(V));
27 if (std::numeric_limits<T>::is_signed)
28 return static_cast<T>(v);
29 else
30 {
31 T mask = std::numeric_limits<T>::max();
32 size_t shift = (sizeof(T) - sizeof(V)) * 8;
33 mask >>= shift;
34 return static_cast<T>(v) & mask;
35 }
36}
37
38#endif

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status