Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
25658086d2e9b490e26b2219b35f7cd2ccbf091a
[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 private:
53   xbt_main_func_t code_ = nullptr;
54   int argc_ = 0;
55   char **argv_ = nullptr;
56   void_pfn_smxprocess_t cleanup_func_ = nullptr;
57   smx_process_t process_ = nullptr;
58 public:
59   bool iwannadie;
60 public:
61   Context(xbt_main_func_t code,
62           int argc, char **argv,
63           void_pfn_smxprocess_t cleanup_func,
64           smx_process_t process);
65   int operator()()
66   {
67     return code_(argc_, argv_);
68   }
69   smx_process_t process()
70   {
71     return this->process_;
72   }
73
74   // Virtual methods
75   virtual ~Context();
76   virtual void stop();
77   virtual void suspend() = 0;
78 };
79
80 }
81 }
82
83 #endif