Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[simix] Encapsulate main function, argc and argv in a closure
[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 #include <functional>
13
14 #include <xbt/function_types.h>
15 #include <simgrid/simix.h>
16
17 namespace simgrid {
18 namespace simix {
19
20 class Context;
21 class ContextFactory;
22
23 class ContextFactory {
24 private:
25   std::string name_;
26 public:
27
28   ContextFactory(std::string name) : name_(std::move(name)) {}
29   virtual ~ContextFactory();
30   virtual Context* create_context(std::function<void()> code,
31     void_pfn_smxprocess_t cleanup, smx_process_t process) = 0;
32   virtual void run_all() = 0;
33   virtual Context* self();
34   std::string const& name() const
35   {
36     return name_;
37   }
38 private:
39   void declare_context(void* T, std::size_t size);
40 protected:
41   template<class T, class... Args>
42   T* new_context(Args&&... args)
43   {
44     T* context = new T(std::forward<Args>(args)...);
45     this->declare_context(context, sizeof(T));
46     return context;
47   }
48 };
49
50 class Context {
51 private:
52   std::function<void()> code_;
53   void_pfn_smxprocess_t cleanup_func_ = nullptr;
54   smx_process_t process_ = nullptr;
55 public:
56   bool iwannadie;
57 public:
58   Context(std::function<void()> code,
59           void_pfn_smxprocess_t cleanup_func,
60           smx_process_t process);
61   void operator()()
62   {
63     code_();
64   }
65   bool has_code() const
66   {
67     return (bool) code_;
68   }
69   smx_process_t process()
70   {
71     return this->process_;
72   }
73   void set_cleanup(void_pfn_smxprocess_t cleanup)
74   {
75     cleanup_func_ = cleanup;
76   }
77
78   // Virtual methods
79   virtual ~Context();
80   virtual void stop();
81   virtual void suspend() = 0;
82 };
83
84 }
85 }
86
87 #endif