Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of framagit.org:simgrid/simgrid
[simgrid.git] / include / simgrid / s4u / Activity.hpp
index 22fa0a7..4608f49 100644 (file)
@@ -6,13 +6,16 @@
 #ifndef SIMGRID_S4U_ACTIVITY_HPP
 #define SIMGRID_S4U_ACTIVITY_HPP
 
-#include "xbt/asserts.h"
+#include <xbt/asserts.h>
+#include <algorithm>
 #include <atomic>
 #include <set>
 #include <simgrid/forward.h>
+#include <stdexcept>
 #include <string>
 #include <vector>
 #include <xbt/signal.hpp>
+#include <xbt/utility.hpp>
 
 XBT_LOG_EXTERNAL_CATEGORY(s4u_activity);
 
@@ -50,10 +53,29 @@ protected:
 
   void add_successor(ActivityPtr a)
   {
+    if(this == a)
+      throw std::invalid_argument("Cannot be its own successor");
+    auto p = std::find_if(successors_.begin(), successors_.end(), [a](ActivityPtr const& i){ return i.get() == a.get(); });
+    if (p != successors_.end())
+      throw std::invalid_argument("Dependency already exists");
+
     successors_.push_back(a);
     a->dependencies_.insert({this});
   }
 
+  void remove_successor(ActivityPtr a)
+  {
+    if(this == a)
+      throw std::invalid_argument("Cannot ask to remove its from successors");
+
+    auto p = std::find_if(successors_.begin(), successors_.end(), [a](ActivityPtr const& i){ return i.get() == a.get(); });
+    if (p != successors_.end()){
+      successors_.erase(p);
+      a->dependencies_.erase({this});
+    } else
+      throw std::invalid_argument("Dependency does not exist. Can not be removed.");
+  }
+
 public:
   void vetoable_start()
   {
@@ -69,7 +91,8 @@ public:
   Activity& operator=(Activity const&) = delete;
 #endif
 
-  enum class State { INITED = 0, STARTING, STARTED, CANCELED, FINISHED };
+  // enum class State { ... }
+  XBT_DECLARE_ENUM_CLASS(State, INITED, STARTING, STARTED, CANCELED, FINISHED);
 
   /** Starts a previously created activity.
    *
@@ -153,7 +176,11 @@ public:
     Activity::add_successor(a);
     return static_cast<AnyActivity*>(this);
   }
-
+  AnyActivity* remove_successor(ActivityPtr a)
+  {
+    Activity::remove_successor(a);
+    return static_cast<AnyActivity*>(this);
+  }
   AnyActivity* set_name(const std::string& name)
   {
     xbt_assert(get_state() == State::INITED, "Cannot change the name of an activity after its start");