Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
factoring
[simgrid.git] / src / kernel / EngineImpl.cpp
1 /* Copyright (c) 2016-2019. 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 #include "src/kernel/EngineImpl.hpp"
7 #include "simgrid/Exception.hpp"
8 #include "simgrid/kernel/routing/NetPoint.hpp"
9 #include "simgrid/kernel/routing/NetZoneImpl.hpp"
10 #include "simgrid/s4u/Host.hpp"
11 #include "src/kernel/resource/DiskImpl.hpp"
12 #include "src/simix/smx_private.hpp"
13 #include "src/surf/StorageImpl.hpp"
14 #include "src/surf/network_interface.hpp"
15 #include "src/surf/xml/platf_private.hpp" // FIXME: KILLME. There must be a better way than mimicking XML here
16
17 extern int surf_parse_lineno;
18
19 namespace simgrid {
20 namespace kernel {
21
22 EngineImpl::~EngineImpl()
23 {
24   /* Since hosts_ is a std::map, the hosts are destroyed in the lexicographic order, which ensures that the output is
25    * reproducible.
26    */
27   while (not hosts_.empty())
28     hosts_.begin()->second->destroy();
29
30   /* Also delete the other data */
31   delete netzone_root_;
32   for (auto const& kv : netpoints_)
33     delete kv.second;
34
35   for (auto const& kv : storages_)
36     if (kv.second)
37       kv.second->get_impl()->destroy();
38
39   for (auto const& kv : links_)
40     if (kv.second)
41       kv.second->get_impl()->destroy();
42 }
43
44 void EngineImpl::load_deployment(const std::string& file)
45 {
46   sg_platf_exit();
47   sg_platf_init();
48
49   surf_parse_open(file);
50   try {
51     int parse_status = surf_parse();
52     surf_parse_close();
53     xbt_assert(not parse_status, "Parse error at %s:%d", file.c_str(), surf_parse_lineno);
54   } catch (const Exception&) {
55     xbt_die(
56         "Unrecoverable error at %s:%d. The full exception stack follows, in case it helps you to diagnose the problem.",
57         file.c_str(), surf_parse_lineno);
58     throw;
59   }
60 }
61 void EngineImpl::register_function(const std::string& name, xbt_main_func_t code)
62 {
63   simix_global->registered_functions[name] = [code](std::vector<std::string> args) {
64     return simgrid::xbt::wrap_main(code, std::move(args));
65   };
66 }
67
68 void EngineImpl::register_function(const std::string& name, void (*code)(std::vector<std::string>))
69 {
70   simix_global->registered_functions[name] = [code](std::vector<std::string> args) {
71     return std::bind(std::move(code), std::move(args));
72   };
73 }
74
75 void EngineImpl::register_default(xbt_main_func_t code)
76 {
77   simix_global->default_function = [code](std::vector<std::string> args) {
78     return simgrid::xbt::wrap_main(code, std::move(args));
79   };
80 }
81
82 } // namespace kernel
83 } // namespace simgrid