• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 The Dawn Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef COMMON_DYNAMICLIB_H_
16 #define COMMON_DYNAMICLIB_H_
17 
18 #include "common/Assert.h"
19 
20 #include <string>
21 #include <type_traits>
22 
23 class DynamicLib {
24   public:
25     DynamicLib() = default;
26     ~DynamicLib();
27 
28     DynamicLib(const DynamicLib&) = delete;
29     DynamicLib& operator=(const DynamicLib&) = delete;
30 
31     DynamicLib(DynamicLib&& other);
32     DynamicLib& operator=(DynamicLib&& other);
33 
34     bool Valid() const;
35 
36     bool Open(const std::string& filename, std::string* error = nullptr);
37     void Close();
38 
39     void* GetProc(const std::string& procName, std::string* error = nullptr) const;
40 
41     template <typename T>
42     bool GetProc(T** proc, const std::string& procName, std::string* error = nullptr) const {
43         ASSERT(proc != nullptr);
44         static_assert(std::is_function<T>::value, "");
45 
46         *proc = reinterpret_cast<T*>(GetProc(procName, error));
47         return *proc != nullptr;
48     }
49 
50   private:
51     void* mHandle = nullptr;
52 };
53 
54 #endif  // COMMON_DYNAMICLIB_H_
55