blob: 6cf6b8646ba027e042c5d5570cc2a5135f4469d5 [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 *
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 Zhang19a11d22018-04-12 22:58:20 -070019 * @author Zhiyi Zhang <zhiyi@cs.ucla.edu>
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070020 */
21
22#ifndef NDN_GEP_INTERVAL_HPP
23#define NDN_GEP_INTERVAL_HPP
24
25#include "common.hpp"
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070026#include <boost/date_time/posix_time/posix_time.hpp>
27
28namespace ndn {
29namespace gep {
30
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:
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070040 explicit Error(const std::string& what)
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070041 : std::runtime_error(what)
42 {
43 }
44 };
45
46public:
47 /**
48 * @brief Construction to create an object
49 *
Alexander Afanasyev9d7f8fe2016-08-05 11:28:06 -070050 * @param isValid If isValid is true, the created interval is an empty interval
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070051 */
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070052 explicit Interval(bool isValid = false);
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070053
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070054 Interval(const TimeStamp& startTime, const TimeStamp& endTime);
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070055
56 /**
57 * @brief Check if the timestamp tp is in the interval
58 * @pre this->isValid() == true
59 *
Alexander Afanasyev9d7f8fe2016-08-05 11:28:06 -070060 * @param tp A timestamp
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070061 */
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 Zhang19a11d22018-04-12 22:58:20 -070072 operator&&(const Interval& interval);
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070073
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 Zhang19a11d22018-04-12 22:58:20 -070081 operator||(const Interval& interval);
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070082
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
110private:
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