-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
37c85ee
commit 05f2b7d
Showing
19 changed files
with
694 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,20 @@ | ||
{ | ||
"configurations": [ | ||
{ | ||
"browse": { | ||
"databaseFilename": "${default}", | ||
"limitSymbolsToIncludedHeaders": false | ||
}, | ||
"includePath": [ | ||
"${env:ROS_WORKSPACE_ENV_PATH}/include/**", | ||
"/usr/include/**", | ||
"${workspaceFolder}/**" | ||
], | ||
"name": "Linux", | ||
"intelliSenseMode": "gcc-x64", | ||
"cStandard": "gnu17", | ||
"cppStandard": "c++20" | ||
} | ||
], | ||
"version": 4 | ||
} | ||
"configurations": [ | ||
{ | ||
"browse": { | ||
"databaseFilename": "${default}", | ||
"limitSymbolsToIncludedHeaders": false | ||
}, | ||
"includePath": [ | ||
"${workspaceFolder}/**", | ||
"${env:ROS_WORKSPACE_ENV_PATH}/include/**", | ||
"/usr/include/**" | ||
], | ||
"name": "Linux", | ||
"intelliSenseMode": "${default}", | ||
"cStandard": "gnu17", | ||
"cppStandard": "c++20" | ||
} | ||
], | ||
"version": 4 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,26 @@ | ||
#include <hi_can_raw.hpp> | ||
#include <iostream> | ||
|
||
#include <simple_networking.hpp> | ||
#include <string> | ||
#include <thread> | ||
#include <vector> | ||
using namespace std; | ||
using namespace networking; | ||
int main() | ||
{ | ||
Client client({"localhost", "8080"}, socket_protocol::TCP, 8082); | ||
client.transmit("c1-1\n"); | ||
std::this_thread::sleep_for(std::chrono::seconds(2)); | ||
if (true) | ||
{ | ||
Client c2({"localhost", "8080"}, socket_protocol::UDP, 8082); | ||
c2.transmit("c2-1\n"); | ||
std::this_thread::sleep_for(std::chrono::seconds(2)); | ||
swap(client, c2); | ||
c2.transmit("c2-2\n"); | ||
std::this_thread::sleep_for(std::chrono::seconds(1)); | ||
} | ||
std::this_thread::sleep_for(std::chrono::seconds(1)); | ||
client.transmit("c1-2\n"); | ||
std::this_thread::sleep_for(std::chrono::seconds(2)); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# PROJECT SETUP | ||
cmake_minimum_required(VERSION 3.23) | ||
|
||
project( | ||
ptr_wrapper | ||
VERSION 0.0.1 | ||
LANGUAGES CXX) | ||
|
||
# credit https://www.kitware.com/cmake-and-the-default-build-type/ | ||
set(default_build_type "Debug") | ||
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) | ||
message( | ||
STATUS | ||
"Setting build type to '${default_build_type}' as none was specified.") | ||
set(CMAKE_BUILD_TYPE | ||
"${default_build_type}" | ||
CACHE STRING "Choose the type of build." FORCE) | ||
# Set the possible values of build type for cmake-gui | ||
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" | ||
"MinSizeRel" "RelWithDebInfo") | ||
endif() | ||
# we always want debug info for stack tracing, so switch to RelWithDebInfo from | ||
# Release | ||
if(CMAKE_BUILD_TYPE STREQUAL "Release") | ||
set(CMAKE_BUILD_TYPE "RelWithDebInfo") | ||
endif() | ||
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}") | ||
|
||
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") | ||
add_compile_options(-Wall -Wextra -Wpedantic) | ||
endif() | ||
# Add -Werror flag for release builds | ||
if(CMAKE_BUILD_TYPE MATCHES "Rel.*") | ||
add_compile_options(-Werror) | ||
endif() | ||
|
||
# Set the C++ standard | ||
set(CMAKE_CXX_STANDARD 20) | ||
set(CMAKE_CXX_STANDARD_REQUIRED ON) | ||
|
||
# COMPILE (Libraries) | ||
|
||
# Find all source files in the src directory | ||
file(GLOB_RECURSE CODE_SOURCES src/*.cpp) | ||
# Add the target with those sources | ||
add_library(${PROJECT_NAME} SHARED ${CODE_SOURCES}) | ||
# Set the include directories for the library | ||
target_include_directories( | ||
${PROJECT_NAME} PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> | ||
$<INSTALL_INTERFACE:include/${PROJECT_NAME}>) | ||
|
||
# INSTALL (Libraries) | ||
include(GNUInstallDirs) | ||
include(CMakePackageConfigHelpers) | ||
|
||
# CMake packaging - see https://blog.vito.nyc/posts/cmake-pkg/ for a good | ||
# explanation | ||
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/config.cmake.in | ||
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config.cmake @ONLY) | ||
write_basic_package_version_file( | ||
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config-version.cmake | ||
COMPATIBILITY SameMinorVersion) | ||
|
||
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config-version.cmake | ||
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config.cmake | ||
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}) | ||
|
||
# Headers | ||
install(DIRECTORY include/ DESTINATION include/${PROJECT_NAME}) | ||
|
||
# Versioning | ||
set_target_properties(${PROJECT_NAME} PROPERTIES VERSION ${PROJECT_VERSION}) | ||
# The main install - default locations are fine | ||
install( | ||
TARGETS ${PROJECT_NAME} | ||
EXPORT ${PROJECT_NAME}_targets | ||
ARCHIVE | ||
LIBRARY | ||
RUNTIME) | ||
|
||
# The last of the CMake packaging info | ||
install(EXPORT ${PROJECT_NAME}_targets | ||
DESTINATION ${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
include(${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@_targets.cmake) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
stdenv, | ||
cleanCmakeSource, | ||
cmake, | ||
}: | ||
|
||
stdenv.mkDerivation rec { | ||
pname = "ptr-wrapper"; | ||
version = "0.0.1"; | ||
|
||
src = cleanCmakeSource { | ||
src = ./.; | ||
name = pname; | ||
}; | ||
|
||
nativeBuildInputs = [ cmake ]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
#pragma once | ||
|
||
#include <functional> | ||
#include <stdexcept> | ||
|
||
/// @brief A wrapper around a pointer which will automatically deallocate the resource when it goes out of scope | ||
/// @details Intended for use in situations where @ref shared_ptr can't be used, such as when the resource is not allocated with `new` | ||
template <typename T> | ||
class PtrWrapper | ||
{ | ||
public: | ||
PtrWrapper() = default; | ||
PtrWrapper(T* ptr, std::function<void(T*)> deallocator) | ||
: PtrWrapper([ptr]() | ||
{ return ptr; }, deallocator) {} | ||
PtrWrapper(std::function<T*(void)> allocator, std::function<void(T*)> deallocator) | ||
: _deallocator(deallocator) | ||
{ | ||
if (!allocator) | ||
throw std::invalid_argument("Allocator must be provided"); | ||
if (!deallocator) | ||
throw std::invalid_argument("Deallocator must be provided"); | ||
_ptr = allocator(); | ||
} | ||
|
||
~PtrWrapper() | ||
{ | ||
try | ||
{ | ||
if (_ptr) | ||
_deallocator(_ptr); | ||
} | ||
catch (...) | ||
{ | ||
// ignore - since we're in a destructor, we can't throw | ||
} | ||
} | ||
|
||
// we do NOT want to allow copying, that doesn't make sense. | ||
// Moving is OK though | ||
PtrWrapper(const PtrWrapper&) = delete; | ||
PtrWrapper(PtrWrapper&& other) noexcept | ||
{ | ||
swap(*this, other); | ||
} | ||
PtrWrapper& operator=(PtrWrapper) = delete; | ||
PtrWrapper& operator=(PtrWrapper&& other) noexcept | ||
{ | ||
swap(*this, other); | ||
return *this; | ||
} | ||
|
||
PtrWrapper& operator=(T* ptr) | ||
{ | ||
if (_ptr) | ||
_deallocator(_ptr); | ||
_ptr = ptr; | ||
return *this; | ||
} | ||
|
||
T** operator&() | ||
{ | ||
return &_ptr; | ||
} | ||
|
||
friend void swap(PtrWrapper& first, PtrWrapper& second) | ||
{ | ||
using std::swap; | ||
swap(first._ptr, second._ptr); | ||
swap(first._deallocator, second._deallocator); | ||
} | ||
|
||
// allow all the common methods of getting the file descriptor/"dereferencing" | ||
virtual inline explicit operator T*() const { return get(); } | ||
virtual inline T* operator*() const { return get(); } | ||
virtual inline T* operator->() const { return get(); } | ||
virtual inline T* get() const { return _ptr; } | ||
|
||
private: | ||
T* _ptr = nullptr; | ||
|
||
std::function<void(T*)> _deallocator = nullptr; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#include "ptr_wrapper.hpp" |
Oops, something went wrong.