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 | * |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame^] | 5 | * This file is part of NAC (Name-Based Access Control for NDN). |
| 6 | * See AUTHORS.md for complete list of NAC authors and contributors. |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 7 | * |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame^] | 8 | * NAC is free software: you can redistribute it and/or modify it under the terms |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 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 | * |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame^] | 12 | * NAC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 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 |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame^] | 17 | * NAC, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 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 | |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame^] | 22 | #ifndef NDN_NAC_INTERVAL_HPP |
| 23 | #define NDN_NAC_INTERVAL_HPP |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 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 { |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame^] | 29 | namespace nac { |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 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: |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame^] | 40 | using std::runtime_error::runtime_error; |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | public: |
| 44 | /** |
| 45 | * @brief Construction to create an object |
| 46 | * |
Alexander Afanasyev | 9d7f8fe | 2016-08-05 11:28:06 -0700 | [diff] [blame] | 47 | * @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] | 48 | */ |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame^] | 49 | explicit |
| 50 | Interval(bool isValid = false); |
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 | Interval(const TimeStamp& startTime, const TimeStamp& endTime); |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 53 | |
| 54 | /** |
| 55 | * @brief Check if the timestamp tp is in the interval |
| 56 | * @pre this->isValid() == true |
| 57 | * |
Alexander Afanasyev | 9d7f8fe | 2016-08-05 11:28:06 -0700 | [diff] [blame] | 58 | * @param tp A timestamp |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 59 | */ |
| 60 | bool |
| 61 | covers(const TimeStamp& tp) const; |
| 62 | |
| 63 | /** |
| 64 | * @brief Get the intersection interval of two intervals |
| 65 | * @pre this->isValid() == true && interval.isValid() == true |
| 66 | * |
| 67 | * Two intervals should all be valid but they can be empty |
| 68 | */ |
| 69 | Interval& |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 70 | operator&&(const Interval& interval); |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 71 | |
| 72 | /** |
| 73 | * @brief Get the union set interval of two intervals |
| 74 | * @pre this->isValid() == true && interval.isValid() == true |
| 75 | * |
| 76 | * Two intervals should all be valid but they can be empty |
| 77 | */ |
| 78 | Interval& |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 79 | operator||(const Interval& interval); |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 80 | |
| 81 | const TimeStamp& |
| 82 | getStartTime() const |
| 83 | { |
| 84 | BOOST_ASSERT(isValid()); |
| 85 | return m_startTime; |
| 86 | } |
| 87 | |
| 88 | const TimeStamp& |
| 89 | getEndTime() const |
| 90 | { |
| 91 | BOOST_ASSERT(isValid()); |
| 92 | return m_endTime; |
| 93 | } |
| 94 | |
| 95 | bool |
| 96 | isValid() const |
| 97 | { |
| 98 | return m_isValid; |
| 99 | } |
| 100 | |
| 101 | bool |
| 102 | isEmpty() const |
| 103 | { |
| 104 | BOOST_ASSERT(isValid()); |
| 105 | return m_startTime == m_endTime; |
| 106 | } |
| 107 | |
| 108 | private: |
| 109 | TimeStamp m_startTime; |
| 110 | TimeStamp m_endTime; |
| 111 | |
| 112 | bool m_isValid; |
| 113 | }; |
| 114 | |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame^] | 115 | } // namespace nac |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 116 | } // namespace ndn |
| 117 | |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame^] | 118 | #endif // NDN_NAC_INTERVAL_HPP |