Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2014-2018, Regents of the University of California |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-group-encrypt (Group-based Encryption Protocol for NDN). |
| 6 | * See AUTHORS.md for complete list of ndn-group-encrypt authors and contributors. |
| 7 | * |
| 8 | * ndn-group-encrypt is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * ndn-group-encrypt is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * ndn-group-encrypt, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame^] | 19 | * @author Zhiyi Zhang <zhiyi@cs.ucla.edu> |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #ifndef NDN_GEP_INTERVAL_HPP |
| 23 | #define NDN_GEP_INTERVAL_HPP |
| 24 | |
| 25 | #include "common.hpp" |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 26 | #include <boost/date_time/posix_time/posix_time.hpp> |
| 27 | |
| 28 | namespace ndn { |
| 29 | namespace gep { |
| 30 | |
| 31 | typedef boost::posix_time::ptime TimeStamp; |
| 32 | |
| 33 | ///@brief Interval define a time duration which contains a start timestamp and an end timestamp |
| 34 | class Interval |
| 35 | { |
| 36 | public: |
| 37 | class Error : public std::runtime_error |
| 38 | { |
| 39 | public: |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame^] | 40 | explicit Error(const std::string& what) |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 41 | : std::runtime_error(what) |
| 42 | { |
| 43 | } |
| 44 | }; |
| 45 | |
| 46 | public: |
| 47 | /** |
| 48 | * @brief Construction to create an object |
| 49 | * |
Alexander Afanasyev | 9d7f8fe | 2016-08-05 11:28:06 -0700 | [diff] [blame] | 50 | * @param isValid If isValid is true, the created interval is an empty interval |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 51 | */ |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame^] | 52 | explicit Interval(bool isValid = false); |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 53 | |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame^] | 54 | Interval(const TimeStamp& startTime, const TimeStamp& endTime); |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 55 | |
| 56 | /** |
| 57 | * @brief Check if the timestamp tp is in the interval |
| 58 | * @pre this->isValid() == true |
| 59 | * |
Alexander Afanasyev | 9d7f8fe | 2016-08-05 11:28:06 -0700 | [diff] [blame] | 60 | * @param tp A timestamp |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 61 | */ |
| 62 | bool |
| 63 | covers(const TimeStamp& tp) const; |
| 64 | |
| 65 | /** |
| 66 | * @brief Get the intersection interval of two intervals |
| 67 | * @pre this->isValid() == true && interval.isValid() == true |
| 68 | * |
| 69 | * Two intervals should all be valid but they can be empty |
| 70 | */ |
| 71 | Interval& |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame^] | 72 | operator&&(const Interval& interval); |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 73 | |
| 74 | /** |
| 75 | * @brief Get the union set interval of two intervals |
| 76 | * @pre this->isValid() == true && interval.isValid() == true |
| 77 | * |
| 78 | * Two intervals should all be valid but they can be empty |
| 79 | */ |
| 80 | Interval& |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame^] | 81 | operator||(const Interval& interval); |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 82 | |
| 83 | const TimeStamp& |
| 84 | getStartTime() const |
| 85 | { |
| 86 | BOOST_ASSERT(isValid()); |
| 87 | return m_startTime; |
| 88 | } |
| 89 | |
| 90 | const TimeStamp& |
| 91 | getEndTime() const |
| 92 | { |
| 93 | BOOST_ASSERT(isValid()); |
| 94 | return m_endTime; |
| 95 | } |
| 96 | |
| 97 | bool |
| 98 | isValid() const |
| 99 | { |
| 100 | return m_isValid; |
| 101 | } |
| 102 | |
| 103 | bool |
| 104 | isEmpty() const |
| 105 | { |
| 106 | BOOST_ASSERT(isValid()); |
| 107 | return m_startTime == m_endTime; |
| 108 | } |
| 109 | |
| 110 | private: |
| 111 | TimeStamp m_startTime; |
| 112 | TimeStamp m_endTime; |
| 113 | |
| 114 | bool m_isValid; |
| 115 | }; |
| 116 | |
| 117 | } // namespace gep |
| 118 | } // namespace ndn |
| 119 | |
| 120 | #endif // NDN_GEP_INTERVAL_HPP |