Skip to content

Commit

Permalink
initial version of plugin based storage api
Browse files Browse the repository at this point in the history
  • Loading branch information
Karsten1987 committed Jul 18, 2018
1 parent 779eaae commit b2e20ac
Show file tree
Hide file tree
Showing 13 changed files with 582 additions and 10 deletions.
58 changes: 48 additions & 10 deletions rosbag2_storage/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,57 @@ endif()

# find dependencies
find_package(ament_cmake REQUIRED)
# uncomment the following section in order to fill in
# further dependencies manually.
# find_package(<dependency> REQUIRED)
find_package(pluginlib REQUIRED)

add_library(
rosbag2_storage
SHARED
src/storage_factory.cpp)
target_include_directories(rosbag2_storage PUBLIC include)
ament_target_dependencies(
rosbag2_storage
"pluginlib")

# Causes the visibility macros to use dllexport rather than dllimport,
# which is appropriate when building the dll but not consuming it.
target_compile_definitions(rosbag2_storage PRIVATE "ROSBAG2_STORAGE_BUILDING_DLL")

install(
DIRECTORY include/
DESTINATION include)

install(
TARGETS rosbag2_storage
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin)

install(
DIRECTORY cmake
DESTINATION share/${PROJECT_NAME}/cmake)

if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
# the following line skips the linter which checks for copyrights
# remove the line when a copyright and license is present in all source files
set(ament_cmake_copyright_FOUND TRUE)
# the following line skips cpplint (only works in a git repo)
# remove the line when this package is a git repo
set(ament_cmake_cpplint_FOUND TRUE)
ament_lint_auto_find_test_dependencies()

add_library(test_plugin SHARED test/test_plugin.cpp)
target_link_libraries(test_plugin rosbag2_storage)
install(
TARGETS test_plugin
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin)
pluginlib_export_plugin_description_file(rosbag2_storage test/test_plugin.xml)

ament_add_gtest(
test_storage_factory
test/test_storage_factory.cpp
)
target_include_directories(test_storage_factory PRIVATE include)
target_link_libraries(test_storage_factory rosbag2_storage)
endif()

ament_package()
ament_export_include_directories(include)
ament_export_libraries(rosbag2_storage)
ament_package(
CONFIG_EXTRAS cmake/rosbag2_storage_register_storage_plugin.cmake)
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Copyright 2018 Open Source Robotics Foundation, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

find_package(ament_cmake_core REQUIRED)

macro(rosbag2_storage_register_storage_plugin target)
if(NOT TARGET ${target})
message(
FATAL_ERROR
"rosbag2_storage_register_storage_plugin() first argument "
"'${target}' is not a target")
endif()

cmake_parse_arguments(_ARG "SKIP_INSTALL" "" "" ${ARGN})

get_target_property(_target_type ${target} TYPE)
if(NOT _target_type STREQUAL "SHARED_LIBRARY")
message(
FATAL_ERROR
"rosbag2_storage_register_storage_plugin() first argument "
"'${target}' is not a shared library target")
endif()
set(_unique_names)
foreach(_arg ${_ARG_UNPARSED_ARGUMENTS})
if(_arg IN_LIST _unique_names)
message(
FATAL_ERROR
"rosbag2_storage_register_storage_plugin() the plugin names "
"must be unique (multiple '${_arg}')")
endif()
list(APPEND _unique_names "${_arg}")

if(_ARG_SKIP_INSTALL)
set(_library_path $<TARGET_FILE:${target}>)
else()
if(WIN32)
set(_path "bin")
else()
set(_path "lib")
endif()
set(_library_path ${_path}/$<TARGET_FILE_NAME:${target}>)
endif()
set(_STORAGE_PLUGINS
"${_STORAGE_PLUGINS}${_arg};${_library_path}\n")
endforeach()

if(_ARG_SKIP_INSTALL)
ament_index_register_resource("rosbag2_storage_plugins" CONTENT ${_STORAGE_PLUGINS} AMENT_INDEX_BINARY_DIR "${CMAKE_BINARY_DIR}/ament_cmake_index_$<CONFIG>" SKIP_INSTALL)
else()
ament_index_register_resource("rosbag2_storage_plugins" CONTENT ${_STORAGE_PLUGINS})
endif()
endmacro()
52 changes: 52 additions & 0 deletions rosbag2_storage/include/rosbag2_storage/storage_factory.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2018 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef ROSBAG2_STORAGE__STORAGE_FACTORY_HPP_
#define ROSBAG2_STORAGE__STORAGE_FACTORY_HPP_

#include <memory>
#include <string>

#include "rosbag2_storage/storage_interface.hpp"
#include "rosbag2_storage/visibility_control.h"

namespace rosbag2_storage
{

class StorageFactoryImpl;

/// Factory to create instances for various storage interfaces
class StorageFactory
{
public:
explicit StorageFactory(const std::string & storage_identifier);

virtual ~StorageFactory();

/// Static function to create \sa StorageInterface given the storage_identifier
/**
* The function returns the StorageInterface base class for a specific identifier.
* \param storage_identifier unique string indicating which storage format to use.
*/
ROSBAG2_STORAGE_PUBLIC
std::shared_ptr<StorageInterface>
get_storage_interface();

private:
std::unique_ptr<StorageFactoryImpl> impl_;
};

} // namespace rosbag2_storage

