BioDynaMo  v1.05.119-a4ff3934
visualization_adaptor.cc
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------
2 //
3 // Copyright (C) 2021 CERN & University of Surrey for the benefit of the
4 // BioDynaMo collaboration. All Rights Reserved.
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 //
9 // See the LICENSE file distributed with this work for details.
10 // See the NOTICE file distributed with this work for additional information
11 // regarding copyright ownership.
12 //
13 // -----------------------------------------------------------------------------
14 
15 #include <string>
16 #include <unordered_map>
17 
18 #include "core/param/param.h"
19 #include "core/simulation.h"
20 #include "core/util/log.h"
22 
23 #include "TInterpreter.h"
24 #include "TPluginManager.h"
25 #include "TROOT.h"
26 
27 namespace bdm {
28 
29 // This map keeps track of whether plugins (the keys) have been loaded, and
30 // registers their corresponding plugin handler pointer (the values). This way
31 // we can reuse the plugin handler without dynamically loading the shared
32 // library more than once
33 static std::unordered_map<std::string, TPluginHandler *> loaded_;
34 
35 VisualizationAdaptor *VisualizationAdaptor::Create(const std::string &adaptor) {
36  auto *param = Simulation::GetActive()->GetParam();
37  if (!(param->insitu_visualization || param->export_visualization)) {
38  return nullptr;
39  }
40  VisualizationAdaptor *va = nullptr;
41  bool first_try = !loaded_.count(adaptor);
42  // If this is the first time we try to load `adaptor`
43  if (first_try) {
44  // Try to find plugin handler in etc/plugins
45  auto *h = gPluginMgr->FindHandler("VisualizationAdaptor", adaptor.c_str());
46  if (h) {
47  // Try to load the plugin (dynamically load shared library)
48  if (h->LoadPlugin() == 0) {
49  // Register the plugin handler for future use
50  loaded_[adaptor] = h;
51  va = reinterpret_cast<VisualizationAdaptor *>(h->ExecPlugin(0));
52  return va;
53  } else {
54  Log::Error("VisualizationAdaptor::Create",
55  "Was unable to load plugin '", adaptor, "'!");
56  return nullptr;
57  }
58  } else {
59  Log::Error(
60  "VisualizationAdaptor::Create", "Unable to find plugin '", adaptor,
61  "'. This is most likely because bdm.rootrc was not read properly.");
62  }
63  } else { // If we already tried loading this plugin, we avoid dynamically
64  // loading the shared library again
65  if (loaded_[adaptor]) { // If we successfully loaded the plugin before
66  va = reinterpret_cast<VisualizationAdaptor *>(
67  loaded_[adaptor]->ExecPlugin(0));
68  return va;
69  } else { // If we failed to load the plugin before
70  return nullptr;
71  }
72  }
73 
74  Log::Info("VisualizationAdaptor::Create", "Loaded plugin '", adaptor,
75  "' successfully!");
76  return va;
77 }
78 
79 } // namespace bdm
bdm
Definition: agent.cc:39
bdm::VisualizationAdaptor
Definition: visualization_adaptor.h:22
bdm::Log::Info
static void Info(const std::string &location, const Args &... parts)
Prints information message.
Definition: log.h:55
bdm::Log::Error
static void Error(const std::string &location, const Args &... parts)
Prints error message.
Definition: log.h:79
log.h
simulation.h
bdm::Simulation::GetParam
const Param * GetParam() const
Returns the simulation parameters.
Definition: simulation.cc:254
bdm::VisualizationAdaptor::Create
static VisualizationAdaptor * Create(const std::string &adaptor)
Definition: visualization_adaptor.cc:35
param.h
bdm::Simulation::GetActive
static Simulation * GetActive()
This function returns the currently active Simulation simulation.
Definition: simulation.cc:68
visualization_adaptor.h