Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[simgrid.git] / include / simgrid / s4u / engine.hpp
1 /* Copyright (c) 2006-2015. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #ifndef SIMGRID_S4U_ENGINE_HPP
7 #define SIMGRID_S4U_ENGINE_HPP
8
9 #include <xbt/base.h>
10 #include <xbt/functional.hpp>
11
12 #include <simgrid/simix.hpp>
13
14 #include <simgrid/s4u/forward.hpp>
15
16 namespace simgrid {
17 namespace s4u {
18 /** @brief Simulation engine
19  *
20  * This class is an interface to the simulation engine.
21  */
22 XBT_PUBLIC_CLASS Engine {
23 public:
24   /** Constructor, taking the command line parameters of your main function */
25   Engine(int *argc, char **argv);
26
27   /** Finalize the default engine and all its dependencies */
28   static void shutdown();
29
30   /** @brief Load a platform file describing the environment
31    *
32    * The environment is either a XML file following the simgrid.dtd formalism, or a lua file.
33    * Some examples can be found in the directory examples/platforms.
34    */
35   void loadPlatform(const char *platf);
36
37   /** Registers the main function of an actor that will be launched from the deployment file */
38   void registerFunction(const char*name, int (*code)(int,char**));
39
40   /** Registers a function as the default main function of actors
41    *
42    * It will be used as fallback when the function requested from the deployment file was not registered.
43    * It is used for trace-based simulations (see examples/msg/actions).
44    */
45   void registerDefault(int (*code)(int,char**));
46
47   /** @brief Load a deployment file and launch the actors that it contains */
48   void loadDeployment(const char *deploy);
49
50   /** @brief Run the simulation */
51   void run();
52
53   /** @brief Retrieve the simulation time */
54   static double getClock();
55   
56   /** @brief Retrieve the engine singleton */
57   static s4u::Engine *instance();
58
59   /** @brief Retrieve the root AS, containing all others */
60   simgrid::s4u::As *rootAs();
61   /** @brief Retrieve the AS of the given name (or nullptr if not found) */
62   simgrid::s4u::As *asByNameOrNull(const char *name);
63
64   template<class F>
65   void registerFunction(const char* name)
66   {
67     simgrid::simix::registerFunction(name, [](std::vector<std::string> args){
68       return simgrid::simix::ActorCode([args] {
69         F code(std::move(args));
70         code();
71       });
72     });
73   }
74
75   template<class F>
76   void registerFunction(const char* name, F code)
77   {
78     simgrid::simix::registerFunction(name, [code](std::vector<std::string> args){
79       return simgrid::simix::ActorCode([code,args] {
80         code(std::move(args));
81       });
82     });
83   }
84
85 private:
86   static s4u::Engine *instance_;
87 };
88 }} // namespace simgrid::s4u
89
90 #endif /* SIMGRID_S4U_ENGINE_HPP */