← Back to team overview

kicad-developers team mailing list archive

[PATCH] Add size_t to "common.h" and remove KIROUND in favor of constexpr.

 

Add size_t to common.h for everyone to use. While touching that file also remove KIROUND macro and make the function constexpr replacing the compile-time functionality of the macro.
>From 18cc52ed083a4c76ed3a845a77f0167d76bd5534 Mon Sep 17 00:00:00 2001
From: decimad <michsteinb@xxxxxxxxx>
Date: Tue, 5 Jul 2016 19:09:37 +0200
Subject: [PATCH] Add size_t declaration to "common.h". Remove KIROUND. Make
 KiRound constexpr.

---
 include/common.h | 24 ++++++++++--------------
 1 file changed, 10 insertions(+), 14 deletions(-)

diff --git a/include/common.h b/include/common.h
index a80c677..f3a90a8 100644
--- a/include/common.h
+++ b/include/common.h
@@ -32,9 +32,12 @@
 #ifndef INCLUDE__COMMON_H_
 #define INCLUDE__COMMON_H_
 
-#include <vector>
+#include <vector>   /// @todo Not used throughout this header, arguably doesn't belong here.
 #include <boost/cstdint.hpp>
 
+#include <cstddef>
+using std::size_t;   // better be defensive.
+
 #include <wx/wx.h>
 #include <wx/confbase.h>
 #include <wx/fileconf.h>
@@ -123,17 +126,12 @@ enum pseudokeys {
 
 #if !defined( DEBUG )
 
-/// KiROUND: a function so v is not evaluated twice.  Unfortunately, compiler
-/// is unable to pre-compute constants using this.
-static inline int KiROUND( double v )
+/// KiROUND: round double to integer, no argument range check in release build.
+constexpr int KiROUND( double v )
 {
-    return int( v < 0 ? v - 0.5 : v + 0.5 );
+    return static_cast<int>( v < 0 ? v - 0.5 : v + 0.5 );
 }
 
-/// KIROUND: a macro so compiler can pre-compute constants.  Use this with compile
-/// time constants rather than the inline function above.
-#define KIROUND( v )    int( (v) < 0 ? (v) - 0.5 : (v) + 0.5 )
-
 #else
 
 // DEBUG: KiROUND() is a macro to capture line and file, then calls this inline
@@ -154,9 +152,6 @@ static inline int kiRound_( double v, int line, const char* filename )
 
 #define KiROUND( v )    kiRound_( v, __LINE__, __FILE__ )
 
-// in Debug build, use the overflow catcher since code size is immaterial
-#define KIROUND( v )    KiROUND( v )
-
 #endif
 
 //-----</KiROUND KIT>-----------------------------------------------------------
@@ -164,10 +159,10 @@ static inline int kiRound_( double v, int line, const char* filename )
 
 
 /// Convert mm to mils.
-inline int Mm2mils( double x ) { return KiROUND( x * 1000./25.4 ); }
+constexpr int Mm2mils( double x ) { return KiROUND( x * 1000./25.4 ); }
 
 /// Convert mils to mm.
-inline int Mils2mm( double x ) { return KiROUND( x * 25.4 / 1000. ); }
+constexpr int Mils2mm( double x ) { return KiROUND( x * 25.4 / 1000. ); }
 
 
 enum EDA_UNITS_T {
@@ -201,6 +196,7 @@ public:
 private:
     void setUserLocale( const char* aUserLocale );
 
+    /// @todo Everybody includes <atomic> because if this implementation detail.
     // allow for nesting of LOCALE_IO instantiations
     static std::atomic<unsigned int> m_c_count;
 
-- 
2.9.0.windows.1