Skip to content

Commit

Permalink
Merge pull request #61 from robotology/add-exists-tool
Browse files Browse the repository at this point in the history
Add blockfactory-exists command line tool
  • Loading branch information
diegoferigo authored Feb 11, 2021
2 parents b4d0810 + e36a10b commit d5b5640
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
4 changes: 3 additions & 1 deletion sources/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ if(USES_MATLAB)
add_subdirectory(Simulink)
endif()

add_subdirectory(Tools)

# ========================
# MAIN BLOCKFACTORY TARGET
# ========================

# Dummy target
add_library(BlockFactory INTERFACE)

set(BLOCKFACTORY_DEPENDENCIES BlockFactoryCore BlockFactorySimulinkCoder)
set(BLOCKFACTORY_DEPENDENCIES BlockFactoryCore BlockFactorySimulinkCoder BlockFactoryTools)
if (USES_MATLAB)
list(APPEND BLOCKFACTORY_DEPENDENCIES BlockFactorySimulink)
endif()
Expand Down
29 changes: 29 additions & 0 deletions sources/Tools/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright (C) Istituto Italiano di Tecnologia (IIT). All rights reserved.
# This software may be modified and distributed under the terms of the
# GNU Lesser General Public License v2.1 or any later version.

set(BLOCKFACTORY_EXISTS_SRC
src/BlockfactoryExists.cpp)

add_executable(blockfactory-exists ${BLOCKFACTORY_EXISTS_SRC})

target_link_libraries(blockfactory-exists PRIVATE BlockFactory::Core)

target_compile_warnings(blockfactory-exists
WARNINGS_AS_ERRORS ${TREAT_WARNINGS_AS_ERRORS}
DEPENDS ENABLE_WARNINGS)

install(
TARGETS blockfactory-exists
EXPORT BlockFactoryToolsExport
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})

install_basic_package_files(BlockFactoryTools
VERSION ${PROJECT_VERSION}
COMPATIBILITY AnyNewerVersion
EXPORT BlockFactoryToolsExport
FIRST_TARGET blockfactory-exists
NAMESPACE BlockFactory::
NO_CHECK_REQUIRED_COMPONENTS_MACRO)
51 changes: 51 additions & 0 deletions sources/Tools/src/BlockfactoryExists.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (C) Istituto Italiano di Tecnologia (IIT)
* All rights reserved.
*
* This software may be modified and distributed under the terms of the
* GNU Lesser General Public License v2.1 or any later version.
*/

#include <cstdlib>
#include <iostream>

#include "BlockFactory/Core/Block.h"
#include "BlockFactory/Core/FactorySingleton.h"

int main(int argc, char* argv[])
{
std::string commandName = "blockfactory-exists";
if (argc != 3)
{
std::cout << commandName << ": Utility to check for the existence "
<< "of a given block inside a given plugin." << std::endl;
std::cout << "USAGE : " << commandName << " pluginName blockName" << std::endl;
std::cout << " : Note that the pluginName should be specified without prefix (lib) or suffix (.dll, .so, .dylib)." << std::endl;

return EXIT_FAILURE;
}

// Get the class name and the library name from the parameter
const std::string blockName(argv[2]);
const std::string pluginName(argv[1]);

// Get the block factory
auto factory = blockfactory::core::ClassFactorySingleton::getInstance().getClassFactory(
{pluginName, blockName});

if (!factory) {
std::cerr << "ERROR: Failed to get factory object (blockName=" << blockName
<< ",pluginName=" << pluginName << ")";
return EXIT_FAILURE;
}

if (!factory->isValid()) {
std::cerr << "ERROR: Factory error (" << static_cast<std::uint32_t>(factory->getStatus())
<< "): " << factory->getError().c_str();
return EXIT_FAILURE;
}

std::cout << "SUCCESS: Block \"" << blockName
<< "\" found and loaded from plugin \"" << pluginName << "\"." << std::endl;
return EXIT_SUCCESS;
}

0 comments on commit d5b5640

Please sign in to comment.