| 1 | # The MIT License (MIT) |
| 2 | # |
| 3 | # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com> |
| 4 | # |
| 5 | # Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | # of this software and associated documentation files (the "Software"), to deal |
| 7 | # in the Software without restriction, including without limitation the rights |
| 8 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | # copies of the Software, and to permit persons to whom the Software is |
| 10 | # furnished to do so, subject to the following conditions: |
| 11 | # |
| 12 | # The above copyright notice and this permission notice shall be included in |
| 13 | # all copies or substantial portions of the Software. |
| 14 | # |
| 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | # THE SOFTWARE. |
| 22 | |
| 23 | from bt2 import native_bt, object, utils |
| 24 | import uuid as uuidp |
| 25 | import numbers |
| 26 | import bt2 |
| 27 | |
| 28 | |
| 29 | def _create_clock_value_from_ptr(ptr): |
| 30 | clock_value = _ClockValue._create_from_ptr(ptr) |
| 31 | return clock_value |
| 32 | |
| 33 | |
| 34 | class _ClockValue(object._Object): |
| 35 | def __init__(self, clock_class_ptr, cycles): |
| 36 | utils._check_uint64(cycles) |
| 37 | ptr = native_bt.clock_value_create(clock_class_ptr, cycles) |
| 38 | |
| 39 | if ptr is None: |
| 40 | raise bt2.CreationError('cannot create clock value object') |
| 41 | |
| 42 | super().__init__(ptr) |
| 43 | |
| 44 | @property |
| 45 | def clock_class(self): |
| 46 | ptr = native_bt.clock_value_get_class(self._ptr) |
| 47 | assert(ptr) |
| 48 | return bt2.ClockClass._create_from_ptr(ptr) |
| 49 | |
| 50 | @property |
| 51 | def cycles(self): |
| 52 | ret, cycles = native_bt.clock_value_get_value(self._ptr) |
| 53 | assert(ret == 0) |
| 54 | return cycles |
| 55 | |
| 56 | @property |
| 57 | def ns_from_epoch(self): |
| 58 | ret, ns = native_bt.clock_value_get_value_ns_from_epoch(self._ptr) |
| 59 | utils._handle_ret(ret, "cannot get clock value object's nanoseconds from Epoch") |
| 60 | return ns |
| 61 | |
| 62 | def __eq__(self, other): |
| 63 | if isinstance(other, numbers.Integral): |
| 64 | return int(other) == self.cycles |
| 65 | |
| 66 | if not isinstance(other, self.__class__): |
| 67 | # not comparing apples to apples |
| 68 | return False |
| 69 | |
| 70 | if self.addr == other.addr: |
| 71 | return True |
| 72 | |
| 73 | self_props = self.clock_class, self.cycles |
| 74 | other_props = other.clock_class, other.cycles |
| 75 | return self_props == other_props |
| 76 | |
| 77 | def __copy__(self): |
| 78 | return self.clock_class(self.cycles) |
| 79 | |
| 80 | def __deepcopy__(self, memo): |
| 81 | cpy = self.__copy__() |
| 82 | memo[id(self)] = cpy |
| 83 | return cpy |