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 template<typename T>
46 class SimpleOptional
47 {
48
49 public:
50 SimpleOptional() = default;
51
52 SimpleOptional(const T& value):
53 m_value(value),
54 m_isInitialized(true)
55 {
56 }
57
58 template<typename U>
59 SimpleOptional(const SimpleOptional<U>& other):
60 m_value(other.value()),
61 m_isInitialized(other.isInitialized())
62 {
63 }
64
65 const T* operator->() const
66 {
67 if (!m_isInitialized)
68 return nullptr;
69
70 return &m_value;
71 }
72
73 T* operator->()
74 {
75 if (!m_isInitialized)
76 return nullptr;
77
78 return &m_value;
79 }
80
81 const T& operator*() const
82 {
83 return m_value;
84 }
85
86 T& operator*()
87 {
88 return m_value;
89 }
90
91 template<typename U>
92 SimpleOptional& operator=(const SimpleOptional<U>& other)
93 {
94 m_value = other.value();
95 m_isInitialized = other.isInitialized();
96
97 return *this;
98 }
99
100 template<typename U>
101 SimpleOptional& operator=(U&& value)
102 {
103 m_value = std::forward<U>(value);
104 m_isInitialized = true;
105
106 return *this;
107 }
108
109 explicit operator bool() const { return m_isInitialized; }
110
111 const T& value() const
112 {
113 return m_value;
114 }
115
116 bool isInitialized() const { return m_isInitialized; }
117
118 void reset() { m_isInitialized = false; }
119
120 private:
121 T m_value{};
122 bool m_isInitialized = false;
123 };
124
125 std::string substituteAllTemplateVariables(const std::string& text,
126 const std::unordered_map<std::string, std::string>& templateSubstitutionMap);
127
128 } // namespace stub
129 } // namespace analytics
130 } // namespace vms_server_plugins
131 } // namespace nx
nx::vms_server_plugins::analytics::stub::SimpleOptional
Definition: utils.h:46
nx
Definition: apple_utils.h:6