DCCL v3
common.h
1 // Copyright 2009-2017 Toby Schneider (http://gobysoft.org/index.wt/people/toby)
2 // GobySoft, LLC (for 2013-)
3 // Massachusetts Institute of Technology (for 2007-2014)
4 // Community contributors (see AUTHORS file)
5 //
6 //
7 // This file is part of the Dynamic Compact Control Language Library
8 // ("DCCL").
9 //
10 // DCCL is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 2.1 of the License, or
13 // (at your option) any later version.
14 //
15 // DCCL is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with DCCL. If not, see <http://www.gnu.org/licenses/>.
22 #ifndef DCCLConstants20091211H
23 #define DCCLConstants20091211H
24 
25 #include <iostream>
26 #include <cmath>
27 #include <limits>
28 
29 #include <boost/type_traits/is_integral.hpp>
30 #include <boost/type_traits/is_floating_point.hpp>
31 #include <boost/utility/enable_if.hpp>
32 
33 #include <google/protobuf/message.h>
34 #include <google/protobuf/descriptor.h>
35 
36 #include "dccl/bitset.h"
37 
38 
39 namespace dccl
40 {
41  inline unsigned floor_bits2bytes(unsigned bits)
42  { return bits >> 3; }
43  // more efficient way to do ceil(total_bits / 8)
44  // to get the number of bytes rounded up.
45  inline unsigned ceil_bits2bytes(unsigned bits)
46  {
47  enum { BYTE_MASK = 7 }; // 00000111
48  return (bits& BYTE_MASK) ?
49  floor_bits2bytes(bits) + 1 :
50  floor_bits2bytes(bits);
51  }
52 
53  // use the Google Protobuf types as they handle system quirks already
55  typedef google::protobuf::uint32 uint32;
57  typedef google::protobuf::int32 int32;
59  typedef google::protobuf::uint64 uint64;
61  typedef google::protobuf::int64 int64;
62 
63  const unsigned BITS_IN_BYTE = 8;
64 
65  inline std::ostream& operator<<(std::ostream& out,
66  const google::protobuf::Message& msg)
67  {
68  return (out << "[["
69  << msg.GetDescriptor()->name()
70  << "]] " << msg.DebugString());
71  }
72 
73  template<typename Float>
74  Float round(Float d)
75  { return std::floor(d + 0.5); }
76 
81  template<typename Float>
82  typename boost::enable_if<boost::is_floating_point<Float>, Float>::type round(Float value, int precision)
83  {
84  Float scaling = std::pow(10.0, precision);
85  return round(value*scaling)/scaling;
86  }
87 
88  // C++98 has no long long overload for abs
89  template<typename Int>
90  Int abs(Int i) { return (i < 0) ? -i : i; }
91 
96  template<typename Int>
97  typename boost::enable_if<boost::is_integral<Int>, Int>::type round(Int value, int precision)
98  {
99  if(precision >= 0)
100  {
101  // doesn't mean anything to round an integer to positive precision
102  return value;
103  }
104  else
105  {
106  Int scaling = (Int)std::pow(10.0, -precision);
107  Int remainder = value % scaling;
108 
109  value -= remainder;
110  if(remainder >= scaling/2)
111  value += scaling;
112 
113  return value;
114  }
115  }
116 
117 
118 }
119 #endif
google::protobuf::int64 int64
a signed 64 bit integer
Definition: common.h:61
google::protobuf::uint32 uint32
an unsigned 32 bit integer
Definition: common.h:55
google::protobuf::uint64 uint64
an unsigned 64 bit integer
Definition: common.h:59
Dynamic Compact Control Language namespace.
google::protobuf::int32 int32
a signed 32 bit integer
Definition: common.h:57