src/duration.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2010, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage, Inc. nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34  #include <ros/duration.h>
35 #include <ros/impl/duration.h>
36 
37 namespace ros {
38 
39  Duration::Duration(const Rate& rate)
40  : DurationBase<Duration>(rate.expectedCycleTime().sec, rate.expectedCycleTime().nsec)
41  { }
42 
44  : DurationBase<WallDuration>(rate.expectedCycleTime().sec, rate.expectedCycleTime().nsec)
45  { }
46 
47  void normalizeSecNSecSigned(int64_t& sec, int64_t& nsec)
48  {
49  int64_t nsec_part = nsec % 1000000000L;
50  int64_t sec_part = sec + nsec / 1000000000L;
51  if (nsec_part < 0)
52  {
53  nsec_part += 1000000000L;
54  --sec_part;
55  }
56 
57  if (sec_part < std::numeric_limits<int32_t>::min() || sec_part > std::numeric_limits<int32_t>::max())
58  throw std::runtime_error("Duration is out of dual 32-bit range");
59 
60  sec = sec_part;
61  nsec = nsec_part;
62  }
63 
64  void normalizeSecNSecSigned(int32_t& sec, int32_t& nsec)
65  {
66  int64_t sec64 = sec;
67  int64_t nsec64 = nsec;
68 
69  normalizeSecNSecSigned(sec64, nsec64);
70 
71  sec = (int32_t)sec64;
72  nsec = (int32_t)nsec64;
73  }
74 
75  template class DurationBase<Duration>;
76  template class DurationBase<WallDuration>;
77 }
78 
ROSTIME_DECL void normalizeSecNSecSigned(int64_t &sec, int64_t &nsec)
Class to help run loops at a desired frequency.
Definition: rate.h:51
Duration representation for use with the WallTime class.
Definition: duration.h:136
Base class for Duration implementations. Provides storage, common functions and operator overloads...
Definition: duration.h:72
Duration representation for use with the Time class.
Definition: duration.h:108


rostime
Author(s): Josh Faust
autogenerated on Sat Apr 6 2019 02:52:21