Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 10 | * |
| 11 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 12 | * |
| 13 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 14 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 15 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 18 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 19 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 20 | * |
| 21 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 22 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 23 | * <http://www.gnu.org/licenses/>. |
| 24 | * |
| 25 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 26 | */ |
| 27 | |
| 28 | #ifndef NDN_UTIL_CONCEPTS_HPP |
| 29 | #define NDN_UTIL_CONCEPTS_HPP |
| 30 | |
| 31 | #include <boost/concept/usage.hpp> |
Junxiao Shi | a4c5048 | 2014-12-04 12:40:49 -0700 | [diff] [blame] | 32 | #include "../encoding/block.hpp" |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 33 | #include "../encoding/encoding-buffer.hpp" |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 34 | |
| 35 | namespace ndn { |
| 36 | |
Junxiao Shi | a4c5048 | 2014-12-04 12:40:49 -0700 | [diff] [blame] | 37 | /** \brief a concept check for TLV abstraction with .wireEncode method |
| 38 | */ |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 39 | template<class X> |
| 40 | class WireEncodable |
| 41 | { |
| 42 | public: |
| 43 | BOOST_CONCEPT_USAGE(WireEncodable) |
| 44 | { |
Junxiao Shi | a4c5048 | 2014-12-04 12:40:49 -0700 | [diff] [blame] | 45 | Block block = j.wireEncode(); |
| 46 | block.size(); // avoid 'unused variable block' |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 47 | } |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 48 | |
| 49 | private: |
| 50 | X j; |
| 51 | }; |
| 52 | |
| 53 | /** \brief a concept check for TLV abstraction with .wireEncode method |
| 54 | */ |
| 55 | template<class X> |
| 56 | class WireEncodableWithEncodingBuffer |
| 57 | { |
| 58 | public: |
| 59 | BOOST_CONCEPT_USAGE(WireEncodableWithEncodingBuffer) |
| 60 | { |
| 61 | EncodingEstimator estimator; |
| 62 | size_t estimatedSize = j.wireEncode(estimator); |
| 63 | |
| 64 | EncodingBuffer encoder(estimatedSize, 0); |
| 65 | j.wireEncode(encoder); |
| 66 | } |
| 67 | |
| 68 | private: |
| 69 | X j; |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 70 | }; |
| 71 | |
Junxiao Shi | a4c5048 | 2014-12-04 12:40:49 -0700 | [diff] [blame] | 72 | /** \brief a concept check for TLV abstraction with .wireDecode method |
| 73 | * and constructible from Block |
| 74 | */ |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 75 | template<class X> |
| 76 | class WireDecodable |
| 77 | { |
| 78 | public: |
| 79 | BOOST_CONCEPT_USAGE(WireDecodable) |
| 80 | { |
| 81 | Block block; |
| 82 | X j(block); |
| 83 | j.wireDecode(block); |
| 84 | } |
| 85 | }; |
| 86 | |
Junxiao Shi | a4c5048 | 2014-12-04 12:40:49 -0700 | [diff] [blame] | 87 | /** \brief a concept check for CryptoPP hash algorithm |
| 88 | */ |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 89 | template<class X> |
| 90 | class Hashable |
| 91 | { |
| 92 | public: |
| 93 | BOOST_CONCEPT_USAGE(Hashable) |
| 94 | { |
| 95 | X hash; |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 96 | uint8_t* buf = 0; |
| 97 | size_t size = hash.DigestSize(); |
| 98 | |
| 99 | hash.Update(buf, size); |
| 100 | hash.Final(buf); |
| 101 | hash.Restart(); |
| 102 | } |
| 103 | }; |
| 104 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 105 | // NDN_CXX_ASSERT_DEFAULT_CONSTRUCTIBLE and NDN_CXX_ASSERT_FORWARD_ITERATOR |
| 106 | // originally written as part of NFD codebase |
| 107 | |
| 108 | namespace detail { |
| 109 | |
| 110 | // As of Boost 1.54.0, the internal implementation of BOOST_CONCEPT_ASSERT does not allow |
| 111 | // multiple assertions on the same line, so we have to combine multiple concepts together. |
| 112 | |
| 113 | template<typename T> |
| 114 | class StlForwardIteratorConcept : public boost::ForwardIterator<T> |
| 115 | , public boost::DefaultConstructible<T> |
| 116 | { |
| 117 | }; |
| 118 | |
| 119 | } // namespace detail |
| 120 | |
| 121 | /** \brief assert T is default constructible |
| 122 | * \sa http://en.cppreference.com/w/cpp/concept/DefaultConstructible |
| 123 | */ |
| 124 | #define NDN_CXX_ASSERT_DEFAULT_CONSTRUCTIBLE(T) \ |
| 125 | static_assert(std::is_default_constructible<T>::value, \ |
| 126 | #T " must be default-constructible"); \ |
| 127 | BOOST_CONCEPT_ASSERT((boost::DefaultConstructible<T>)) |
| 128 | |
| 129 | /** \brief assert T is a forward iterator |
| 130 | * \sa http://en.cppreference.com/w/cpp/concept/ForwardIterator |
| 131 | * \note A forward iterator should be default constructible, but boost::ForwardIterator follows |
| 132 | * SGI standard which doesn't require DefaultConstructible, so a separate check is needed. |
| 133 | */ |
| 134 | #define NDN_CXX_ASSERT_FORWARD_ITERATOR(T) \ |
| 135 | BOOST_CONCEPT_ASSERT((::ndn::detail::StlForwardIteratorConcept<T>)); \ |
| 136 | static_assert(std::is_default_constructible<T>::value, \ |
| 137 | #T " must be default-constructible") |
| 138 | |
| 139 | |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 140 | } // namespace ndn |
| 141 | |
| 142 | #endif // NDN_UTIL_CONCEPTS_HPP |