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 bool startsWith(const std::string& str, const std::string& prefix);
16
17 template<typename T>
18 T clamp(const T& value, const T& lowerBound, const T& upperBound)
19 {
20 if (value < lowerBound)
21 return lowerBound;
22
23 if (value > upperBound)
24 return upperBound;
25
26 return value;
27 }
28
29 std::vector<char> loadFile(const std::string& path);
30
31 std::string imageFormatFromPath(const std::string& path);
32
33 bool isHttpOrHttpsUrl(const std::string& path);
34
35 std::string join(const std::vector<std::string>& strings,
36 const std::string& delimiter,
37 const std::string& itemPrefix = std::string(),
38 const std::string& itemPostfix = std::string());
39
40 template<typename T>
41 class SimpleOptional
42 {
43
44 public:
45 SimpleOptional() = default;
46
47 SimpleOptional(const T& value):
48 m_value(value),
49 m_isInitialized(true)
50 {
51 }
52
53 template<typename U>
54 SimpleOptional(const SimpleOptional<U>& other):
55 m_value(other.value()),
56 m_isInitialized(other.isInitialized())
57 {
58 }
59
60 const T* operator->() const
61 {
62 if (!m_isInitialized)
63 return nullptr;
64
65 return &m_value;
66 }
67
68 T* operator->()
69 {
70 if (!m_isInitialized)
71 return nullptr;
72
73 return &m_value;
74 }
75
76 const T& operator*() const
77 {
78 return m_value;
79 }
80
81 T& operator*()
82 {
83 return m_value;
84 }
85
86 template<typename U>
87 SimpleOptional& operator=(const SimpleOptional<U>& other)
88 {
89 m_value = other.value();
90 m_isInitialized = other.isInitialized();
91
92 return *this;
93 }
94
95 template<typename U>
96 SimpleOptional& operator=(U&& value)
97 {
98 m_value = std::forward<U>(value);
99 m_isInitialized = true;
100
101 return *this;
102 }
103
104 explicit operator bool() const { return m_isInitialized; }
105
106 const T& value() const
107 {
108 return m_value;
109 }
110
111 bool isInitialized() const { return m_isInitialized; }
112
113 void reset() { m_isInitialized = false; }
114
115 private:
116 T m_value{};
117 bool m_isInitialized = false;
118 };
119
120 } // namespace stub
121 } // namespace analytics
122 } // namespace vms_server_plugins
123 } // namespace nx
nx::vms_server_plugins::analytics::stub::SimpleOptional
Definition: utils.h:41
nx
Definition: apple_utils.h:6