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//
6// This program is made available under the GNU GPL version 2.0 or
7// greater. See the accompanying file COPYING for details.
8//
9// This program is distributed WITHOUT ANY WARRANTY; without even the
10// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11// PURPOSE.
12
13#include <limits>
14
15#include "mt-stdint.h"
16#include <boost/static_assert.hpp>
17
18typedef uint8_t u8;
19typedef uint16_t u16;
20typedef uint32_t u32;
21typedef uint64_t u64;
22
23BOOST_STATIC_ASSERT(sizeof(char) == 1);
24BOOST_STATIC_ASSERT(CHAR_BIT == 8);
25
26template <typename T, typename V>
27inline T
28widen(V const & v)
29{
30 BOOST_STATIC_ASSERT(sizeof(T) >= sizeof(V));
31 if (std::numeric_limits<T>::is_signed)
32 return static_cast<T>(v);
33 else
34 {
35 T mask = std::numeric_limits<T>::max();
36 size_t shift = (sizeof(T) - sizeof(V)) * 8;
37 mask >>= shift;
38 return static_cast<T>(v) & mask;
39 }
40}
41
42// Local Variables:
43// mode: C++
44// fill-column: 76
45// c-file-style: "gnu"
46// indent-tabs-mode: nil
47// End:
48// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s:
49
50#endif

Archive Download this file

Branches

Tags

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