Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 1 | /* -*- 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 | |
| 12 | namespace nfd { |
| 13 | namespace ndnlp { |
| 14 | |
| 15 | /** \brief represents a block of sequence numbers |
| 16 | */ |
| 17 | class SequenceBlock |
| 18 | { |
| 19 | public: |
| 20 | SequenceBlock(uint64_t start, size_t count); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame^] | 21 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 22 | /** \return{ the pos-th sequence number } |
| 23 | */ |
| 24 | uint64_t |
| 25 | operator[](size_t pos) const; |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame^] | 26 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 27 | size_t |
| 28 | count() const; |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame^] | 29 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 30 | private: |
| 31 | uint64_t m_start; |
| 32 | size_t m_count; |
| 33 | }; |
| 34 | |
| 35 | inline uint64_t |
| 36 | SequenceBlock::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 | |
| 43 | inline size_t |
| 44 | SequenceBlock::count() const |
| 45 | { |
| 46 | return m_count; |
| 47 | } |
| 48 | |
| 49 | /** \class SequenceGenerator |
| 50 | * \brief generates sequence numbers |
| 51 | */ |
| 52 | class SequenceGenerator : noncopyable |
| 53 | { |
| 54 | public: |
| 55 | SequenceGenerator(); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame^] | 56 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 57 | /** \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 | |
| 64 | private: |
| 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 |