concepts.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
28 #ifndef NDN_UTIL_CONCEPTS_HPP
29 #define NDN_UTIL_CONCEPTS_HPP
30 
31 #include <boost/concept/usage.hpp>
32 #include "../encoding/block.hpp"
33 #include "../encoding/encoding-buffer.hpp"
34 
35 namespace ndn {
36 
39 template<class X>
41 {
42 public:
44  {
45  Block block = j.wireEncode();
46  block.size(); // avoid 'unused variable block'
47  }
48 
49 private:
50  X j;
51 };
52 
55 template<class X>
57 {
58 public:
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;
70 };
71 
75 template<class X>
77 {
78 public:
80  {
81  Block block;
82  X j(block);
83  j.wireDecode(block);
84  }
85 };
86 
89 template<class X>
90 class Hashable
91 {
92 public:
94  {
95  X hash;
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 
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 
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 
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 
140 } // namespace ndn
141 
142 #endif // NDN_UTIL_CONCEPTS_HPP
Copyright (c) 2013-2016 Regents of the University of California.
Definition: common.hpp:74
EncodingImpl< EstimatorTag > EncodingEstimator
BOOST_CONCEPT_USAGE(Hashable)
Definition: concepts.hpp:93
BOOST_CONCEPT_USAGE(WireDecodable)
Definition: concepts.hpp:79
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
size_t size() const
Definition: block.cpp:504
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:56
EncodingImpl< EncoderTag > EncodingBuffer
BOOST_CONCEPT_USAGE(WireEncodable)
Definition: concepts.hpp:43
BOOST_CONCEPT_USAGE(WireEncodableWithEncodingBuffer)
Definition: concepts.hpp:59
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:40
a concept check for TLV abstraction with .wireDecode method and constructible from Block ...
Definition: concepts.hpp:76
a concept check for CryptoPP hash algorithm
Definition: concepts.hpp:90