[PPL-devel] [GIT] ppl/ppl(master): Magic constants avoided.

Roberto Bagnara bagnara at cs.unipr.it
Sun Oct 9 20:59:25 CEST 2011


Module: ppl/ppl
Branch: master
Commit: a52e104fe997e8347139bc14cd6d1c44f40f9d08
URL:    http://www.cs.unipr.it/git/gitweb.cgi?p=ppl/ppl.git;a=commit;h=a52e104fe997e8347139bc14cd6d1c44f40f9d08

Author: Roberto Bagnara <bagnara at cs.unipr.it>
Date:   Sun Oct  9 19:33:37 2011 +0200

Magic constants avoided.
Detected by ECLAIR service nomagicc.

---

 Watchdog/src/Time.cc         |    2 +-
 Watchdog/src/Time.defs.hh    |    6 ++++++
 Watchdog/src/Time.inlines.hh |   17 +++++++++--------
 3 files changed, 16 insertions(+), 9 deletions(-)

diff --git a/Watchdog/src/Time.cc b/Watchdog/src/Time.cc
index 9b52e4b..afc3aaf 100644
--- a/Watchdog/src/Time.cc
+++ b/Watchdog/src/Time.cc
@@ -29,5 +29,5 @@ namespace PWL = Parma_Watchdog_Library;
 
 bool
 PWL::Time::OK() const {
-  return microsecs < 1000000;
+  return microsecs < MUSECS_IN_SEC;
 }
diff --git a/Watchdog/src/Time.defs.hh b/Watchdog/src/Time.defs.hh
index 003d07c..3fd73fc 100644
--- a/Watchdog/src/Time.defs.hh
+++ b/Watchdog/src/Time.defs.hh
@@ -100,6 +100,12 @@ public:
   bool OK() const;
 
 private:
+  //! Number of microseconds in a second.
+  static const unsigned long MUSECS_IN_SEC = 1000000UL;
+
+  //! Number of hundredths of a second in a second.
+  static const unsigned long HSECS_IN_SEC = 100UL;
+
   //! Number of seconds.
   unsigned long secs;
 
diff --git a/Watchdog/src/Time.inlines.hh b/Watchdog/src/Time.inlines.hh
index cfcc814..7b005ac 100644
--- a/Watchdog/src/Time.inlines.hh
+++ b/Watchdog/src/Time.inlines.hh
@@ -36,8 +36,9 @@ Time::Time()
 
 inline
 Time::Time(unsigned long hundredths_of_a_second)
-  : secs(hundredths_of_a_second / 100),
-    microsecs((hundredths_of_a_second % 100) * 10000) {
+  : secs(hundredths_of_a_second / HSECS_IN_SEC),
+    microsecs((hundredths_of_a_second % HSECS_IN_SEC)
+              * (MUSECS_IN_SEC/HSECS_IN_SEC)) {
   assert(OK());
 }
 
@@ -45,9 +46,9 @@ inline
 Time::Time(unsigned long s, unsigned long m)
   : secs(s),
     microsecs(m) {
-  if (microsecs >= 1000000) {
-    secs += microsecs / 1000000;
-    microsecs %= 1000000;
+  if (microsecs >= MUSECS_IN_SEC) {
+    secs += microsecs / MUSECS_IN_SEC;
+    microsecs %= MUSECS_IN_SEC;
   }
   assert(OK());
 }
@@ -66,9 +67,9 @@ inline Time&
 Time::operator+=(const Time& y) {
   unsigned long r_secs = secs + y.secs;
   unsigned long r_microsecs = microsecs + y.microsecs;
-  if (r_microsecs >= 1000000) {
+  if (r_microsecs >= MUSECS_IN_SEC) {
     ++r_secs;
-    r_microsecs %= 1000000;
+    r_microsecs %= MUSECS_IN_SEC;
   }
   secs = r_secs;
   microsecs = r_microsecs;
@@ -82,7 +83,7 @@ Time::operator-=(const Time& y) {
   long r_microsecs = microsecs - y.microsecs;
   if (r_microsecs < 0) {
     --r_secs;
-    r_microsecs += 1000000;
+    r_microsecs += MUSECS_IN_SEC;
   }
   if (r_secs < 0)
     r_secs = r_microsecs = 0;




More information about the PPL-devel mailing list