xbmc
SysfsPath.h
1 /*
2  * Copyright (C) 2011-2018 Team Kodi
3  * This file is part of Kodi - https://kodi.tv
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  * See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include "utils/log.h"
12 
13 #include <exception>
14 #include <fstream>
15 #include <optional>
16 #include <string>
17 
19 {
20 public:
21  CSysfsPath() = default;
22  CSysfsPath(const std::string& path) : m_path(path) {}
23  ~CSysfsPath() = default;
24 
25  bool Exists();
26 
27  template<typename T>
28  std::optional<T> Get()
29  {
30  try
31  {
32  std::ifstream file(m_path);
33 
34  T value;
35 
36  file >> value;
37 
38  if (file.bad())
39  {
40  CLog::LogF(LOGERROR, "error reading from '{}'", m_path);
41  return std::nullopt;
42  }
43 
44  return value;
45  }
46  catch (const std::exception& e)
47  {
48  CLog::LogF(LOGERROR, "exception reading from '{}': {}", m_path, e.what());
49  return std::nullopt;
50  }
51  }
52 
53 private:
54  std::string m_path;
55 };
56 
57 template<>
58 std::optional<std::string> CSysfsPath::Get<std::string>();
Definition: SysfsPath.h:18