adamantine
utils.hh
Go to the documentation of this file.
1 /* SPDX-FileCopyrightText: Copyright (c) 2016 - 2024, the adamantine authors.
2  * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3  */
4 
5 #ifndef UTILS_HH
6 #define UTILS_HH
7 
8 #include <deal.II/base/exceptions.h>
9 
10 #include <cassert>
11 #include <cstring>
12 #include <exception>
13 #include <filesystem>
14 #include <iostream>
15 #include <stdexcept>
16 #include <string>
17 #include <utility>
18 
19 namespace adamantine
20 {
24 inline void wait_for_file(std::string const &filename,
25  std::string const &message)
26 {
27  unsigned int counter = 1;
28  while (!std::filesystem::exists(filename))
29  {
30  // Spin loop waiting for the file to appear (message printed if counter
31  // overflows)
32  if (counter == 0)
33  std::cout << message << std::endl;
34  ++counter;
35  }
36 }
37 
41 inline void
42 wait_for_file_to_update(std::string const &filename, std::string const &message,
43  std::filesystem::file_time_type &last_write_time)
44 {
45  unsigned int counter = 1;
46  // We check when the file was last written to know if he file was updated.
47  // When the file is being overwritten, the last_write_time() function throws
48  // an error. Since it's an "expected" behavior, we just catch the error keep
49  // working.
50  try
51  {
52  while (std::filesystem::last_write_time(filename) == last_write_time)
53  {
54  // Spin loop waiting for the file to be updated (message printed if
55  // counter overflows)
56  if (counter == 0)
57  std::cout << message << std::endl;
58  ++counter;
59  }
60  }
61  catch (std::filesystem::filesystem_error const &)
62  {
63  // No-op
64  }
65  last_write_time = std::filesystem::last_write_time(filename);
66 }
67 
68 #define ASSERT(condition, message) assert((condition) && (message))
69 
70 inline void ASSERT_THROW(bool cond, std::string const &message)
71 {
72  if (cond == false)
73  throw std::runtime_error(message);
74 }
75 
76 // ----------- Custom Exceptions --------------//
77 class NotImplementedExc : public std::exception
78 {
79  virtual const char *what() const throw() override
80  {
81  return "The function is not implemented";
82  }
83 };
84 
86 {
87  NotImplementedExc exception;
88  throw exception;
89 }
90 
91 } // namespace adamantine
92 
93 #endif
virtual const char * what() const override
Definition: utils.hh:79
void ASSERT_THROW_NOT_IMPLEMENTED()
Definition: utils.hh:85
void wait_for_file_to_update(std::string const &filename, std::string const &message, std::filesystem::file_time_type &last_write_time)
Definition: utils.hh:42
void wait_for_file(std::string const &filename, std::string const &message)
Definition: utils.hh:24
void ASSERT_THROW(bool cond, std::string const &message)
Definition: utils.hh:70