-
Notifications
You must be signed in to change notification settings - Fork 534
Expand file tree
/
Copy pathutils.hpp
More file actions
188 lines (159 loc) · 6.5 KB
/
Copy pathutils.hpp
File metadata and controls
188 lines (159 loc) · 6.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* SPDX-FileCopyrightText: Copyright (c) 2021-2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <gst/gst.h>
#include <glib.h>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <sys/timeb.h>
#include <functional>
#include <iostream>
#include "gstnvdsmeta.h"
#include "nvdsmeta_schema.h"
#include <memory>
#include <optional>
#include <mutex>
#include <pybind11/cast.h>
#include <pybind11/pybind11.h>
namespace py = pybind11;
namespace pydeepstream {
// rewritting ""_a operator from pybind11 namespace and putting it into
// pydeepstream namespace
pybind11::arg operator ""_a(const char *str, size_t len);
}
namespace pydeepstream {
void bindutils(py::module &m);
}
namespace pydeepstream::utils {
using COPYFUNC_SIG = gpointer(gpointer, gpointer);
using RELEASEFUNC_SIG = void(gpointer, gpointer);
/// Purpose:
/// This templated struct allows storing std::function at compile time
/// in a predetermined memory space, so that a function pointer
/// referencing that std::function can be retrieved and stored
/// somewhere else.
/// This allows storing a function pointer in a C struct
/// which will be used from some callbacks.
/// This struct should not be used if you intend to work only
/// with C++ app, please use directly std::function instead.
/// UniqueName: used to create a unique function_storage
/// RetValue: return value of the function
/// ArgTypes: All function arguments
template<const char *UniqueName, typename RetValue, typename... ArgTypes>
struct function_storage {
public:
/// This allows decoupling std::function from the return value and arguments
typedef std::optional<std::function<RetValue(ArgTypes...)>> function_type;
/// Stores the provided std::function statically in the instanciated templated struct
static void store(const function_type &f) {
const std::lock_guard<std::mutex> lock(instance().mut_);
auto &inst = instance();
if(!inst.stopped_)
inst.fn_ = f;
}
static void
__attribute__((optimize("O0")))
free_instance(){
auto &inst = instance();
const std::lock_guard<std::mutex> lock(inst.mut_);
auto &opt = inst.fn_;
if (opt.has_value()){
opt.reset();
}
inst.stopped_=true;
}
/// Helps defining the actual function pointer needed
static RetValue invoke(ArgTypes... args) {
const std::lock_guard<std::mutex> lock(instance().mut_);
auto &opt = instance().fn_;
// here we check if the function's content is valid before calling it,
// as it can be empty if free instance is called. In that case we return
// the default value of RetValue type. RetValue must have a default
// constructor with no parameters.
if (!opt.has_value())
return RetValue();
auto &fun = opt.value();
if (!fun)
return RetValue();
return fun(args...);
}
/// Declares the type of pointer returned
typedef decltype(&function_storage::invoke) pointer_type;
/// Returns a function pointer that can be stored
static pointer_type get_ptr() { return &invoke; }
private:
static function_storage &instance() {
static function_storage inst_;
return inst_;
}
/// contains a storage for an std::function.
function_type fn_;
std::mutex mut_;
bool stopped_=false;
};
// Is used to keep track of font names without duplicate
// since std::string::c_str() returns as const char*, it cannot be used
// as is. We need a char *, that's the reason why a std::shared_ptr<char>
// is used
extern std::unordered_map<std::string, std::shared_ptr<char>> font_name_memory;
template<typename TYPE, typename FIELDTYPE>
auto get_field_content_lambda(FIELDTYPE member) {
return [member](TYPE *object) {
return reinterpret_cast<size_t>((*object).*member);
};
}
template<typename TYPE, typename FIELDTYPE>
auto set_field_content_string_lambda(FIELDTYPE member) {
return [member](TYPE *object, std::string str) {
auto &map_str = font_name_memory;
const auto &search = map_str.find(str);
if (search == map_str.end()) {
auto tmp_str = std::shared_ptr<char>(
new char[str.size() + 1],
std::default_delete<char[]>());
for (uint i = 0; i < str.size(); ++i)
tmp_str.get()[i] = str[i];
tmp_str.get()[str.size()] = '\0';
map_str[str] = tmp_str;
}
(*object).*member = map_str[str].get();
};
}
template<const char *UniqueName, typename RetValue, typename... ArgTypes>
typename function_storage<UniqueName, RetValue, ArgTypes...>::pointer_type
get_fn_ptr_from_std_function(
const std::function<RetValue(ArgTypes...)> &f) {
typedef function_storage<UniqueName, RetValue, ArgTypes...> custom_fun;
custom_fun::store(f);
return custom_fun::get_ptr();
}
template<const char *UniqueName, typename RetValue, typename... ArgTypes>
typename function_storage<UniqueName, RetValue, ArgTypes...>::pointer_type
__attribute__((optimize("O0")))
free_fn_ptr_from_std_function(const std::function<RetValue(ArgTypes...)> &f) {
typedef function_storage<UniqueName, RetValue, ArgTypes...> custom_fun;
custom_fun::free_instance();
}
void
set_copyfunc(NvDsUserMeta *meta,
std::function<COPYFUNC_SIG> const &func);
void set_freefunc(NvDsUserMeta *meta,
std::function<RELEASEFUNC_SIG> const &func);
void release_all_func();
void generate_ts_rfc3339(char *buf, int buf_size);
}