Skip to content
Snippets Groups Projects
Commit 31d44b3b authored by Atkins, Charles Vernon's avatar Atkins, Charles Vernon
Browse files

Add a helper class for symbol binding to dynamic libraries.

parent 34dcdc22
No related branches found
No related tags found
1 merge request!215Add plugin engine
......@@ -18,6 +18,7 @@ add_library(adios2
core/VariableCompound.cpp core/VariableCompound.tcc
#helper
helper/adiosDynamicBinder.h helper/adiosDynamicBinder.cpp
helper/adiosMath.cpp
helper/adiosMPIFunctions.cpp
helper/adiosString.cpp
......
/*
* Distributed under the OSI-approved Apache License, Version 2.0. See
* accompanying file Copyright.txt for details.
*
* DynamicBinder.cpp
*
* Created on: Jul 21, 2017
* Author: Chuck Atkins
*/
#include "adiosDynamicBinder.h"
#include <algorithm> // for copy
#include <iostream> // for operator<<, stringstream, bas...
#include <iterator> // for ostream_iterator
#include <sstream> // for stringstream
#include <stdexcept> // for runtime_error
#include <vector> // for vector
#include <adios2sys/DynamicLoader.hxx>
namespace adios2
{
struct DynamicBinder::Impl
{
adios2sys::DynamicLoader::LibraryHandle m_LibraryHandle;
};
DynamicBinder::DynamicBinder(std::string libName)
{
std::vector<std::string> libPrefixes;
libPrefixes.emplace_back("");
libPrefixes.emplace_back("lib");
#ifdef __CYGWIN__
libPrefixes.emplace_back("cyg");
#endif
std::vector<std::string> libSuffixes;
#ifdef __APPLE__
libSuffixes.emplace_back(".dylib");
libSuffixes.emplace_back(".so");
#endif
#ifdef __hpux
libSuffixes.emplace_back(".sl");
#endif
#ifdef __unix__
libSuffixes.emplace_back(".so");
#endif
#ifdef _WIN32
libSuffixes.emplace_back(".dll");
#endif
std::vector<std::string> searchedLibs;
std::string fileName;
// Test the various combinations of library names
for (const std::string &prefix : libPrefixes)
{
for (const std::string &suffix : libSuffixes)
{
fileName = prefix + libName + suffix;
m_Impl->m_LibraryHandle =
adios2sys::DynamicLoader::OpenLibrary(fileName);
searchedLibs.push_back(fileName);
if (m_Impl->m_LibraryHandle)
{
break;
}
}
if (m_Impl->m_LibraryHandle)
{
break;
}
}
if (!m_Impl->m_LibraryHandle)
{
std::stringstream errString;
errString << "Unable to locate the " << libName
<< " library; searched for ";
std::copy(searchedLibs.begin(), searchedLibs.end(),
std::ostream_iterator<std::string>(errString, " "));
throw std::runtime_error(errString.str());
}
}
DynamicBinder::~DynamicBinder()
{
adios2sys::DynamicLoader::CloseLibrary(m_Impl->m_LibraryHandle);
}
DynamicBinder::VoidSymbolPointer
DynamicBinder::GetSymbol(std::string symbolName)
{
return adios2sys::DynamicLoader::GetSymbolAddress(m_Impl->m_LibraryHandle,
symbolName);
}
};
/*
* Distributed under the OSI-approved Apache License, Version 2.0. See
* accompanying file Copyright.txt for details.
*
* DynamicBinder.h
*
* Created on: Jul 21, 2017
* Author: Chuck Atkins
*/
#ifndef ADIOS2_HELPER_DYNAMICBINDER_H_
#define ADIOS2_HELPER_DYNAMICBINDER_H_
#include <memory>
#include <string>
#include <type_traits>
namespace adios2
{
class DynamicBinder
{
public:
using VoidSymbolPointer = std::add_pointer<void()>::type;
public:
DynamicBinder(std::string libName);
~DynamicBinder();
VoidSymbolPointer GetSymbol(std::string symbolName);
private:
struct Impl;
std::unique_ptr<Impl> m_Impl;
};
}
#endif // ADIOS2_HELPER_DYNAMICBINDER_H_
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment