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