libAlgAudio  v1.99-440-g08538e5-dirty
The development library for AlgAudio framework.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
LibLoader.hpp
Go to the documentation of this file.
1 #ifndef LIBLOADER_HPP
2 #define LIBLOADER_HPP
3 /*
4 This file is part of AlgAudio.
5 
6 AlgAudio, Copyright (C) 2015 CeTA - Audiovisual Technology Center
7 
8 AlgAudio is free software: you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as
10 published by the Free Software Foundation, either version 3 of the
11 License, or (at your option) any later version.
12 
13 AlgAudio is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Lesser General Public License for more details.
17 
18 You should have received a copy of the GNU Lesser General Public License
19 along with AlgAudio. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 #include <string>
23 #include <map>
24 #include <memory>
25 
26 #ifndef __unix__
27  #include <windows.h>
28 #endif
29 
30 #include "Module.hpp"
31 #include "Utilities.hpp"
32 
33 namespace AlgAudio{
34 
35 typedef void* (create_instance_func_t)(const char *);
36 typedef void (deleter_t)(void*);
37 
38 namespace Exceptions{
39 struct LibLoading : public Exception{
40  LibLoading(std::string p, std::string t) : Exception(t), path(p) {};
41  virtual std::string what() override {return "When loading file '" + path + "': " + text;}
42  std::string path;
43 };
44 } // namespace Exceptions
45 
46 /* This class is a wrapper for dynamically loadable libraries.
47  * The static part of this class opens up and loads dynamic libraries, while
48  * the instantiable part requests the Module instances from an AA file.
49  */
50 class LibLoader{
51 public:
53  std::shared_ptr<Module> AskForInstance(std::string);
55  deleter_t* GetDeleter() {return deleter_func;}
58  static std::shared_ptr<LibLoader> GetByPath(std::string path);
59 
60  ~LibLoader();
61 private:
62  LibLoader(std::string);
64  static std::shared_ptr<LibLoader> Preload(std::string path);
66  const std::string path;
67 
69  create_instance_func_t* create_instance_func = nullptr;
70  deleter_t* deleter_func = nullptr;
71 
72 #ifdef __unix__
73  void* handle;
74 #else
75  HINSTANCE hLib;
76 #endif
77 
78  // Library cache.
79  static std::map<std::string, std::shared_ptr<LibLoader>> libs_by_path;
80 };
81 
82 } // namespace AlgAudio
83 #endif // LIBLOADER_HPP
std::shared_ptr< Module > AskForInstance(std::string)
virtual std::string what() override
Definition: LibLoader.hpp:41
std::string path
Definition: LibLoader.hpp:42
static std::shared_ptr< LibLoader > GetByPath(std::string path)
LibLoading(std::string p, std::string t)
Definition: LibLoader.hpp:40
Definition: Alertable.hpp:26
Definition: LibLoader.hpp:39
deleter_t * GetDeleter()
Definition: LibLoader.hpp:55
std::string text
Definition: Exception.hpp:42
Definition: LibLoader.hpp:50
void *( create_instance_func_t)(const char *)
Definition: LibLoader.hpp:35
void( deleter_t)(void *)
Definition: LibLoader.hpp:36
Definition: Exception.hpp:29