Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 6717fbd | 2017-07-09 18:41:35 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | b5f8bcc | 2017-02-05 17:58:05 -0500 | [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 | |
Junxiao Shi | a4c5048 | 2014-12-04 12:40:49 -0700 | [diff] [blame] | 31 | #include "../encoding/block.hpp" |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 32 | #include "../encoding/encoding-buffer.hpp" |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 33 | |
Davide Pesavento | eba76ad | 2017-02-05 02:48:24 -0500 | [diff] [blame] | 34 | #include <boost/concept/usage.hpp> |
| 35 | #include <boost/type_traits/has_equal_to.hpp> |
| 36 | #include <boost/type_traits/has_not_equal_to.hpp> |
| 37 | #include <boost/type_traits/has_left_shift.hpp> |
| 38 | |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 39 | namespace ndn { |
| 40 | |
Junxiao Shi | a4c5048 | 2014-12-04 12:40:49 -0700 | [diff] [blame] | 41 | /** \brief a concept check for TLV abstraction with .wireEncode method |
| 42 | */ |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 43 | template<class X> |
| 44 | class WireEncodable |
| 45 | { |
| 46 | public: |
| 47 | BOOST_CONCEPT_USAGE(WireEncodable) |
| 48 | { |
Junxiao Shi | a4c5048 | 2014-12-04 12:40:49 -0700 | [diff] [blame] | 49 | Block block = j.wireEncode(); |
| 50 | block.size(); // avoid 'unused variable block' |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 51 | } |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 52 | |
| 53 | private: |
| 54 | X j; |
| 55 | }; |
| 56 | |
| 57 | /** \brief a concept check for TLV abstraction with .wireEncode method |
| 58 | */ |
| 59 | template<class X> |
| 60 | class WireEncodableWithEncodingBuffer |
| 61 | { |
| 62 | public: |
| 63 | BOOST_CONCEPT_USAGE(WireEncodableWithEncodingBuffer) |
| 64 | { |
| 65 | EncodingEstimator estimator; |
| 66 | size_t estimatedSize = j.wireEncode(estimator); |
| 67 | |
| 68 | EncodingBuffer encoder(estimatedSize, 0); |
| 69 | j.wireEncode(encoder); |
| 70 | } |
| 71 | |
| 72 | private: |
| 73 | X j; |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 74 | }; |
| 75 | |
Junxiao Shi | a4c5048 | 2014-12-04 12:40:49 -0700 | [diff] [blame] | 76 | /** \brief a concept check for TLV abstraction with .wireDecode method |
| 77 | * and constructible from Block |
| 78 | */ |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 79 | template<class X> |
| 80 | class WireDecodable |
| 81 | { |
| 82 | public: |
| 83 | BOOST_CONCEPT_USAGE(WireDecodable) |
| 84 | { |
| 85 | Block block; |
| 86 | X j(block); |
| 87 | j.wireDecode(block); |
| 88 | } |
| 89 | }; |
| 90 | |
Davide Pesavento | 88eb748 | 2017-02-18 19:25:58 -0500 | [diff] [blame] | 91 | namespace detail { |
| 92 | |
Davide Pesavento | eba76ad | 2017-02-05 02:48:24 -0500 | [diff] [blame] | 93 | template<class X> |
Davide Pesavento | 88eb748 | 2017-02-18 19:25:58 -0500 | [diff] [blame] | 94 | class NfdMgmtProtocolStruct : public WireEncodable<X> |
| 95 | , public WireEncodableWithEncodingBuffer<X> |
| 96 | , public WireDecodable<X> |
Davide Pesavento | eba76ad | 2017-02-05 02:48:24 -0500 | [diff] [blame] | 97 | { |
| 98 | public: |
Davide Pesavento | 88eb748 | 2017-02-18 19:25:58 -0500 | [diff] [blame] | 99 | BOOST_CONCEPT_USAGE(NfdMgmtProtocolStruct) |
Davide Pesavento | eba76ad | 2017-02-05 02:48:24 -0500 | [diff] [blame] | 100 | { |
| 101 | static_assert(std::is_default_constructible<X>::value, ""); |
| 102 | static_assert(boost::has_equal_to<X, X, bool>::value, ""); |
| 103 | static_assert(boost::has_not_equal_to<X, X, bool>::value, ""); |
| 104 | static_assert(boost::has_left_shift<std::ostream, X, std::ostream&>::value, ""); |
| 105 | static_assert(std::is_base_of<tlv::Error, typename X::Error>::value, ""); |
| 106 | } |
| 107 | }; |
| 108 | |
Davide Pesavento | 88eb748 | 2017-02-18 19:25:58 -0500 | [diff] [blame] | 109 | } // namespace detail |
| 110 | |
| 111 | /** \brief concept check for an item in a Status Dataset |
| 112 | * \sa https://redmine.named-data.net/projects/nfd/wiki/StatusDataset |
| 113 | */ |
| 114 | template<class X> |
| 115 | class StatusDatasetItem : public detail::NfdMgmtProtocolStruct<X> |
| 116 | { |
| 117 | }; |
| 118 | |
| 119 | /** \brief concept check for an item in a Notification Stream |
| 120 | * \sa https://redmine.named-data.net/projects/nfd/wiki/Notification |
| 121 | */ |
| 122 | template<class X> |
| 123 | class NotificationStreamItem : public detail::NfdMgmtProtocolStruct<X> |
| 124 | { |
| 125 | }; |
| 126 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 127 | // NDN_CXX_ASSERT_DEFAULT_CONSTRUCTIBLE and NDN_CXX_ASSERT_FORWARD_ITERATOR |
| 128 | // originally written as part of NFD codebase |
| 129 | |
| 130 | namespace detail { |
| 131 | |
Davide Pesavento | eba76ad | 2017-02-05 02:48:24 -0500 | [diff] [blame] | 132 | // As of Boost 1.61.0, the internal implementation of BOOST_CONCEPT_ASSERT does not allow |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 133 | // multiple assertions on the same line, so we have to combine multiple concepts together. |
| 134 | |
| 135 | template<typename T> |
| 136 | class StlForwardIteratorConcept : public boost::ForwardIterator<T> |
| 137 | , public boost::DefaultConstructible<T> |
| 138 | { |
| 139 | }; |
| 140 | |
| 141 | } // namespace detail |
| 142 | |
Davide Pesavento | eba76ad | 2017-02-05 02:48:24 -0500 | [diff] [blame] | 143 | // std::is_default_constructible is broken in gcc-4.8, see bug #3882 |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 144 | /** \brief assert T is default constructible |
| 145 | * \sa http://en.cppreference.com/w/cpp/concept/DefaultConstructible |
| 146 | */ |
| 147 | #define NDN_CXX_ASSERT_DEFAULT_CONSTRUCTIBLE(T) \ |
| 148 | static_assert(std::is_default_constructible<T>::value, \ |
| 149 | #T " must be default-constructible"); \ |
| 150 | BOOST_CONCEPT_ASSERT((boost::DefaultConstructible<T>)) |
| 151 | |
| 152 | /** \brief assert T is a forward iterator |
| 153 | * \sa http://en.cppreference.com/w/cpp/concept/ForwardIterator |
| 154 | * \note A forward iterator should be default constructible, but boost::ForwardIterator follows |
| 155 | * SGI standard which doesn't require DefaultConstructible, so a separate check is needed. |
| 156 | */ |
| 157 | #define NDN_CXX_ASSERT_FORWARD_ITERATOR(T) \ |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 158 | static_assert(std::is_default_constructible<T>::value, \ |
Davide Pesavento | eba76ad | 2017-02-05 02:48:24 -0500 | [diff] [blame] | 159 | #T " must be default-constructible"); \ |
| 160 | BOOST_CONCEPT_ASSERT((::ndn::detail::StlForwardIteratorConcept<T>)) |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 161 | |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 162 | } // namespace ndn |
| 163 | |
| 164 | #endif // NDN_UTIL_CONCEPTS_HPP |