nx_metadata_sdk  1.0
Metadata SDK
utils.h
1 // Copyright 2018-present Network Optix, Inc. Licensed under MPL 2.0: www.mozilla.org/MPL/2.0/
2 
3 #pragma once
4 
5 #include <string>
6 #include <vector>
7 
8 namespace nx {
9 namespace vms_server_plugins {
10 namespace analytics {
11 namespace stub {
12 
13 bool toBool(std::string str);
14 
15 template<typename T>
16 T clamp(const T& value, const T& lowerBound, const T& upperBound)
17 {
18  if (value < lowerBound)
19  return lowerBound;
20 
21  if (value > upperBound)
22  return upperBound;
23 
24  return value;
25 }
26 
27 std::vector<char> loadFile(const std::string& path);
28 
29 std::string imageFormatFromPath(const std::string& path);
30 
31 bool isHttpOrHttpsUrl(const std::string& path);
32 
33 std::string join(const std::vector<std::string>& strings,
34  const std::string& delimiter,
35  const std::string& itemPrefix = std::string(),
36  const std::string& itemPostfix = std::string());
37 
38 template<typename T>
40 {
41 
42 public:
43  SimpleOptional() = default;
44 
45  SimpleOptional(const T& value):
46  m_value(value),
47  m_isInitialized(true)
48  {
49  }
50 
51  template<typename U>
52  SimpleOptional(const SimpleOptional<U>& other):
53  m_value(other.value()),
54  m_isInitialized(other.isInitialized())
55  {
56  }
57 
58  const T* operator->() const
59  {
60  if (!m_isInitialized)
61  return nullptr;
62 
63  return &m_value;
64  }
65 
66  T* operator->()
67  {
68  if (!m_isInitialized)
69  return nullptr;
70 
71  return &m_value;
72  }
73 
74  const T& operator*() const
75  {
76  return m_value;
77  }
78 
79  T& operator*()
80  {
81  return m_value;
82  }
83 
84  template<typename U>
85  SimpleOptional& operator=(const SimpleOptional<U>& other)
86  {
87  m_value = other.value();
88  m_isInitialized = other.isInitialized();
89 
90  return *this;
91  }
92 
93  template<typename U>
94  SimpleOptional& operator=(U&& value)
95  {
96  m_value = std::forward<U>(value);
97  m_isInitialized = true;
98 
99  return *this;
100  }
101 
102  explicit operator bool() const { return m_isInitialized; }
103 
104  const T& value() const
105  {
106  return m_value;
107  }
108 
109  bool isInitialized() const { return m_isInitialized; }
110 
111  void reset() { m_isInitialized = false; }
112 
113 private:
114  T m_value{};
115  bool m_isInitialized = false;
116 };
117 
118 } // namespace stub
119 } // namespace analytics
120 } // namespace vms_server_plugins
121 } // namespace nx
Definition: apple_utils.h:6