NVIDIA DeepStream SDK API Reference

9.1 Release
sources/libs/ds3d/gst/custom_lib_factory.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2021-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef NVDS3D_CUSTOMLIB_FACTORY_HPP
19 #define NVDS3D_CUSTOMLIB_FACTORY_HPP
20 
21 #include <dlfcn.h>
22 #include <ds3d/common/common.h>
23 #include <errno.h>
24 
25 #include <functional>
26 #include <iostream>
27 
28 namespace ds3d {
29 
30 template <class T>
31 T*
32 dlsym_ptr(void* handle, char const* name)
33 {
34  return reinterpret_cast<T*>(dlsym(handle, name));
35 }
36 
38 public:
39  CustomLibFactory() = default;
40 
42  {
43  if (_libHandle && !_keepOpen) {
44  LOG_DEBUG("dlclose %s", _libName.c_str());
45  dlclose(_libHandle);
46  }
47  }
48  void keepOpen(bool b) { _keepOpen = b; }
49 
50  template <class CustomRefCtx>
51  CustomRefCtx* CreateCtx(const std::string& libName, const std::string& symName)
52  {
53  if (!_libHandle) {
54  _libName = libName;
55  _libHandle = dlopen(libName.c_str(), RTLD_NOW | RTLD_LOCAL);
56  } else {
58  _libName == libName || libName.empty(), nullptr,
59  "CustomLibFactory existing libname: %s is different from new lib: %s",
60  _libName.c_str(), libName.c_str());
61  }
63  _libHandle, nullptr, "open custome-lib: %s failed. dlerr: %s", _libName.c_str(),
64  dlerror());
65  LOG_INFO("Library Opened Successfully");
66 
67  std::function<CustomRefCtx*()> createCtxFunc =
68  dlsym_ptr<CustomRefCtx*()>(_libHandle, symName.c_str());
70  createCtxFunc, nullptr, "dlsym: %s not found, error: %s", symName.c_str(), dlerror());
71 
72  CustomRefCtx* customCtx = createCtxFunc();
74  customCtx, nullptr, "create custom context failed during call: %s", symName.c_str());
75  LOG_INFO("Custom Context created from %s", symName.c_str());
76  return customCtx;
77  }
78 
79 public:
80  void* _libHandle = nullptr;
81  std::string _libName;
82  bool _keepOpen = false;
83 };
84 
85 } // namespace ds3d
86 
87 #endif
ds3d::dlsym_ptr
T * dlsym_ptr(void *handle, char const *name)
Definition: sources/libs/ds3d/gst/custom_lib_factory.h:32
ds3d::CustomLibFactory::_libHandle
void * _libHandle
Definition: sources/libs/ds3d/gst/custom_lib_factory.h:80
DS3D_FAILED_RETURN
#define DS3D_FAILED_RETURN(condition, ret, fmt,...)
Definition: sources/includes/ds3d/common/defines.h:69
ds3d::CustomLibFactory::CreateCtx
CustomRefCtx * CreateCtx(const std::string &libName, const std::string &symName)
Definition: sources/libs/ds3d/gst/custom_lib_factory.h:51
LOG_DEBUG
#define LOG_DEBUG(fmt,...)
Definition: sources/apps/sample_apps/deepstream-3d-action-recognition/deepstream_action.h:54
ds3d::CustomLibFactory::keepOpen
void keepOpen(bool b)
Definition: sources/libs/ds3d/gst/custom_lib_factory.h:48
ds3d::CustomLibFactory::_libName
std::string _libName
Definition: sources/libs/ds3d/gst/custom_lib_factory.h:81
LOG_INFO
#define LOG_INFO(fmt,...)
Definition: sources/apps/sample_apps/deepstream-3d-action-recognition/custom_sequence_preprocess/seq_process_common.h:68
ds3d::CustomLibFactory::~CustomLibFactory
~CustomLibFactory()
Definition: sources/libs/ds3d/gst/custom_lib_factory.h:41
ds3d::CustomLibFactory::CustomLibFactory
CustomLibFactory()=default
ds3d::CustomLibFactory::_keepOpen
bool _keepOpen
Definition: sources/libs/ds3d/gst/custom_lib_factory.h:82
ds3d::CustomLibFactory
Definition: sources/libs/ds3d/gst/custom_lib_factory.h:37
ds3d
Definition: sources/includes/ds3d/common/abi_dataprocess.h:25