Time_Helper.inl

Go to the documentation of this file.
00001 /*
00002  *
00003  *
00004  * Distributed under the OpenDDS License.
00005  * See: http://www.opendds.org/license.html
00006  */
00007 
00008 #include "ace/OS_NS_string.h"
00009 #include "ace/Truncate.h"
00010 
00011 #include <cstring>
00012 
00013 OPENDDS_BEGIN_VERSIONED_NAMESPACE_DECL
00014 
00015 namespace OpenDDS {
00016 namespace DCPS {
00017 
00018 // These operators are used in some inline functions below.  Some
00019 // compilers require the inline definition to appear before its use.
00020 #ifndef OPENDDS_SAFETY_PROFILE
00021 ACE_INLINE
00022 bool operator==(const DDS::Duration_t& t1, const DDS::Duration_t& t2)
00023 {
00024   return t1.sec == t2.sec && t1.nanosec == t2.nanosec;
00025 }
00026 
00027 ACE_INLINE
00028 bool operator!=(const DDS::Duration_t& t1, const DDS::Duration_t& t2)
00029 {
00030   return !(t1 == t2);
00031 }
00032 #endif
00033 
00034 ACE_INLINE
00035 bool operator<(const DDS::Duration_t& t1, const DDS::Duration_t& t2)
00036 {
00037   // @note We wouldn't have to handle the case for INFINITY explicitly
00038   //       if both the Duration_t sec and nanosec fields were the
00039   //       maximum values for their corresponding types.
00040   //       Unfortunately, the OMG DDS specification defines the
00041   //       infinite nanosec value to be somewhere in the middle.
00042   DDS::Duration_t const DDS_DURATION_INFINITY = {
00043     DDS::DURATION_INFINITE_SEC,
00044     DDS::DURATION_INFINITE_NSEC
00045   };
00046 
00047   // We assume that either both the DDS::Duration_t::sec and
00048   // DDS::Duration_t::nanosec fields are INFINITY or neither of them
00049   // are.  It doesn't make sense for only one of the fields to be
00050   // INFINITY.
00051   return
00052     t1 != DDS_DURATION_INFINITY
00053     && (t2 == DDS_DURATION_INFINITY
00054         || t1.sec < t2.sec
00055         || (t1.sec == t2.sec && t1.nanosec < t2.nanosec));
00056 }
00057 
00058 ACE_INLINE
00059 bool operator<=(const DDS::Duration_t& t1, const DDS::Duration_t& t2)
00060 {
00061   // If t2 is *not* less than t1, t1 must be less than
00062   // or equal to t2.
00063   // This is more concise than:
00064   //   t1 < t2 || t1 == t2
00065   return !(t2 < t1);
00066 }
00067 
00068 ACE_INLINE
00069 bool operator>(const DDS::Duration_t& t1, const DDS::Duration_t& t2)
00070 {
00071   return t2 < t1;
00072 }
00073 
00074 ACE_INLINE
00075 bool operator>=(const DDS::Duration_t& t1, const DDS::Duration_t& t2)
00076 {
00077   return t2 <= t1;
00078 }
00079 
00080 ACE_INLINE
00081 bool operator!(const DDS::Time_t& t)
00082 {
00083   return t.sec == DDS::TIME_INVALID_SEC
00084          || t.nanosec == DDS::TIME_INVALID_NSEC;
00085 }
00086 
00087 #ifndef OPENDDS_SAFETY_PROFILE
00088 ACE_INLINE bool
00089 operator==(const DDS::Time_t& t1, const DDS::Time_t& t2)
00090 {
00091   return !(t1 < t2) && !(t2 < t1);
00092 }
00093 
00094 ACE_INLINE bool
00095 operator!=(const DDS::Time_t& t1, const DDS::Time_t& t2)
00096 {
00097   return !(t1 == t2);
00098 }
00099 #endif
00100 
00101 ACE_INLINE bool
00102 operator<(const DDS::Time_t& t1, const DDS::Time_t& t2)
00103 {
00104   if (!t1 || !t2) return false;
00105 
00106   return t1.sec < t2.sec
00107          || (t1.sec == t2.sec && t1.nanosec < t2.nanosec);
00108 }
00109 
00110 ACE_INLINE bool
00111 operator<=(const DDS::Time_t& t1, const DDS::Time_t& t2)
00112 {
00113   return !(t2 < t1);
00114 }
00115 
00116 ACE_INLINE bool
00117 operator>(const DDS::Time_t& t1, const DDS::Time_t& t2)
00118 {
00119   return t2 < t1;
00120 }
00121 
00122 ACE_INLINE bool
00123 operator>=(const DDS::Time_t& t1, const DDS::Time_t& t2)
00124 {
00125   return t2 <= t1;
00126 }
00127 
00128 ACE_INLINE DDS::Time_t
00129 operator-(const DDS::Time_t& t1, const DDS::Time_t& t2)
00130 {
00131   DDS::Time_t t = { t1.sec - t2.sec, t1.nanosec - t2.nanosec };
00132 
00133   if (t2.nanosec > t1.nanosec)
00134     {
00135       t.nanosec = (t1.nanosec + ACE_ONE_SECOND_IN_NSECS) - t2.nanosec;
00136       t.sec = (t1.sec - 1) - t2.sec;
00137     }
00138 
00139   return t;
00140 }
00141 
00142 ACE_INLINE
00143 ACE_Time_Value time_to_time_value(const DDS::Time_t& t)
00144 {
00145   ACE_Time_Value tv(t.sec, t.nanosec / 1000);
00146   return tv;
00147 }
00148 
00149 ACE_INLINE
00150 DDS::Time_t time_value_to_time(const ACE_Time_Value& tv)
00151 {
00152   DDS::Time_t t;
00153   t.sec = ACE_Utils::truncate_cast<CORBA::Long>(tv.sec());
00154   t.nanosec = tv.usec() * 1000;
00155   return t;
00156 }
00157 
00158 
00159 ACE_INLINE
00160 ACE_Time_Value duration_to_time_value(const DDS::Duration_t& t)
00161 {
00162   CORBA::LongLong sec = t.sec + t.nanosec/1000/ACE_ONE_SECOND_IN_USECS;
00163   CORBA::ULong usec = t.nanosec/1000 % ACE_ONE_SECOND_IN_USECS;
00164 
00165   if (sec > ACE_Time_Value::max_time.sec()) {
00166     return ACE_Time_Value::max_time;
00167   }
00168   else {
00169     return ACE_Time_Value(ACE_Utils::truncate_cast<time_t>(sec), usec);
00170   }
00171 }
00172 
00173 
00174 ACE_INLINE
00175 ACE_Time_Value duration_to_absolute_time_value(const DDS::Duration_t& t,
00176                                                const ACE_Time_Value& now)
00177 {
00178   CORBA::LongLong sec
00179     = t.sec + now.sec() + (t.nanosec/1000 + now.usec())/ACE_ONE_SECOND_IN_USECS;
00180   CORBA::ULong usec = (t.nanosec/1000 + now.usec()) % ACE_ONE_SECOND_IN_USECS;
00181 
00182   if (sec > ACE_Time_Value::max_time.sec()) {
00183     return ACE_Time_Value::max_time;
00184   }
00185   else {
00186     return ACE_Time_Value(ACE_Utils::truncate_cast<time_t>(sec), usec);
00187   }
00188 }
00189 
00190 
00191 ACE_INLINE
00192 DDS::Duration_t time_value_to_duration(const ACE_Time_Value& tv)
00193 {
00194   DDS::Duration_t t;
00195   t.sec = ACE_Utils::truncate_cast<CORBA::Long>(tv.sec());
00196   t.nanosec = tv.usec() * 1000;
00197   return t;
00198 }
00199 
00200 ACE_INLINE
00201 DDS::Duration_t time_to_duration(const DDS::Time_t& t)
00202 {
00203   DDS::Duration_t d = { t.sec, t.nanosec };
00204   return d;
00205 }
00206 
00207 ACE_INLINE
00208 bool valid_duration(const DDS::Duration_t& t)
00209 {
00210   DDS::Duration_t const DDS_DURATION_INFINITY = {
00211     DDS::DURATION_INFINITE_SEC,
00212     DDS::DURATION_INFINITE_NSEC
00213   };
00214 
00215   // Only accept infinite or positive finite durations.  (Zero
00216   // excluded).
00217   //
00218   // Note that it doesn't make much sense for users to set
00219   // durations less than 10 milliseconds since the underlying
00220   // timer resolution is generally no better than that.
00221   return
00222     t == DDS_DURATION_INFINITY
00223     || t.sec > 0
00224     || (t.sec >= 0 && t.nanosec > 0);
00225 }
00226 
00227 ACE_INLINE
00228 bool non_negative_duration(const DDS::Duration_t& t)
00229 {
00230   return
00231     (t.sec == DDS::DURATION_ZERO_SEC  // Allow zero duration.
00232      && t.nanosec == DDS::DURATION_ZERO_NSEC)
00233     || valid_duration(t);
00234 }
00235 
00236 } // namespace DCPS
00237 } // namespace OpenDDS
00238 
00239 OPENDDS_END_VERSIONED_NAMESPACE_DECL
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 10 Aug 2018 for OpenDDS by  doxygen 1.6.1