HAL
gate_library_writer_manager.cpp
Go to the documentation of this file.
2 
7 
8 #include <chrono>
9 #include <fstream>
10 #include <unordered_map>
11 
12 namespace hal
13 {
14  namespace gate_library_writer_manager
15  {
16  namespace
17  {
18  std::unordered_map<std::string, std::vector<std::string>> m_writer_to_extensions;
19  std::unordered_map<std::string, std::pair<std::string, WriterFactory>> m_extension_to_writer;
20 
21  WriterFactory get_writer_factory_for_file(const std::filesystem::path& file_name)
22  {
23  std::string extension = utils::to_lower(file_name.extension().string());
24  if (!extension.empty() && extension[0] != '.')
25  {
26  extension = "." + extension;
27  }
28 
29  if (auto it = m_extension_to_writer.find(extension); it != m_extension_to_writer.end())
30  {
31  log_info("gate_library_writer", "selected gate library writer '{}'.", it->second.first);
32  return it->second.second;
33  }
34 
35  log_error("gate_library_writer", "no gate library writer registered for file extension '{}'.", extension);
36  return WriterFactory();
37  }
38  } // namespace
39 
40  void register_writer(const std::string& name, const WriterFactory& writer_factory, const std::vector<std::string>& supported_file_extensions)
41  {
42  for (std::string ext : supported_file_extensions)
43  {
44  ext = utils::trim(utils::to_lower(ext));
45  if (!ext.empty() && ext[0] != '.')
46  {
47  ext = "." + ext;
48  }
49  if (auto it = m_extension_to_writer.find(ext); it != m_extension_to_writer.end())
50  {
51  log_warning("gate_library_writer", "writer '{}' cannot be registered as file extension '{}' is already associated with writer '{}'.", name, ext, it->second.first);
52  continue;
53  }
54  m_extension_to_writer.emplace(ext, std::make_pair(name, writer_factory));
55  m_writer_to_extensions[name].push_back(ext);
56 
57  log_info("gate_library_writer", "registered gate library writer '{}' for file extension '{}'.", name, ext);
58  }
59  }
60 
61  void unregister_writer(const std::string& name)
62  {
63  if (auto it = m_writer_to_extensions.find(name); it != m_writer_to_extensions.end())
64  {
65  for (const auto& ext : it->second)
66  {
67  if (auto rm_it = m_extension_to_writer.find(ext); rm_it != m_extension_to_writer.end())
68  {
69  m_extension_to_writer.erase(rm_it);
70  log_info("gate_library_writer", "unregistered gate library writer '{}' for file extension '{}'.", name, ext);
71  }
72  }
73  m_writer_to_extensions.erase(it);
74  }
75  }
76 
77  bool write(const GateLibrary* gate_lib, const std::filesystem::path& file_path)
78  {
79  auto factory = get_writer_factory_for_file(file_path);
80  if (!factory)
81  {
82  return false;
83  }
84 
85  std::unique_ptr<GateLibraryWriter> writer = factory();
86 
87  log_info("gate_library_writer", "writing gate library '{}' to file '{}'...", gate_lib->get_name(), file_path.string());
88 
89  auto begin_time = std::chrono::high_resolution_clock::now();
90  if (!writer->write(gate_lib, file_path))
91  {
92  log_error("gate_library_writer", "failed to write gate library '{}' to file '{}'.", gate_lib->get_name(), file_path.string());
93  return false;
94  }
95 
96  log_info("gate_library_writer",
97  "wrote gate library '{}' to file '{}' in {:2.2f} seconds.",
98  gate_lib->get_name(),
99  file_path.string(),
100  (double)std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - begin_time).count() / 1000);
101 
102  return true;
103  }
104  } // namespace gate_library_writer_manager
105 } // namespace hal
std::string get_name() const
#define log_error(channel,...)
Definition: log.h:78
#define log_info(channel,...)
Definition: log.h:70
#define log_warning(channel,...)
Definition: log.h:76
void unregister_writer(const std::string &name)
std::function< std::unique_ptr< GateLibraryWriter >()> WriterFactory
void register_writer(const std::string &name, const WriterFactory &writer_factory, const std::vector< std::string > &supported_file_extensions)
bool write(const GateLibrary *gate_lib, const std::filesystem::path &file_path)
CORE_API T trim(const T &s, const char *to_remove=" \t\r\n")
Definition: utils.h:358
CORE_API T to_lower(const T &s)
Definition: utils.h:477
std::string name