Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Missing explicit.
[simgrid.git] / src / mc / remote / RemotePtr.hpp
index d92eb7a..2f51118 100644 (file)
@@ -1,5 +1,4 @@
-/* Copyright (c) 2008-2017. The SimGrid Team.
- * All rights reserved.                                                     */
+/* Copyright (c) 2008-2019. The SimGrid Team. All rights reserved.          */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
@@ -7,11 +6,7 @@
 #ifndef SIMGRID_MC_REMOTE_PTR_HPP
 #define SIMGRID_MC_REMOTE_PTR_HPP
 
-#include <cstddef>
-#include <cstdint>
-#include <cstring>
-
-#include <stdexcept>
+#include "src/simix/smx_private.hpp"
 #include <type_traits>
 
 namespace simgrid {
@@ -25,41 +20,29 @@ namespace mc {
  *
  *  * raw memory copy (std::memcpy) is used to copy Remote<T>;
  *
- *  * raw memory comparison is used to compare them;
- *
  *  * when T is a trivial type, Remote is convertible to a T.
  *
  *  We currently only handle the case where the type has the same layout
  *  in the current process and in the target process: we don't handle
  *  cross-architecture (such as 32-bit/64-bit access).
  */
-template <class T> union Remote {
+template <class T> class Remote {
 private:
-  T buffer;
+  typename std::aligned_storage<sizeof(T), alignof(T)>::type buffer;
 
 public:
-  Remote() { /* Nothing to do */}
-  ~Remote() { /* Nothing to do */}
-  Remote(T const& p) { std::memcpy(static_cast<void*>(&buffer), static_cast<const void*>(&p), sizeof(buffer)); }
-  Remote(Remote const& that)
-  {
-    std::memcpy(static_cast<void*>(&buffer), static_cast<const void*>(&that.buffer), sizeof(buffer));
-  }
-  Remote& operator=(Remote const& that)
-  {
-    std::memcpy(static_cast<void*>(&buffer), static_cast<const void*>(&that.buffer), sizeof(buffer));
-    return *this;
-  }
-  T* getBuffer() { return &buffer; }
-  const T* getBuffer() const { return &buffer; }
+  Remote() = default;
+  explicit Remote(T const& p) { std::memcpy(&buffer, &p, sizeof buffer); }
+
+  T* getBuffer() { return reinterpret_cast<T*>(&buffer); }
+  const T* getBuffer() const { return reinterpret_cast<const T*>(&buffer); }
   std::size_t getBufferSize() const { return sizeof(T); }
   operator T() const
   {
-//FIXME: assert turned off because smpi:Request is not seen as "trivial".
-//    static_assert(std::is_trivial<T>::value, "Cannot convert non trivial type");
-    return buffer;
+    static_assert(std::is_trivial<T>::value, "Cannot convert non trivial type");
+    return *getBuffer();
   }
-  void clear() { std::memset(static_cast<void*>(&buffer), 0, sizeof(T)); }
+  void clear() { std::memset(&buffer, 0, sizeof buffer); }
 };
 
 /** Pointer to a remote address-space (process, snapshot)
@@ -81,9 +64,10 @@ template <class T> class RemotePtr {
 
 public:
   RemotePtr() : address_(0) {}
-  RemotePtr(std::uint64_t address) : address_(address) {}
-  RemotePtr(T* address) : address_((std::uintptr_t)address) {}
-  RemotePtr(Remote<T*> p) : RemotePtr(*p.getBuffer()) {}
+  explicit RemotePtr(std::nullptr_t) : address_(0) {}
+  explicit RemotePtr(std::uint64_t address) : address_(address) {}
+  explicit RemotePtr(T* address) : address_((std::uintptr_t)address) {}
+  explicit RemotePtr(Remote<T*> p) : address_((std::uintptr_t)*p.getBuffer()) {}
   std::uint64_t address() const { return address_; }
 
   /** Turn into a local pointer
@@ -94,6 +78,11 @@ public:
   operator bool() const { return address_; }
   bool operator!() const { return not address_; }
   operator RemotePtr<void>() const { return RemotePtr<void>(address_); }
+  RemotePtr<T>& operator=(std::nullptr_t)
+  {
+    address_ = 0;
+    return *this;
+  }
   RemotePtr<T> operator+(std::uint64_t n) const { return RemotePtr<T>(address_ + n * sizeof(T)); }
   RemotePtr<T> operator-(std::uint64_t n) const { return RemotePtr<T>(address_ - n * sizeof(T)); }
   RemotePtr<T>& operator+=(std::uint64_t n)