encoding+util: ensure move constructors are properly declared
This also fixes a race condition in scheduler::EventId::operator bool()
Change-Id: I468f0c46039a3d1a38c69c419ae45b4445d8205a
Refs: #3414
diff --git a/src/encoding/block.cpp b/src/encoding/block.cpp
index 639ed68..1aa9756 100644
--- a/src/encoding/block.cpp
+++ b/src/encoding/block.cpp
@@ -34,20 +34,17 @@
namespace ndn {
BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Block>));
-static_assert(std::is_nothrow_move_constructible<Block>::value,
- "Block must be MoveConstructible with noexcept");
-static_assert(std::is_nothrow_move_assignable<Block>::value,
- "Block must be MoveAssignable with noexcept");
const size_t MAX_SIZE_OF_BLOCK_FROM_STREAM = MAX_NDN_PACKET_SIZE;
// ---- constructor, creation, assignment ----
-Block::Block()
- : m_type(std::numeric_limits<uint32_t>::max())
- , m_size(0)
-{
-}
+Block::Block() = default;
+
+Block::Block(const Block&) = default;
+
+Block&
+Block::operator=(const Block&) = default;
Block::Block(const EncodingBuffer& buffer)
: Block(buffer.getBuffer(), buffer.begin(), buffer.end(), true)
@@ -116,10 +113,13 @@
uint64_t length = tlv::readVarNumber(pos, end);
// pos now points to TLV-VALUE
+ BOOST_ASSERT(pos <= end);
if (length > static_cast<uint64_t>(end - pos)) {
BOOST_THROW_EXCEPTION(Error("Not enough bytes in the buffer to fully parse TLV"));
}
- size_t typeLengthSize = pos - buf;
+
+ BOOST_ASSERT(pos > buf);
+ uint64_t typeLengthSize = static_cast<uint64_t>(pos - buf);
m_size = typeLengthSize + length;
m_buffer = make_shared<Buffer>(buf, m_size);
@@ -262,7 +262,7 @@
Block::resetWire()
{
m_buffer.reset(); // discard underlying buffer by resetting shared_ptr
- m_begin = m_end = m_valueBegin = m_valueEnd = Buffer::const_iterator();
+ m_begin = m_end = m_valueBegin = m_valueEnd = {};
}
Buffer::const_iterator
@@ -313,7 +313,7 @@
size_t
Block::value_size() const
{
- return hasValue() ? m_valueEnd - m_valueBegin : 0;
+ return hasValue() ? static_cast<size_t>(m_valueEnd - m_valueBegin) : 0;
}
Block
diff --git a/src/encoding/block.hpp b/src/encoding/block.hpp
index 63f3677..2aec91c 100644
--- a/src/encoding/block.hpp
+++ b/src/encoding/block.hpp
@@ -61,6 +61,24 @@
*/
Block();
+ /** @brief Copy constructor
+ */
+ Block(const Block&);
+
+ /** @brief Copy assignment operator
+ */
+ Block&
+ operator=(const Block&);
+
+ /** @brief Move constructor
+ */
+ Block(Block&&) noexcept;
+
+ /** @brief Move assignment operator
+ */
+ Block&
+ operator=(Block&&) noexcept;
+
/** @brief Parse Block from an EncodingBuffer
* @param buffer an EncodingBuffer containing one TLV element
* @throw tlv::Error Type-Length parsing fails, or TLV-LENGTH does not match size of TLV-VALUE
@@ -412,13 +430,13 @@
Buffer::const_iterator m_valueBegin; ///< @sa m_buffer
Buffer::const_iterator m_valueEnd; ///< @sa m_buffer
- uint32_t m_type; ///< TLV-TYPE
+ uint32_t m_type = std::numeric_limits<uint32_t>::max(); ///< TLV-TYPE
/** @brief total size including Type-Length-Value
*
* This field is valid only if empty() is false.
*/
- uint32_t m_size;
+ size_t m_size = 0;
/** @brief sub elements
*
@@ -439,6 +457,12 @@
operator<<(std::ostream& os, const Block& block);
};
+inline
+Block::Block(Block&&) noexcept = default;
+
+inline Block&
+Block::operator=(Block&&) noexcept = default;
+
/** @brief Compare whether two Blocks have same TLV-TYPE, TLV-LENGTH, and TLV-VALUE
*/
bool
diff --git a/src/encoding/buffer.cpp b/src/encoding/buffer.cpp
deleted file mode 100644
index 531e9fa..0000000
--- a/src/encoding/buffer.cpp
+++ /dev/null
@@ -1,46 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/*
- * Copyright (c) 2013-2018 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.
- *
- * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
- */
-
-#include "buffer.hpp"
-
-namespace ndn {
-
-static_assert(std::is_nothrow_move_constructible<Buffer>::value,
- "Buffer must be MoveConstructible with noexcept");
-static_assert(std::is_nothrow_move_assignable<Buffer>::value,
- "Buffer must be MoveAssignable with noexcept");
-
-Buffer::Buffer() = default;
-
-Buffer::Buffer(size_t size)
- : std::vector<uint8_t>(size, 0)
-{
-}
-
-Buffer::Buffer(const void* buf, size_t length)
- : std::vector<uint8_t>(reinterpret_cast<const uint8_t*>(buf),
- reinterpret_cast<const uint8_t*>(buf) + length)
-{
-}
-
-} // namespace ndn
diff --git a/src/encoding/buffer.hpp b/src/encoding/buffer.hpp
index fbfeb00..d695f1c 100644
--- a/src/encoding/buffer.hpp
+++ b/src/encoding/buffer.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2013-2017 Regents of the University of California.
+ * Copyright (c) 2013-2018 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -42,19 +42,44 @@
public:
/** @brief Creates an empty Buffer
*/
- Buffer();
+ Buffer() = default;
+
+ /** @brief Copy constructor
+ */
+ Buffer(const Buffer&);
+
+ /** @brief Copy assignment operator
+ */
+ Buffer&
+ operator=(const Buffer&);
+
+ /** @brief Move constructor
+ */
+ Buffer(Buffer&&) noexcept;
+
+ /** @brief Move assignment operator
+ */
+ Buffer&
+ operator=(Buffer&&) noexcept;
/** @brief Creates a Buffer with pre-allocated size
* @param size size of the Buffer to be allocated
*/
explicit
- Buffer(size_t size);
+ Buffer(size_t size)
+ : std::vector<uint8_t>(size, 0)
+ {
+ }
/** @brief Creates a Buffer by copying contents from a raw buffer
* @param buf const pointer to buffer to copy
* @param length length of the buffer to copy
*/
- Buffer(const void* buf, size_t length);
+ Buffer(const void* buf, size_t length)
+ : std::vector<uint8_t>(reinterpret_cast<const uint8_t*>(buf),
+ reinterpret_cast<const uint8_t*>(buf) + length)
+ {
+ }
/** @brief Creates a Buffer by copying the elements of the range [first, last)
* @param first an input iterator to the first element to copy
@@ -85,6 +110,18 @@
}
};
+inline
+Buffer::Buffer(const Buffer&) = default;
+
+inline Buffer&
+Buffer::operator=(const Buffer&) = default;
+
+inline
+Buffer::Buffer(Buffer&&) noexcept = default;
+
+inline Buffer&
+Buffer::operator=(Buffer&&) noexcept = default;
+
using BufferPtr = shared_ptr<Buffer>;
using ConstBufferPtr = shared_ptr<const Buffer>;