lp: Packet and Fields
refs #2879
Change-Id: Ie450eb58d1176179efed8d1706d30ea4978b7b02
diff --git a/src/lp/detail/field-decl.hpp b/src/lp/detail/field-decl.hpp
new file mode 100644
index 0000000..e6fd4aa
--- /dev/null
+++ b/src/lp/detail/field-decl.hpp
@@ -0,0 +1,150 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2015 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#ifndef NDN_CXX_LP_DETAIL_FIELD_DECL_HPP
+#define NDN_CXX_LP_DETAIL_FIELD_DECL_HPP
+
+#include "../../common.hpp"
+
+#include "../field.hpp"
+#include "../sequence.hpp"
+#include "../cache-policy.hpp"
+#include "../nack.hpp"
+#include "../tlv.hpp"
+
+#include <boost/concept/requires.hpp>
+
+namespace ndn {
+namespace lp {
+namespace detail {
+
+template<typename TlvType, typename T>
+struct DecodeHelper
+{
+ static
+ BOOST_CONCEPT_REQUIRES(((WireDecodable<T>)), (T))
+ decode(const Block& wire)
+ {
+ if (wire.type() != TlvType::value) {
+ throw ndn::tlv::Error("Unexpected TLV type " + std::to_string(wire.type()));
+ }
+
+ T type;
+ type.wireDecode(wire);
+ return type;
+ }
+};
+
+template<typename TlvType>
+struct DecodeHelper<TlvType, uint64_t>
+{
+ static uint64_t
+ decode(const Block& wire)
+ {
+ if (wire.type() != TlvType::value) {
+ throw ndn::tlv::Error("Unexpected TLV type " + std::to_string(wire.type()));
+ }
+
+ return readNonNegativeInteger(wire);
+ }
+};
+
+template<typename TlvType>
+struct DecodeHelper<TlvType, std::pair<Buffer::const_iterator, Buffer::const_iterator>>
+{
+ static std::pair<Buffer::const_iterator, Buffer::const_iterator>
+ decode(const Block& wire)
+ {
+ if (wire.type() != TlvType::value) {
+ throw ndn::tlv::Error("Unexpected TLV type " + std::to_string(wire.type()));
+ }
+
+ if (wire.value_size() == 0) {
+ throw ndn::tlv::Error(std::to_string(wire.type()) + " must not be empty");
+ }
+
+ return std::make_pair(wire.value_begin(), wire.value_end());
+ }
+};
+
+template<typename encoding::Tag TAG, typename TlvType, typename T>
+struct EncodeHelper
+{
+ static
+ BOOST_CONCEPT_REQUIRES(((WireEncodable<T>)), (size_t))
+ encode(EncodingImpl<TAG>& encoder, const T& value)
+ {
+ return value.wireEncode(encoder);
+ }
+};
+
+template<typename encoding::Tag TAG, typename TlvType>
+struct EncodeHelper<TAG, TlvType, uint64_t>
+{
+ static size_t
+ encode(EncodingImpl<TAG>& encoder, const uint64_t value)
+ {
+ return prependNonNegativeIntegerBlock(encoder, TlvType::value, value);
+ }
+};
+
+template<typename encoding::Tag TAG, typename TlvType>
+struct EncodeHelper<TAG, TlvType, std::pair<Buffer::const_iterator, Buffer::const_iterator>>
+{
+ static size_t
+ encode(EncodingImpl<TAG>& encoder, const std::pair<Buffer::const_iterator, Buffer::const_iterator>& value)
+ {
+ size_t length = 0;
+ length += encoder.prependRange(value.first, value.second);
+ length += encoder.prependVarNumber(length);
+ length += encoder.prependVarNumber(TlvType::value);
+ return length;
+ }
+};
+
+template<typename LOCATION, typename VALUE, uint64_t TYPE, bool REPEATABLE = false>
+class FieldDecl
+{
+public:
+ typedef LOCATION FieldLocation;
+ typedef VALUE ValueType;
+ typedef std::integral_constant<uint64_t, TYPE> TlvType;
+ typedef std::integral_constant<bool, REPEATABLE> IsRepeatable;
+
+ static ValueType
+ decode(const Block& wire)
+ {
+ return DecodeHelper<TlvType, ValueType>::decode(wire);
+ }
+
+ template<typename encoding::Tag TAG, typename T>
+ static size_t
+ encode(EncodingImpl<TAG>& encoder, const T& value)
+ {
+ return EncodeHelper<TAG, TlvType, T>::encode(encoder, value);
+ }
+};
+
+} // namespace detail
+} // namespace lp
+} // namesapce ndn
+
+#endif // NDN_CXX_LP_DETAIL_FIELD_DECL_HPP
\ No newline at end of file
diff --git a/src/lp/detail/field-info.cpp b/src/lp/detail/field-info.cpp
new file mode 100644
index 0000000..fadd858
--- /dev/null
+++ b/src/lp/detail/field-info.cpp
@@ -0,0 +1,74 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2015 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#include "field-info.hpp"
+
+#include <boost/mpl/for_each.hpp>
+#include <boost/bind.hpp>
+
+namespace ndn {
+namespace lp {
+namespace detail {
+
+struct ExtractFieldInfo
+{
+ typedef void result_type;
+
+ template<typename T>
+ void
+ operator()(FieldInfo* info, T)
+ {
+ if (T::TlvType::value != info->tlvType) {
+ return;
+ }
+ info->isRecognized = true;
+ info->canIgnore = false;
+ info->isRepeatable = T::IsRepeatable::value;
+ info->locationSortOrder = getLocationSortOrder<typename T::FieldLocation>();
+ }
+};
+
+FieldInfo::FieldInfo()
+ : tlvType(0)
+ , isRecognized(false)
+ , canIgnore(false)
+ , isRepeatable(false)
+ , locationSortOrder(getLocationSortOrder<field_location_tags::Header>())
+{
+}
+
+FieldInfo::FieldInfo(uint64_t tlv)
+ : tlvType(tlv)
+ , isRecognized(false)
+ , canIgnore(false)
+ , isRepeatable(false)
+ , locationSortOrder(getLocationSortOrder<field_location_tags::Header>())
+{
+ boost::mpl::for_each<FieldSet>(boost::bind(ExtractFieldInfo(), this, _1));
+ if (!isRecognized) {
+ canIgnore = tlv::HEADER3_MIN <= tlvType && tlvType <= tlv::HEADER3_MAX &&
+ (tlvType & 0x01) == 0x01;
+ }
+}
+
+} // namespace detail
+} // namespace lp
+} // namespace ndn
\ No newline at end of file
diff --git a/src/lp/detail/field-info.hpp b/src/lp/detail/field-info.hpp
new file mode 100644
index 0000000..2ee0e10
--- /dev/null
+++ b/src/lp/detail/field-info.hpp
@@ -0,0 +1,97 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2015 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#ifndef NDN_CXX_LP_DETAIL_FIELD_INFO_HPP
+#define NDN_CXX_LP_DETAIL_FIELD_INFO_HPP
+
+#include "../../common.hpp"
+
+#include "../fields.hpp"
+
+namespace ndn {
+namespace lp {
+namespace detail {
+
+class FieldInfo
+{
+public:
+ FieldInfo();
+
+ explicit
+ FieldInfo(uint64_t tlv);
+
+public:
+ /**
+ * \brief TLV-TYPE of the field; 0 if field does not exist
+ */
+ uint64_t tlvType;
+
+ /**
+ * \brief is this field known
+ */
+ bool isRecognized;
+
+ /**
+ * \brief can this unknown field be ignored
+ */
+ bool canIgnore;
+
+ /**
+ * \brief is the field repeatable
+ */
+ bool isRepeatable;
+
+ /**
+ * \brief sort order of field_location_tag
+ */
+ int locationSortOrder;
+};
+
+template<typename TAG>
+inline int
+getLocationSortOrder();
+
+template<>
+inline int
+getLocationSortOrder<field_location_tags::Header>()
+{
+ return 1;
+}
+
+template<>
+inline int
+getLocationSortOrder<field_location_tags::Fragment>()
+{
+ return 2;
+}
+
+inline bool
+compareFieldSortOrder(const FieldInfo& first, const FieldInfo& second)
+{
+ return (first.locationSortOrder < second.locationSortOrder) ||
+ (first.locationSortOrder == second.locationSortOrder && first.tlvType < second.tlvType);
+}
+
+} // namespace detail
+} // namespace lp
+} // namespace ndn
+
+#endif // NDN_CXX_LP_DETAIL_FIELD_INFO_HPP
\ No newline at end of file