blob: 05c0ffe13ffbed8952ccf06bfaf32586f663c903 [file] [log] [blame]
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesaventob5f8bcc2017-02-05 17:58:05 -05003 * 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 Afanasyev4abdbf12014-08-11 12:48:54 -070010 *
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 Shia4c50482014-12-04 12:40:49 -070032#include "../encoding/block.hpp"
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070033#include "../encoding/encoding-buffer.hpp"
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -070034
35namespace ndn {
36
Junxiao Shia4c50482014-12-04 12:40:49 -070037/** \brief a concept check for TLV abstraction with .wireEncode method
38 */
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -070039template<class X>
40class WireEncodable
41{
42public:
43 BOOST_CONCEPT_USAGE(WireEncodable)
44 {
Junxiao Shia4c50482014-12-04 12:40:49 -070045 Block block = j.wireEncode();
46 block.size(); // avoid 'unused variable block'
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -070047 }
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070048
49private:
50 X j;
51};
52
53/** \brief a concept check for TLV abstraction with .wireEncode method
54 */
55template<class X>
56class WireEncodableWithEncodingBuffer
57{
58public:
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
68private:
69 X j;
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -070070};
71
Junxiao Shia4c50482014-12-04 12:40:49 -070072/** \brief a concept check for TLV abstraction with .wireDecode method
73 * and constructible from Block
74 */
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -070075template<class X>
76class WireDecodable
77{
78public:
79 BOOST_CONCEPT_USAGE(WireDecodable)
80 {
81 Block block;
82 X j(block);
83 j.wireDecode(block);
84 }
85};
86
Junxiao Shia4c50482014-12-04 12:40:49 -070087/** \brief a concept check for CryptoPP hash algorithm
88 */
Yingdi Yude222c72014-08-15 16:06:52 -070089template<class X>
90class Hashable
91{
92public:
93 BOOST_CONCEPT_USAGE(Hashable)
94 {
95 X hash;
Yingdi Yude222c72014-08-15 16:06:52 -070096 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 Yucbe72b02015-11-25 17:35:37 -0800105// NDN_CXX_ASSERT_DEFAULT_CONSTRUCTIBLE and NDN_CXX_ASSERT_FORWARD_ITERATOR
106// originally written as part of NFD codebase
107
108namespace 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
113template<typename T>
114class 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 Afanasyev4abdbf12014-08-11 12:48:54 -0700140} // namespace ndn
141
142#endif // NDN_UTIL_CONCEPTS_HPP