eDSP  0.0.1
A cross-platform DSP library written in C++.
system.hpp
Go to the documentation of this file.
1 /*
2 * eDSP, A cross-platform Digital Signal Processing library written in modern C++.
3 * Copyright (C) 2018 Mohammed Boujemaoui Boulaghmoudi, All rights reserved.
4 *
5 * This program is free software: you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation, either version 3 of the License, or (at your option)
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along width
16 * this program. If not, see <http://www.gnu.org/licenses/>
17 *
18 * Filename: system.hpp
19 * Author: Mohammed Boujemaoui
20 * Date: 07/10/18
21 */
22 
23 #ifndef EDSP_SYSTEM_HPP
24 #define EDSP_SYSTEM_HPP
25 
26 #include <edsp/core/internal/config.hpp>
28 #include <edsp/types/expected.hpp>
29 #include <edsp/meta/expects.hpp>
30 #include <edsp/meta/is_null.hpp>
31 #include <edsp/meta/empty.hpp>
32 #include <mutex>
33 
34 namespace edsp { inline namespace core {
35 
39  enum class systems {
40  windows,
41  linux,
42  freeBSD,
43  macOS,
44  iOS,
45  android,
46  unknown
47  };
48 
49  inline logger& operator<<(logger& stream, systems sys) {
50  switch (sys) {
51  case systems::windows:
52  return stream << "Microsoft Windows OS";
53  case systems::linux:
54  return stream << "Linux distribution";
55  case systems::freeBSD:
56  return stream << "Free BSD distribution";
57  case systems::macOS:
58  return stream << "Mac OS X system";
59  case systems::iOS:
60  return stream << "iOS system";
61  case systems::android:
62  return stream << "Android system";
63  default:
64  return stream << edsp::red << "unknown" << edsp::endc;
65  }
66  }
67 
71  enum class processors {
72  x86,
73  x64,
74  arm,
75  unknown
76  };
77 
78  inline logger& operator<<(logger& stream, processors proc) {
79  switch (proc) {
80  case processors::x86:
81  return stream << "x86 architecture";
82  case processors::x64:
83  return stream << "x64 architecture";
84  case processors::arm:
85  return stream << "arm architecture";
86  default:
87  return stream << edsp::red << "unknown" << edsp::endc;
88  }
89  }
90 
95  enum class compilers {
96  gcc,
97  clang,
98  mvsc,
99  unknown
100  };
101 
102  inline logger& operator<<(logger& stream, compilers comp) {
103  switch (comp) {
104  case compilers::gcc:
105  return stream << "gcc";
106  case compilers::clang:
107  return stream << "clang";
108  case compilers::mvsc:
109  return stream << "mvsc";
110  default:
111  return stream << edsp::red << "unknown" << edsp::endc;
112  }
113  }
114 
115  struct system_info {
116  // clang-format off
117  static constexpr compilers compiler() noexcept {
118  #if defined(COMPILER_GNU) && !defined(COMPILER_CLANG)
119  return compilers::gcc;
120  #elif defined(COMPILER_CLANG)
121  return compilers::clang;
122  #elif defined(COMPILER_MVSC)
123  return compilers::mvsc;
124  #else
125  return compilers::unknown;
126  #endif
127  }
128  // clang-format on
129 
130  // clang-format off
131  static constexpr systems os() noexcept {
132  #if defined(OS_WINDOWS)
133  return systems::windows;
134  #elif defined(OS_LINUX)
135  return systems::linux;
136  #elif defined(OS_MACOS)
137  return systems::macOS;
138  #elif defined(OS_IOS)
139  return systems::iOS;
140  #elif defined(OS_FREEBSD)
141  return systems::freeBSD;
142  #elif defined(OS_ANDROID)
143  return systems::android;
144  #else
145  return systems::unknown;
146  #endif
147  }
148  // clang-format on
149 
150  // clang-format off
151  static constexpr processors processor() noexcept {
152  #if defined(PROCESSOR_X86_32)
153  return processors::x86;
154  #elif defined(PROCESSOR_X86_64)
155  return processors::x64;
156  #elif defined(PROCESSOR_ARM)
157  return processors::arm;
158  #else
159  return processors::unknown;
160  #endif
161  }
162  // clang-format on
163  };
164 
165  struct system_env {
171  NoError = 0,
174  InvalidArgument = EINVAL,
175  NoMemory = ENOMEM,
176  Unknown = -1
177  };
178 
184  static edsp::expected<std::string, SystemEnvironmentError>
185  get_env(const edsp::string_view& variable_name) noexcept {
186  std::lock_guard<std::mutex> lock(mutex());
187  const char* env_p = std::getenv(variable_name.data());
188  if (meta::empty(variable_name)) {
189  return edsp::make_unexpected(SystemEnvironmentError::Empty);
190  } else {
191  if (!meta::is_null(env_p)) {
192  return env_p;
193  } else {
194  return edsp::make_unexpected(SystemEnvironmentError::NotFound);
195  }
196  }
197  }
198 
206  static SystemEnvironmentError set_env(const edsp::string_view& variable_name,
207  const edsp::string_view& variable_value, bool overwrite = true) noexcept {
208  std::lock_guard<std::mutex> lock(mutex());
209  if (meta::empty(variable_name)) {
210  return SystemEnvironmentError::Empty;
211  } else {
212  const auto result = setenv(meta::data(variable_name), meta::data(variable_value), overwrite);
213  return static_cast<SystemEnvironmentError>(result);
214  }
215  }
216 
222  static bool exist(const edsp::string_view& variable_name) noexcept {
223  std::lock_guard<std::mutex> lock(mutex());
224  return !meta::is_null(std::getenv(meta::data(variable_name)));
225  }
226 
227  private:
234  static std::mutex& mutex() {
235  static std::mutex environment_mutex;
236  return environment_mutex;
237  }
238  };
239 
240 }} // namespace edsp::core
241 
242 #endif //EDSP_SYSTEM_HPP
static bool exist(const edsp::string_view &variable_name) noexcept
Checks if a variable name is defined in the system environment.
Definition: system.hpp:222
processors
Represents the architecture of the processor running the library.
Definition: system.hpp:71
Definition: system.hpp:115
Definition: system.hpp:173
systems
Represents the OS running the library.
Definition: system.hpp:39
static constexpr processors processor() noexcept
Definition: system.hpp:151
Definition: system.hpp:172
logger & red(logger &stream)
Updates the logger output color to red.
Definition: logger.hpp:325
Definition: system.hpp:165
static edsp::expected< std::string, SystemEnvironmentError > get_env(const edsp::string_view &variable_name) noexcept
Searches in the system environment for the value of the variable name.
Definition: system.hpp:185
SystemEnvironmentError
Definition: system.hpp:170
static constexpr systems os() noexcept
Definition: system.hpp:131
static SystemEnvironmentError set_env(const edsp::string_view &variable_name, const edsp::string_view &variable_value, bool overwrite=true) noexcept
Shall update or add a variable in the environment of the calling process.
Definition: system.hpp:206
Definition: logger.hpp:40
static constexpr compilers compiler() noexcept
Definition: system.hpp:117
logger & endc(logger &stream)
End of color, styled streaming.
Definition: logger.hpp:407
compilers
Represents the type of Operative System running the library.
Definition: system.hpp:95
Definition: amplifier.hpp:29
logger & operator<<(logger &stream, fft_lib lib)
Definition: library_info.hpp:38