NVIDIA DeepStream SDK API Reference

6.4 Release
custom_lib_factory.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2021-2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: LicenseRef-NvidiaProprietary
4  *
5  * NVIDIA CORPORATION, its affiliates and licensors retain all intellectual
6  * property and proprietary rights in and to this material, related
7  * documentation and any modifications thereto. Any use, reproduction,
8  * disclosure or distribution of this material and related documentation
9  * without an express license agreement from NVIDIA CORPORATION or
10  * its affiliates is strictly prohibited.
11  */
12 
13 #ifndef NVDS3D_CUSTOMLIB_FACTORY_HPP
14 #define NVDS3D_CUSTOMLIB_FACTORY_HPP
15 
16 #include <dlfcn.h>
17 #include <ds3d/common/common.h>
18 #include <errno.h>
19 
20 #include <functional>
21 #include <iostream>
22 
23 namespace ds3d {
24 
25 template <class T>
26 T*
27 dlsym_ptr(void* handle, char const* name)
28 {
29  return reinterpret_cast<T*>(dlsym(handle, name));
30 }
31 
33 public:
34  CustomLibFactory() = default;
35 
37  {
38  if (_libHandle) {
39  dlclose(_libHandle);
40  }
41  }
42 
43  template <class CustomRefCtx>
44  CustomRefCtx* CreateCtx(const std::string& libName, const std::string& symName)
45  {
46  if (!_libHandle) {
47  _libName = libName;
48  _libHandle = dlopen(libName.c_str(), RTLD_NOW | RTLD_LOCAL);
49  } else {
51  _libName == libName || libName.empty(), nullptr,
52  "CustomLibFactory existing libname: %s is different from new lib: %s",
53  _libName.c_str(), libName.c_str());
54  }
56  _libHandle, nullptr, "open custome-lib: %s failed. dlerr: %s", _libName.c_str(),
57  dlerror());
58  LOG_INFO("Library Opened Successfully");
59 
60  std::function<CustomRefCtx*()> createCtxFunc =
61  dlsym_ptr<CustomRefCtx*()>(_libHandle, symName.c_str());
63  createCtxFunc, nullptr, "dlsym: %s not found, error: %s", symName.c_str(), dlerror());
64 
65  CustomRefCtx* customCtx = createCtxFunc();
67  customCtx, nullptr, "create custom context failed during call: %s", symName.c_str());
68  LOG_INFO("Custom Context created from %s", symName.c_str());
69  return customCtx;
70  }
71 
72 public:
73  void* _libHandle = nullptr;
74  std::string _libName;
75 };
76 
77 } // namespace ds3d
78 
79 #endif
ds3d::CustomLibFactory::CreateCtx
CustomRefCtx * CreateCtx(const std::string &libName, const std::string &symName)
Definition: custom_lib_factory.h:44
ds3d::CustomLibFactory::_libName
std::string _libName
Definition: custom_lib_factory.h:74
LOG_INFO
#define LOG_INFO
Definition: logging.h:19
common.h
ds3d::CustomLibFactory::~CustomLibFactory
~CustomLibFactory()
Definition: custom_lib_factory.h:36
ds3d::CustomLibFactory::CustomLibFactory
CustomLibFactory()=default
DS3D_FAILED_RETURN
#define DS3D_FAILED_RETURN(condition, ret, fmt,...)
Definition: defines.h:64
ds3d::CustomLibFactory::_libHandle
void * _libHandle
Definition: custom_lib_factory.h:73
ds3d::CustomLibFactory
Definition: custom_lib_factory.h:32
ds3d
Definition: lidar_3d_datatype.h:33
ds3d::dlsym_ptr
T * dlsym_ptr(void *handle, char const *name)
Definition: custom_lib_factory.h:27