Apache Mesos
dynamiclibrary.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use this file except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 
13 #ifndef __STOUT_WINDOWS_DYNAMICLIBRARY_HPP__
14 #define __STOUT_WINDOWS_DYNAMICLIBRARY_HPP__
15 
16 #include <string>
17 
18 #include <stout/nothing.hpp>
19 #include <stout/option.hpp>
20 #include <stout/stringify.hpp>
21 #include <stout/try.hpp>
22 
23 #include <stout/windows/error.hpp>
24 
29 class DynamicLibrary
30 {
31 public:
32  DynamicLibrary() : handle_(nullptr) { }
33 
34  // Since this class manages a naked handle it cannot be copy- or
35  // move-constructed.
36  // TODO(bbannier): Allow for move-construction.
37  DynamicLibrary(const DynamicLibrary&) = delete;
38  DynamicLibrary(DynamicLibrary&&) = delete;
39 
40  virtual ~DynamicLibrary()
41  {
42  if (handle_ != nullptr) {
43  close();
44  }
45  }
46 
47  Try<Nothing> open(const std::string& path)
48  {
49  // Check if we've already opened a library.
50  if (handle_ != nullptr) {
51  return Error("Library already opened");
52  }
53 
54  handle_ = ::LoadLibraryW(wide_stringify(path).data());
55 
56  if (handle_ == nullptr) {
57  return WindowsError("Could not load library '" + path + "'");
58  }
59 
60  path_ = path;
61 
62  return Nothing();
63  }
64 
66  {
67  if (handle_ == nullptr) {
68  return Error("Could not close library; handle was already `nullptr`");
69  }
70 
71  if (!::FreeLibrary(handle_)) {
72  return WindowsError(
73  "Could not close library '" + (path_.isSome() ? path_.get() : ""));
74  }
75 
76  handle_ = nullptr;
77  path_ = None();
78 
79  return Nothing();
80  }
81 
82  Try<void*> loadSymbol(const std::string& name)
83  {
84  if (handle_ == nullptr) {
85  return Error(
86  "Could not get symbol '" + name + "'; library handle was `nullptr`");
87  }
88 
89  void* symbol = ::GetProcAddress(handle_, name.c_str());
90 
91  if (symbol == nullptr) {
92  return WindowsError(
93  "Error looking up symbol '" + name + "' in '" +
94  (path_.isSome() ? path_.get() : ""));
95  }
96 
97  return symbol;
98  }
99 
100 private:
101  HMODULE handle_;
102  Option<std::string> path_;
103 };
104 
105 #endif // __STOUT_WINDOWS_DYNAMICLIBRARY_HPP__
Try< Nothing > open(const std::string &path)
Definition: dynamiclibrary.hpp:47
Definition: nothing.hpp:16
Definition: errorbase.hpp:35
Definition: try.hpp:34
Definition: error.hpp:106
Try< void * > loadSymbol(const std::string &name)
Definition: dynamiclibrary.hpp:82
bool isSome() const
Definition: option.hpp:115
const T & get() const &
Definition: option.hpp:118
DynamicLibrary is a very simple wrapper around the programming interface to the dynamic linking loade...
Definition: dynamiclibrary.hpp:28
Definition: none.hpp:27
DynamicLibrary()
Definition: dynamiclibrary.hpp:32
Try< Nothing > close()
Definition: dynamiclibrary.hpp:64
virtual ~DynamicLibrary()
Definition: dynamiclibrary.hpp:40
constexpr const char * name
Definition: shell.hpp:41