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