| 1 | /* |
| 2 | * Copyright (C) 2007, 2009, 2010 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include "TimeRanges.h" |
| 28 | |
| 29 | namespace WebCore { |
| 30 | |
| 31 | Ref<TimeRanges> TimeRanges::create() |
| 32 | { |
| 33 | return adoptRef(*new TimeRanges); |
| 34 | } |
| 35 | |
| 36 | Ref<TimeRanges> TimeRanges::create(double start, double end) |
| 37 | { |
| 38 | return adoptRef(*new TimeRanges(start, end)); |
| 39 | } |
| 40 | |
| 41 | Ref<TimeRanges> TimeRanges::create(const PlatformTimeRanges& other) |
| 42 | { |
| 43 | return adoptRef(*new TimeRanges(other)); |
| 44 | } |
| 45 | |
| 46 | TimeRanges::TimeRanges() |
| 47 | { |
| 48 | } |
| 49 | |
| 50 | TimeRanges::TimeRanges(double start, double end) |
| 51 | : m_ranges(PlatformTimeRanges(MediaTime::createWithDouble(start), MediaTime::createWithDouble(end))) |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | TimeRanges::TimeRanges(const PlatformTimeRanges& other) |
| 56 | : m_ranges(other) |
| 57 | { |
| 58 | } |
| 59 | |
| 60 | ExceptionOr<double> TimeRanges::start(unsigned index) const |
| 61 | { |
| 62 | bool valid; |
| 63 | MediaTime result = m_ranges.start(index, valid); |
| 64 | if (!valid) |
| 65 | return Exception { IndexSizeError }; |
| 66 | return result.toDouble(); |
| 67 | } |
| 68 | |
| 69 | ExceptionOr<double> TimeRanges::end(unsigned index) const |
| 70 | { |
| 71 | bool valid; |
| 72 | MediaTime result = m_ranges.end(index, valid); |
| 73 | if (!valid) |
| 74 | return Exception { IndexSizeError }; |
| 75 | return result.toDouble(); |
| 76 | } |
| 77 | |
| 78 | void TimeRanges::invert() |
| 79 | { |
| 80 | m_ranges.invert(); |
| 81 | } |
| 82 | |
| 83 | Ref<TimeRanges> TimeRanges::copy() const |
| 84 | { |
| 85 | return TimeRanges::create(m_ranges); |
| 86 | } |
| 87 | |
| 88 | void TimeRanges::intersectWith(const TimeRanges& other) |
| 89 | { |
| 90 | m_ranges.intersectWith(other.ranges()); |
| 91 | } |
| 92 | |
| 93 | void TimeRanges::unionWith(const TimeRanges& other) |
| 94 | { |
| 95 | m_ranges.unionWith(other.ranges()); |
| 96 | } |
| 97 | |
| 98 | unsigned TimeRanges::length() const |
| 99 | { |
| 100 | return m_ranges.length(); |
| 101 | } |
| 102 | |
| 103 | void TimeRanges::add(double start, double end) |
| 104 | { |
| 105 | m_ranges.add(MediaTime::createWithDouble(start), MediaTime::createWithDouble(end)); |
| 106 | } |
| 107 | |
| 108 | bool TimeRanges::contain(double time) const |
| 109 | { |
| 110 | return m_ranges.contain(MediaTime::createWithDouble(time)); |
| 111 | } |
| 112 | |
| 113 | size_t TimeRanges::find(double time) const |
| 114 | { |
| 115 | return m_ranges.find(MediaTime::createWithDouble(time)); |
| 116 | } |
| 117 | |
| 118 | double TimeRanges::nearest(double time) const |
| 119 | { |
| 120 | return m_ranges.nearest(MediaTime::createWithDouble(time)).toDouble(); |
| 121 | } |
| 122 | |
| 123 | double TimeRanges::totalDuration() const |
| 124 | { |
| 125 | return m_ranges.totalDuration().toDouble(); |
| 126 | } |
| 127 | |
| 128 | } |
| 129 | |