Apollo  6.0
Open source self driving car software
shared_library.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2020 The Apollo Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *****************************************************************************/
16 //
17 // Adapted from poco/Foundation/include/Poco/SharedLibrary.h
18 //
19 // Definition of the SharedLibrary class.
20 //
21 // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
22 // and Contributors.
23 //
24 // SPDX-License-Identifier: BSL-1.0
25 //
26 
27 #ifndef CYBER_CLASS_LOADER_SHARED_LIBRARY_SHARED_LIBRARY_H_
28 #define CYBER_CLASS_LOADER_SHARED_LIBRARY_SHARED_LIBRARY_H_
29 
30 #include <mutex>
31 #include <string>
32 
34 
35 namespace apollo {
36 namespace cyber {
37 namespace class_loader {
38 
39 // The SharedLibrary class dynamically loads shared libraries at run-time.
41  public:
42  enum Flags {
43  // On platforms that use dlopen(), use RTLD_GLOBAL. This is the default
44  // if no flags are given.
46 
47  // On platforms that use dlopen(), use RTLD_LOCAL instead of RTLD_GLOBAL.
48  //
49  // Note that if this flag is specified, RTTI (including dynamic_cast and
50  // throw) will not work for types defined in the shared library with GCC
51  // and possibly other compilers as well. See
52  // http://gcc.gnu.org/faq.html#dso for more information.
54  };
55 
56  // Creates a SharedLibrary object.
57  SharedLibrary() = default;
58 
59  // Destroys the SharedLibrary. The actual library
60  // remains loaded.
61  virtual ~SharedLibrary();
62 
63  // Creates a SharedLibrary object and loads a library
64  // from the given path.
65  explicit SharedLibrary(const std::string& path);
66 
67  // Creates a SharedLibrary object and loads a library
68  // from the given path, using the given flags.
69  // See the Flags enumeration for valid values.
70  SharedLibrary(const std::string& path, int flags);
71 
72  public:
73  // Loads a shared library from the given path.
74  // Throws a LibraryAlreadyLoadedException if
75  // a library has already been loaded.
76  // Throws a LibraryLoadException if the library
77  // cannot be loaded.
78  void Load(const std::string& path);
79 
80  // Loads a shared library from the given path,
81  // using the given flags. See the Flags enumeration
82  // for valid values.
83  // Throws a LibraryAlreadyLoadedException if
84  // a library has already been loaded.
85  // Throws a LibraryLoadException if the library
86  // cannot be loaded.
87  void Load(const std::string& path, int flags);
88 
89  // Unloads a shared library.
90  void Unload();
91 
92  // Returns true iff a library has been loaded.
93  bool IsLoaded();
94 
95  // Returns true iff the loaded library contains
96  // a symbol with the given name.
97  bool HasSymbol(const std::string& name);
98 
99  // Returns the address of the symbol with
100  // the given name. For functions, this
101  // is the entry point of the function.
102  // Throws a SymbolNotFoundException if the
103  // symbol does not exist.
104  void* GetSymbol(const std::string& name);
105 
106  // Returns the path of the library, as specified in a call
107  // to load() or the constructor.
108  inline const std::string& GetPath() const { return path_; }
109 
110  public:
111  SharedLibrary(const SharedLibrary&) = delete;
112  SharedLibrary& operator=(const SharedLibrary&) = delete;
113 
114  private:
115  void* handle_ = nullptr;
116  std::string path_;
117  static std::mutex mutex_;
118 };
119 
120 } // namespace class_loader
121 } // namespace cyber
122 } // namespace apollo
123 
124 #endif // CYBER_CLASS_LOADER_SHARED_LIBRARY_SHARED_LIBRARY_H_
void * GetSymbol(const std::string &name)
void Load(const std::string &path)
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
Flags
Definition: shared_library.h:42
SharedLibrary & operator=(const SharedLibrary &)=delete
bool HasSymbol(const std::string &name)
const std::string & GetPath() const
Definition: shared_library.h:108
Definition: shared_library.h:40