blob: d44225201a1cd7a65082030111b229de9bc4eabe [file] [log] [blame]
Zhiyi Zhangcea58d52015-08-26 10:19:56 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Zhiyi Zhang19a11d22018-04-12 22:58:20 -07003 * Copyright (c) 2014-2018, Regents of the University of California
Zhiyi Zhangcea58d52015-08-26 10:19:56 -07004 *
Alexander Afanasyev9091d832018-04-18 17:21:08 -04005 * 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 Zhangcea58d52015-08-26 10:19:56 -07007 *
Alexander Afanasyev9091d832018-04-18 17:21:08 -04008 * NAC is free software: you can redistribute it and/or modify it under the terms
Zhiyi Zhangcea58d52015-08-26 10:19:56 -07009 * 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 Afanasyev9091d832018-04-18 17:21:08 -040012 * NAC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070013 * 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 Afanasyev9091d832018-04-18 17:21:08 -040017 * NAC, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070018 *
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070019 * @author Zhiyi Zhang <zhiyi@cs.ucla.edu>
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070020 */
21
Alexander Afanasyev9091d832018-04-18 17:21:08 -040022#ifndef NDN_NAC_INTERVAL_HPP
23#define NDN_NAC_INTERVAL_HPP
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070024
25#include "common.hpp"
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070026#include <boost/date_time/posix_time/posix_time.hpp>
27
28namespace ndn {
Alexander Afanasyev9091d832018-04-18 17:21:08 -040029namespace nac {
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070030
31typedef boost::posix_time::ptime TimeStamp;
32
33///@brief Interval define a time duration which contains a start timestamp and an end timestamp
34class Interval
35{
36public:
37 class Error : public std::runtime_error
38 {
39 public:
Alexander Afanasyev9091d832018-04-18 17:21:08 -040040 using std::runtime_error::runtime_error;
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070041 };
42
43public:
44 /**
45 * @brief Construction to create an object
46 *
Alexander Afanasyev9d7f8fe2016-08-05 11:28:06 -070047 * @param isValid If isValid is true, the created interval is an empty interval
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070048 */
Alexander Afanasyev9091d832018-04-18 17:21:08 -040049 explicit
50 Interval(bool isValid = false);
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070051
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070052 Interval(const TimeStamp& startTime, const TimeStamp& endTime);
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070053
54 /**
55 * @brief Check if the timestamp tp is in the interval
56 * @pre this->isValid() == true
57 *
Alexander Afanasyev9d7f8fe2016-08-05 11:28:06 -070058 * @param tp A timestamp
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070059 */
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 Zhang19a11d22018-04-12 22:58:20 -070070 operator&&(const Interval& interval);
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070071
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 Zhang19a11d22018-04-12 22:58:20 -070079 operator||(const Interval& interval);
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070080
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
108private:
109 TimeStamp m_startTime;
110 TimeStamp m_endTime;
111
112 bool m_isValid;
113};
114
Alexander Afanasyev9091d832018-04-18 17:21:08 -0400115} // namespace nac
Zhiyi Zhangcea58d52015-08-26 10:19:56 -0700116} // namespace ndn
117
Alexander Afanasyev9091d832018-04-18 17:21:08 -0400118#endif // NDN_NAC_INTERVAL_HPP