Make swap noexcept (as it has to be!) and fix swap misuses

This commit is contained in:
Daniel J. Hofmann
2016-02-05 17:53:17 +01:00
parent 7b37c847bd
commit fa8529949b
4 changed files with 30 additions and 28 deletions
+4 -2
View File
@@ -6,6 +6,7 @@
#include <fstream>
#include <array>
#include <utility>
namespace osrm
{
@@ -52,8 +53,9 @@ template <unsigned BLOCK_SIZE, bool USE_SHARED_MEMORY> class RangeTable
const unsigned sum_lengths)
: sum_lengths(sum_lengths)
{
block_offsets.swap(external_offsets);
diff_blocks.swap(external_blocks);
using std::swap;
swap(block_offsets, external_offsets);
swap(diff_blocks, external_blocks);
}
// construct table from length vector
+14 -14
View File
@@ -55,13 +55,6 @@ template <typename DataT> class SharedMemoryWrapper
SharedMemoryWrapper(DataT *ptr, std::size_t size) : m_ptr(ptr), m_size(size) {}
void swap(SharedMemoryWrapper<DataT> &other)
{
// BOOST_ASSERT_MSG(m_size != 0 || other.size() != 0, "size invalid");
std::swap(m_size, other.m_size);
std::swap(m_ptr, other.m_ptr);
}
DataT &at(const std::size_t index) { return m_ptr[index]; }
const DataT &at(const std::size_t index) const { return m_ptr[index]; }
@@ -85,6 +78,9 @@ template <typename DataT> class SharedMemoryWrapper
BOOST_ASSERT_MSG(index < m_size, "invalid size");
return m_ptr[index];
}
template <typename T>
friend void swap(SharedMemoryWrapper<T> &, SharedMemoryWrapper<T> &) noexcept;
};
template <> class SharedMemoryWrapper<bool>
@@ -98,13 +94,6 @@ template <> class SharedMemoryWrapper<bool>
SharedMemoryWrapper(unsigned *ptr, std::size_t size) : m_ptr(ptr), m_size(size) {}
void swap(SharedMemoryWrapper<bool> &other)
{
// BOOST_ASSERT_MSG(m_size != 0 || other.size() != 0, "size invalid");
std::swap(m_size, other.m_size);
std::swap(m_ptr, other.m_ptr);
}
bool at(const std::size_t index) const
{
const std::size_t bucket = index / 32;
@@ -123,8 +112,19 @@ template <> class SharedMemoryWrapper<bool>
const unsigned offset = index % 32;
return m_ptr[bucket] & (1 << offset);
}
template <typename T>
friend void swap(SharedMemoryWrapper<T> &, SharedMemoryWrapper<T> &) noexcept;
};
// Both SharedMemoryWrapper<T> and the SharedMemoryWrapper<bool> specializations share this impl.
template <typename DataT>
void swap(SharedMemoryWrapper<DataT> &lhs, SharedMemoryWrapper<DataT> &rhs) noexcept
{
std::swap(lhs.m_ptr, rhs.m_ptr);
std::swap(lhs.m_size, rhs.m_size);
}
template <typename DataT, bool UseSharedMemory> struct ShM
{
using vector = typename std::conditional<UseSharedMemory,
+3 -2
View File
@@ -105,8 +105,9 @@ template <typename EdgeDataT, bool UseSharedMemory = false> class StaticGraph
number_of_nodes = static_cast<decltype(number_of_nodes)>(nodes.size() - 1);
number_of_edges = static_cast<decltype(number_of_edges)>(edges.size());
node_array.swap(nodes);
edge_array.swap(edges);
using std::swap;
swap(node_array, nodes);
swap(edge_array, edges);
}
unsigned GetNumberOfNodes() const { return number_of_nodes; }