blob: 4400f050ed741682be09d85ba974c55e26b771f6 [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_REPETITIVE_INTERVAL_HPP
23#define NDN_NAC_REPETITIVE_INTERVAL_HPP
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070024
25#include "common.hpp"
26#include "interval.hpp"
27
28namespace ndn {
Alexander Afanasyev9091d832018-04-18 17:21:08 -040029namespace nac {
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070030
31///@brief An advanced interval which can have a repeat pattern and repeat unit
32class RepetitiveInterval
33{
34public:
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070035 enum class
36 RepeatUnit {
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070037 NONE = 0,
38 DAY = 1,
39 MONTH = 2,
40 YEAR = 3
41 };
42
43public:
44 RepetitiveInterval();
45
46 explicit
47 RepetitiveInterval(const Block& block);
48
49 /**
50 * @brief Construction to create an object
51 * @pre @p startDate <= @p endDate
52 * @pre @p intervalStartHour and @p intervalEndHour can be [0, 24]
53 * @pre @p intervalStartHour < @p intervalEndHour
54 * @pre when @p unit = NONE, then @p startDate == @p endDate
55 */
56 RepetitiveInterval(const TimeStamp& startDate,
57 const TimeStamp& endDate,
58 size_t intervalStartHour,
59 size_t intervalEndHour,
60 size_t nRepeats = 0,
61 RepeatUnit unit = RepeatUnit::NONE);
62
63 template<encoding::Tag TAG>
64 size_t
65 wireEncode(EncodingImpl<TAG>& encoder) const;
66
67 const Block&
68 wireEncode() const;
69
70 void
71 wireDecode(const Block& wire);
72
73 /**
74 * @brief Get get an interval that @p tp falls in
75 *
Alexander Afanasyev9d7f8fe2016-08-05 11:28:06 -070076 * @param tp A timestamp
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070077 *
78 * @return bool If the repetitive interval covers the @p tp, return true, otherwise false
Alexander Afanasyev9d7f8fe2016-08-05 11:28:06 -070079 * @return Interval Return the interval which @p tp falls in
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070080 */
81 std::tuple<bool, Interval>
82 getInterval(const TimeStamp& tp) const;
83
84 /**
85 * @brief To store in std::set, class have to implement operator <
86 *
Alexander Afanasyev9d7f8fe2016-08-05 11:28:06 -070087 * @param interval Interval which will be compared with
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070088 */
89 bool
90 operator<(const RepetitiveInterval& interval) const;
91
92 const TimeStamp&
93 getStartDate() const
94 {
95 return m_startDate;
96 }
97
98 const TimeStamp&
99 getEndDate() const
100 {
101 return m_endDate;
102 }
103
104 size_t
105 getIntervalStartHour() const
106 {
107 return m_intervalStartHour;
108 }
109
110 size_t
111 getIntervalEndHour() const
112 {
113 return m_intervalEndHour;
114 }
115
116 size_t
117 getNRepeats() const
118 {
119 return m_nRepeats;
120 }
121
122 RepeatUnit
123 getRepeatUnit() const
124 {
125 return m_unit;
126 }
127
128private:
129 ///@brief Check if there is any interval in the date of timestamp
130 bool
131 hasIntervalOnDate(const TimeStamp& tp) const;
132
133 TimeStamp m_startDate;
134 TimeStamp m_endDate;
135 size_t m_intervalStartHour;
136 size_t m_intervalEndHour;
137 size_t m_nRepeats;
138 RepeatUnit m_unit;
139
140 mutable Block m_wire;
141};
142
Alexander Afanasyev9091d832018-04-18 17:21:08 -0400143} // namespace nac
Zhiyi Zhangcea58d52015-08-26 10:19:56 -0700144} // namespace ndn
145
Alexander Afanasyev9091d832018-04-18 17:21:08 -0400146#endif // NDN_NAC_REPETITIVE_INTERVAL_HPP