Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[simix] Fix SMPI+MSG
[simgrid.git] / include / simgrid / simix.hpp
1 /* Copyright (c) 2007-2010, 2012-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #ifndef SIMGRID_SIMIX_HPP
8 #define SIMGRID_SIMIX_HPP
9
10 #include <utility>
11 #include <memory>
12
13 #include <xbt/function_types.h>
14 #include <simgrid/simix.h>
15
16 namespace simgrid {
17 namespace simix {
18
19 class Context;
20 class ContextFactory;
21
22 class ContextFactory {
23 private:
24   std::string name_;
25 public:
26
27   ContextFactory(std::string name) : name_(std::move(name)) {}
28   virtual ~ContextFactory();
29   virtual Context* create_context(
30     xbt_main_func_t, int, char **, void_pfn_smxprocess_t,
31     smx_process_t process
32     ) = 0;
33   virtual void run_all() = 0;
34   virtual Context* self();
35   std::string const& name() const
36   {
37     return name_;
38   }
39 private:
40   void declare_context(void* T, std::size_t size);
41 protected:
42   template<class T, class... Args>
43   T* new_context(Args&&... args)
44   {
45     T* context = new T(std::forward<Args>(args)...);
46     this->declare_context(context, sizeof(T));
47     return context;
48   }
49 };
50
51 class Context {
52 protected:
53   xbt_main_func_t code_ = nullptr;
54   int argc_ = 0;
55   char **argv_ = nullptr;
56 private:
57   void_pfn_smxprocess_t cleanup_func_ = nullptr;
58   smx_process_t process_ = nullptr;
59 public:
60   bool iwannadie;
61 public:
62   Context(xbt_main_func_t code,
63           int argc, char **argv,
64           void_pfn_smxprocess_t cleanup_func,
65           smx_process_t process);
66   int operator()()
67   {
68     return code_(argc_, argv_);
69   }
70   smx_process_t process()
71   {
72     return this->process_;
73   }
74   void set_cleanup(void_pfn_smxprocess_t cleanup)
75   {
76     cleanup_func_ = cleanup;
77   }
78
79   // Virtual methods
80   virtual ~Context();
81   virtual void stop();
82   virtual void suspend() = 0;
83 };
84
85 }
86 }
87
88 #endif