blob: 537cac2ef2141c54be5d0aff444e9cbef5326b89 [file] [log] [blame]
Junxiao Shi9b0d3e92014-02-15 12:27:12 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 */
6
7#ifndef NFD_FACE_NDNLP_SEQUENCE_GENERATOR_HPP
8#define NFD_FACE_NDNLP_SEQUENCE_GENERATOR_HPP
9
10#include "common.hpp"
11
12namespace nfd {
13namespace ndnlp {
14
15/** \brief represents a block of sequence numbers
16 */
17class SequenceBlock
18{
19public:
20 SequenceBlock(uint64_t start, size_t count);
Junxiao Shidf3b4382014-02-23 11:28:21 -070021
Junxiao Shi9b0d3e92014-02-15 12:27:12 -070022 /** \return{ the pos-th sequence number }
23 */
24 uint64_t
25 operator[](size_t pos) const;
Junxiao Shidf3b4382014-02-23 11:28:21 -070026
Junxiao Shi9b0d3e92014-02-15 12:27:12 -070027 size_t
28 count() const;
Junxiao Shidf3b4382014-02-23 11:28:21 -070029
Junxiao Shi9b0d3e92014-02-15 12:27:12 -070030private:
31 uint64_t m_start;
32 size_t m_count;
33};
34
35inline uint64_t
36SequenceBlock::operator[](size_t pos) const
37{
38 if (pos >= m_count)
39 throw std::out_of_range("pos");
40 return m_start + static_cast<uint64_t>(pos);
41}
42
43inline size_t
44SequenceBlock::count() const
45{
46 return m_count;
47}
48
49/** \class SequenceGenerator
50 * \brief generates sequence numbers
51 */
52class SequenceGenerator : noncopyable
53{
54public:
55 SequenceGenerator();
Junxiao Shidf3b4382014-02-23 11:28:21 -070056
Junxiao Shi9b0d3e92014-02-15 12:27:12 -070057 /** \brief generates a block of consecutive sequence numbers
58 *
59 * This block must not overlap with a recent block.
60 */
61 SequenceBlock
62 nextBlock(size_t count);
63
64private:
65 /// next sequence number
66 uint64_t m_next;
67};
68
69} // namespace ndnlp
70} // namespace nfd
71
72#endif // NFD_FACE_NDNLP_SEQUENCE_GENERATOR_HPP