#endif // ROSBAG2_STORAGE__STORAGE_FACTORY_HPP_
33 changes: 33 additions & 0 deletions rosbag2_storage/include/rosbag2_storage/storage_handle.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2018 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef ROSBAG2_STORAGE__STORAGE_HANDLE_HPP_
#define ROSBAG2_STORAGE__STORAGE_HANDLE_HPP_

#include <string>

#include "rosbag2_storage/visibility_control.h"

namespace rosbag2_storage
{

typedef struct ROSBAG2_STORAGE_PUBLIC_TYPE StorageHandle
{
std::string storage_identifier;
std::string file_path;
} StorageHandle;

} // rosbag2_storage

#endif // ROSBAG2_STORAGE__STORAGE_HANDLE_HPP_
78 changes: 78 additions & 0 deletions rosbag2_storage/include/rosbag2_storage/storage_interface.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright 2018 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef ROSBAG2_STORAGE__STORAGE_INTERFACE_HPP_
#define ROSBAG2_STORAGE__STORAGE_INTERFACE_HPP_

#include <string>

#include "rosbag2_storage/storage_handle.hpp"
#include "rosbag2_storage/visibility_control.h"

namespace rosbag2_storage
{

/// Abstract base class for the storage API
/**
* Every storage plugin shall implement this interface
* in order to be able to get correctly loaded by the storage API.
*/
class StorageInterface
{
public:
ROSBAG2_STORAGE_PUBLIC
StorageInterface() = default;

ROSBAG2_STORAGE_PUBLIC
virtual ~StorageInterface() = default;

/// Open the bag file given the specified path
/**
* The function opens the bag file and returns a handle to it.
* \param file_path the fqdn path to the bag to be loaded
*/
ROSBAG2_STORAGE_PUBLIC
virtual
StorageHandle
open(const std::string & file_path) = 0;

/// Close the bag
/**
* The function closes the bag file which was previously opened by \sa open.
* \param storage_handle is the returned handle after opening the bag.
*/
ROSBAG2_STORAGE_PUBLIC
virtual
bool
close(StorageHandle & storage_handle) = 0;

ROSBAG2_STORAGE_PUBLIC
virtual
void
set_storage_identifier(const std::string & storage_identifier) = 0;

/// Get the identifier for this storage handle
/**
* The function returns the unique identifier for this storage format
* \return string identifying the storage format
*/
ROSBAG2_STORAGE_PUBLIC
virtual
std::string
get_storage_identifier() = 0;
};

} // namespace rosbag2_storage

#endif // ROSBAG2_STORAGE__STORAGE_INTERFACE_HPP_
56 changes: 56 additions & 0 deletions rosbag2_storage/include/rosbag2_storage/visibility_control.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2018 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/* This header must be included by all rclcpp headers which declare symbols
* which are defined in the rclcpp library. When not building the rclcpp
* library, i.e. when using the headers in other package's code, the contents
* of this header change the visibility of certain symbols which the rclcpp
* library cannot have, but the consuming code must have inorder to link.
*/

#ifndef ROSBAG2_STORAGE__VISIBILITY_CONTROL_H_
#define ROSBAG2_STORAGE__VISIBILITY_CONTROL_H_

// This logic was borrowed (then namespaced) from the examples on the gcc wiki:
// https://gcc.gnu.org/wiki/Visibility

#if defined _WIN32 || defined __CYGWIN__
#ifdef __GNUC__
#define ROSBAG2_STORAGE_EXPORT __attribute__ ((dllexport))
#define ROSBAG2_STORAGE_IMPORT __attribute__ ((dllimport))
#else
#define ROSBAG2_STORAGE_EXPORT __declspec(dllexport)
#define ROSBAG2_STORAGE_IMPORT __declspec(dllimport)
#endif
#ifdef ROSBAG2_STORAGE_BUILDING_DLL
#define ROSBAG2_STORAGE_PUBLIC ROSBAG2_STORAGE_EXPORT
#else
#define ROSBAG2_STORAGE_PUBLIC ROSBAG2_STORAGE_IMPORT
#endif
#define ROSBAG2_STORAGE_PUBLIC_TYPE ROSBAG2_STORAGE_PUBLIC
#define ROSBAG2_STORAGE_LOCAL
#else
#define ROSBAG2_STORAGE_EXPORT __attribute__ ((visibility("default")))
#define ROSBAG2_STORAGE_IMPORT
#if __GNUC__ >= 4
#define ROSBAG2_STORAGE_PUBLIC __attribute__ ((visibility("default")))
#define ROSBAG2_STORAGE_LOCAL __attribute__ ((visibility("hidden")))
#else
#define ROSBAG2_STORAGE_PUBLIC
#define ROSBAG2_STORAGE_LOCAL
#endif
#define ROSBAG2_STORAGE_PUBLIC_TYPE
#endif

#endif // ROSBAG2_STORAGE__VISIBILITY_CONTROL_H_
3 changes: 3 additions & 0 deletions rosbag2_storage/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

<buildtool_depend>ament_cmake</buildtool_depend>

<depend>pluginlib</depend>

<test_depend>ament_cmake_gtest</test_depend>
<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>

Expand Down
Loading

0 comments on commit b2e20ac

Please sign in to comment.