Skip to content

Commit

Permalink
WIP NOT INTENDED FOR USE
Browse files Browse the repository at this point in the history
  • Loading branch information
RandomSpaceship committed Dec 24, 2024
1 parent 37c85ee commit 05f2b7d
Show file tree
Hide file tree
Showing 19 changed files with 694 additions and 28 deletions.
38 changes: 19 additions & 19 deletions .vscode/c_cpp_properties.json
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
}
5 changes: 5 additions & 0 deletions docs/source/standards/software.md
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,11 @@ Although [implicit conversions](https://en.cppreference.com/w/cpp/language/impli

If this is not done, it means that values can get _copied_ around, which can have performance hits, especially if the objects being copied are complex classes that need to run copy constructors and/or destructors to do so.

However, you should _not_ do this for basic types ({cpp:expr}`int`, {cpp:expr}`uint8_t`, {cpp:expr}`double`, etc.), as there is no advantage to doing so here.
In fact, in some cases it can actually _cause_ bugs to appear - although that's exceptionally rare.
If you're not sure what to do, just pass by reference.
% TODO: Update code to comply with this

##### Acceptable

```cpp
Expand Down
6 changes: 5 additions & 1 deletion perseus-v2.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"settings": {
"nixEnvSelector.suggestion": true,
"files.associations": {
"*.scad": "scad",
"array": "cpp",
"atomic": "cpp",
"bit": "cpp",
Expand Down Expand Up @@ -89,7 +90,10 @@
"regex": "cpp",
"source_location": "cpp",
"ranges": "cpp",
"valarray": "cpp"
"valarray": "cpp",
"*.tcc": "cpp",
"hash_map": "cpp",
"hash_set": "cpp"
},
"C_Cpp.default.cppStandard": "c++20",
"nixEnvSelector.nixFile": "${workspaceFolder}/flake.nix",
Expand Down
13 changes: 8 additions & 5 deletions software/native/hi-can-common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,18 @@ add_executable(${PROJECT_NAME} ${CODE_SOURCES})
target_include_directories(
${PROJECT_NAME} PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>)
# check if libraries are already present (Nix build), if not, add them
find_package(hi_can_raw QUIET)
if(NOT hi_can_raw_FOUND)
message(STATUS "hi_can_raw not found, adding subdirectory...")
# find_package(hi_can_raw QUIET) if(NOT hi_can_raw_FOUND) message(STATUS
# "hi_can_raw not found, adding subdirectory...") # add the directory with its
# CMakeLists.txt add_subdirectory(../../shared/hi-can-raw hi-can-raw) endif()
find_package(simple_networking QUIET)
if(NOT simple_networking_FOUND)
message(STATUS "simple_networking not found, adding subdirectory...")
# add the directory with its CMakeLists.txt
add_subdirectory(../../shared/hi-can-raw hi-can-raw)
add_subdirectory(../../shared/simple-networking simple-networking)
endif()

# Link in libraries needed
target_link_libraries(${PROJECT_NAME} PUBLIC hi_can_raw)
target_link_libraries(${PROJECT_NAME} PUBLIC simple_networking)

# Versioning
set_target_properties(${PROJECT_NAME} PROPERTIES VERSION ${PROJECT_VERSION})
Expand Down
6 changes: 5 additions & 1 deletion software/native/hi-can-common/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
cleanCmakeSource,
cmake,
hi-can-raw,
simple-networking,
}:

stdenv.mkDerivation rec {
Expand All @@ -15,5 +16,8 @@ stdenv.mkDerivation rec {
};

nativeBuildInputs = [ cmake ];
buildInputs = [ hi-can-raw ];
buildInputs = [
simple-networking
hi-can-raw
];
}
23 changes: 21 additions & 2 deletions software/native/hi-can-common/src/main.cpp
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;
}
2 changes: 2 additions & 0 deletions software/shared/overlay.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ final: prev: {
hi-can = final.callPackage ./hi-can { };
hi-can-raw = final.callPackage ./hi-can-raw { };
hi-can-net = final.callPackage ./hi-can-net { };
ptr-wrapper = final.callPackage ./ptr-wrapper { };
simple-networking = final.callPackage ./simple-networking { };
}
83 changes: 83 additions & 0 deletions software/shared/ptr-wrapper/CMakeLists.txt
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})
1 change: 1 addition & 0 deletions software/shared/ptr-wrapper/config.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include(${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@_targets.cmake)
17 changes: 17 additions & 0 deletions software/shared/ptr-wrapper/default.nix
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 ];
}
83 changes: 83 additions & 0 deletions software/shared/ptr-wrapper/include/ptr_wrapper.hpp
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;
};
1 change: 1 addition & 0 deletions software/shared/ptr-wrapper/src/ptr_wrapper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "ptr_wrapper.hpp"
Loading

0 comments on commit 05f2b7d

Please sign in to comment.