blob: 57644541b2be38cca55bcc7da39dd0dedefdd27d [file] [log] [blame]
Zhiyi Zhangcea58d52015-08-26 10:19:56 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2015, Regents of the University of California
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 *
19 * @author Zhiyi Zhang <dreamerbarrychang@gmail.com>
20 */
21
22#ifndef NDN_GEP_INTERVAL_HPP
23#define NDN_GEP_INTERVAL_HPP
24
25#include "common.hpp"
26
27#include <boost/date_time/posix_time/posix_time.hpp>
28
29namespace ndn {
30namespace gep {
31
32typedef boost::posix_time::ptime TimeStamp;
33
34///@brief Interval define a time duration which contains a start timestamp and an end timestamp
35class Interval
36{
37public:
38 class Error : public std::runtime_error
39 {
40 public:
41 explicit
42 Error(const std::string& what)
43 : std::runtime_error(what)
44 {
45 }
46 };
47
48public:
49 /**
50 * @brief Construction to create an object
51 *
52 * @parameter isValid If isValid is true, the created interval is an empty interval
53 */
54 explicit
55 Interval(bool isValid = false);
56
57 Interval(const TimeStamp& startTime,
58 const TimeStamp& endTime);
59
60 /**
61 * @brief Check if the timestamp tp is in the interval
62 * @pre this->isValid() == true
63 *
64 * @parameter tp A timestamp
65 */
66 bool
67 covers(const TimeStamp& tp) const;
68
69 /**
70 * @brief Get the intersection interval of two intervals
71 * @pre this->isValid() == true && interval.isValid() == true
72 *
73 * Two intervals should all be valid but they can be empty
74 */
75 Interval&
76 operator &&(const Interval& interval);
77
78 /**
79 * @brief Get the union set interval of two intervals
80 * @pre this->isValid() == true && interval.isValid() == true
81 *
82 * Two intervals should all be valid but they can be empty
83 */
84 Interval&
85 operator ||(const Interval& interval);
86
87 const TimeStamp&
88 getStartTime() const
89 {
90 BOOST_ASSERT(isValid());
91 return m_startTime;
92 }
93
94 const TimeStamp&
95 getEndTime() const
96 {
97 BOOST_ASSERT(isValid());
98 return m_endTime;
99 }
100
101 bool
102 isValid() const
103 {
104 return m_isValid;
105 }
106
107 bool
108 isEmpty() const
109 {
110 BOOST_ASSERT(isValid());
111 return m_startTime == m_endTime;
112 }
113
114private:
115 TimeStamp m_startTime;
116 TimeStamp m_endTime;
117
118 bool m_isValid;
119};
120
121} // namespace gep
122} // namespace ndn
123
124#endif // NDN_GEP_INTERVAL_HPP