Added count() and random_choice() functions
This commit is contained in:
		
							
								
								
									
										12
									
								
								plugin.hpp
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								plugin.hpp
									
									
									
									
									
								
							| @@ -14,6 +14,7 @@ | ||||
| #include <map> | ||||
| #include <functional> | ||||
| #include <iostream> | ||||
| #include <random> | ||||
| #include <dlfcn.h> | ||||
| #include <dirent.h> | ||||
| #include <libscerror.hpp> | ||||
| @@ -59,10 +60,18 @@ namespace sc { | ||||
| 				return result; | ||||
| 			} | ||||
|  | ||||
| 			static size_t count() { return plugins.size(); } | ||||
|  | ||||
| 			static plugin& random_choice() { | ||||
| 				static std::random_device rdev {}; | ||||
| 				static std::default_random_engine reng {rdev()}; | ||||
| 				std::uniform_int_distribution<int> dist {0, count() - 1}; | ||||
| 				return get(names()[dist(reng)]); | ||||
| 			} | ||||
|  | ||||
| 			plugin(const std::string& path) : _path {path} { | ||||
| 				_plugin = ::dlopen(path.c_str(), RTLD_LAZY); | ||||
|  | ||||
| 			//std::cerr << path << (_plugin ? "" : " not") << " opened\n"; | ||||
| 				const char* err {::dlerror()}; | ||||
| 				if (err) std::cerr << err << '\n'; | ||||
| 				if (_plugin) { | ||||
| @@ -77,7 +86,6 @@ namespace sc { | ||||
| 				if (_plugin) { | ||||
| 					::dlclose(_plugin); | ||||
| 					_plugin = nullptr; | ||||
| 				//std::cerr << _path << " closed\n"; | ||||
| 				} | ||||
| 			} | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Bob Polis
					Bob Polis