User Guide
common_utils.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2023 Blue Brain Project, EPFL.
3  * See the top-level LICENSE file for details.
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #pragma once
9 
10 #include <map>
11 #include <memory>
12 #include <string>
13 #include <vector>
14 
15 
16 /**
17  *
18  * \dir
19  * \brief Utility classes and function
20  *
21  * \file
22  * \brief Common utility functions for file/dir manipulation
23  */
24 
25 namespace nmodl {
26 /// file/string manipulation functions
27 namespace utils {
28 
29 /**
30  * @defgroup utils Utility Implementation
31  * @brief Utility classes and function implementation
32  * @{
33  */
34 
35 /// Check if the iterator is pointing to last element in the container
36 template <typename Iter, typename Cont>
37 bool is_last(Iter iter, const Cont& cont) {
38  return ((iter != cont.end()) && (next(iter) == cont.end()));
39 }
40 
41 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
42 /// The character conventionally used by the operating system to separate search path components
43 static constexpr char envpathsep{';'};
44 #else
45 /// The character conventionally used by the operating system to separate search path components
46 static constexpr char envpathsep{':'};
47 #endif
48 
49 /// Enum to wrap bool variable to select if random string
50 /// should have numbers or not
51 enum UseNumbersInString : bool { WithNumbers = true, WithoutNumbers = false };
52 
53 /// Generate random std::string of length len based on a
54 /// uniform distribution
55 std::string generate_random_string(int len, UseNumbersInString use_numbers);
56 
57 /**
58  * \class SingletonRandomString
59  * \brief Singleton class for random strings
60  *
61  * Singleton class for random strings that are appended to the
62  * Eigen matrices names that are used in the solutions of
63  * nmodl::visitor::SympySolverVisitor and need to be the same to
64  * be printed by the nmodl::codegen::CodegenCoreneuronCppVisitor
65  */
66 template <unsigned int SIZE = 4>
68  public:
69  /// Delete public constructor needed for the singleton pattern to work
71  /// Delete public "=" operator
73 
74  /// Function to instantiate the SingletonRandomString class
76  static SingletonRandomString srs;
77  return srs;
78  }
79 
80  /** Check if there is a random string assigned as suffix for the var_name variable
81  *
82  * @param var_name Variable name to check if exists in the std::map of random strings
83  * @return true if it exists, false if not
84  */
85  bool random_string_exists(const std::string& var_name) const {
86  return (random_strings.find(var_name) != random_strings.end());
87  }
88 
89  /** Get the random string of the var_name variable
90  *
91  * @param var_name Variable name for which to get the random string
92  * @return Random string assigned to var_name
93  */
94  const std::string& get_random_string(const std::string& var_name) const {
95  return random_strings.at(var_name);
96  }
97 
98  /** If var_name has already got a random string assigned remove it from map
99  * and assign a new one, else simply insert a new random string for var_name
100  *
101  * @param var_name Variable name for which to reset the random string
102  * @param use_numbers control whether random string can include numeric characters or not
103  * @return Random string assigned to var_name
104  */
105  const std::string& reset_random_string(const std::string& var_name,
106  UseNumbersInString use_numbers) {
107  const auto new_string = generate_random_string(SIZE, use_numbers);
108  auto status = random_strings.emplace(var_name, new_string);
109  if (!status.second) {
110  // insertion didn't take place since element was already there.
111  status.first->second = new_string;
112  }
113  return status.first->second;
114  }
115 
116  private:
117  /// \name Ctor & dtor
118  /// \{
119 
120  /// Constructor used by instance()
121  SingletonRandomString() = default;
122 
123  /// \}
124 
125  /// std::map that keeps the random strings assigned to variables as suffix
126  std::map<std::string, std::string> random_strings;
127 };
128 
129 /** @} */ // end of utils
130 
131 } // namespace utils
132 } // namespace nmodl
nmodl::utils::is_last
bool is_last(Iter iter, const Cont &cont)
Check if the iterator is pointing to last element in the container.
Definition: common_utils.hpp:37
nmodl::utils::SingletonRandomString::random_strings
std::map< std::string, std::string > random_strings
std::map that keeps the random strings assigned to variables as suffix
Definition: common_utils.hpp:126
nmodl
encapsulates code generation backend implementations
Definition: ast_common.hpp:26
nmodl::utils::SingletonRandomString::operator=
SingletonRandomString & operator=(SingletonRandomString const &)=delete
Delete public "=" operator.
nmodl::utils::SingletonRandomString
Singleton class for random strings.
Definition: common_utils.hpp:67
nmodl::utils::UseNumbersInString
UseNumbersInString
Enum to wrap bool variable to select if random string should have numbers or not.
Definition: common_utils.hpp:51
nmodl::utils::WithNumbers
@ WithNumbers
Definition: common_utils.hpp:51
nmodl::utils::SingletonRandomString::get_random_string
const std::string & get_random_string(const std::string &var_name) const
Get the random string of the var_name variable.
Definition: common_utils.hpp:94
nmodl::utils::SingletonRandomString::instance
static SingletonRandomString & instance()
Function to instantiate the SingletonRandomString class.
Definition: common_utils.hpp:75
nmodl::utils::WithoutNumbers
@ WithoutNumbers
Definition: common_utils.hpp:51
nmodl::utils::SingletonRandomString::SingletonRandomString
SingletonRandomString()=default
Constructor used by instance()
nmodl::utils::SingletonRandomString::reset_random_string
const std::string & reset_random_string(const std::string &var_name, UseNumbersInString use_numbers)
If var_name has already got a random string assigned remove it from map and assign a new one,...
Definition: common_utils.hpp:105
nmodl::utils::envpathsep
static constexpr char envpathsep
The character conventionally used by the operating system to separate search path components.
Definition: common_utils.hpp:46
nmodl::utils::SingletonRandomString::random_string_exists
bool random_string_exists(const std::string &var_name) const
Check if there is a random string assigned as suffix for the var_name variable.
Definition: common_utils.hpp:85
nmodl::utils::generate_random_string
std::string generate_random_string(const int len, UseNumbersInString use_numbers)
Generate random std::string of length len based on a uniform distribution.
Definition: common_utils.cpp:27