Skip to content

Commit

Permalink
new class dai::Path for APIs that accept path/filenames (#384)
Browse files Browse the repository at this point in the history
* initial dai::Path and test cases

- fixes #352

* move codecvt from header -> cpp

* add Path::string() and u8string()

- to enable display/log of Path

* fmt for dai::Path; NN::setBlobPath(dai::Path)

* dia::path throws like std::fs::path

* c++17, pub/pvt header, test cmake c++ std level

- enable c++17 std::filesystem support and test cases
- split header into public/private parts
- cmake for test cases now supports optional
  c++ standard level param

* verify c++ std compiler support for tests

- add COMPILER_SUPPORTS_CXX{14,17,20,23} vars
  to Flags.cmake and can be used everywhere

* add dai::Path::empty()

* add dai::Path to Device, DeviceBase, Resources

- simplify Device, DeviceBase constructors by delegating
- add is_same<> template on constructors with bool param to
  prevent implicit convert of almost everything to bool
- make two DeviceInfo constructors explicit to prevent their use in
  implicit conversion
- relevant test cases
- fix minor throw text bugs

* fix Device usb2Mode sigs, add test case

* add dai::Path to CalibrationHandler

* minor refactor dai::Path

* enable 2 Calibration+1 Bootloader example

* add dai::Path to DeviceBootloader, XLinkConnection

- plus test cases

* add dai::Path to Pipeline, StereoDepth, AssetManager

- plus test cases

* add dai::Path to dai::Script + test cases

* linux fixes for test cases, and c++14 type_traits

* add doxygen to dai::Path

* detect compiler c++ std level and update cmake

* fix preprocessor flag for tests on MSVC

- fixes luxonis/depthai-core/issues#408

* partial dai::Path support for c++20 utf-8

- unable to fully test due to bug #407

* add windows header define WIN32_LEAN_AND_MEAN

* rename macro to DEPTHAI_NODISCARD

- review feedback
  • Loading branch information
diablodale committed Apr 9, 2022
1 parent ec14f9a commit 636cbb5
Show file tree
Hide file tree
Showing 34 changed files with 965 additions and 247 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ add_library(${TARGET_CORE_NAME}
src/pipeline/datatype/FeatureTrackerConfig.cpp
src/utility/Initialization.cpp
src/utility/Resources.cpp
src/utility/Path.cpp
src/utility/Platform.cpp
src/utility/Environment.cpp
src/xlink/XLinkConnection.cpp
Expand Down
50 changes: 49 additions & 1 deletion cmake/Flags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ function(add_default_flags target)

elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
# using Visual Studio C++
add_flag(${target} -DNOMINMAX)
add_flag(${target} -DWIN32_LEAN_AND_MEAN)

if(NOT ADF_LEAN)
# TODO(warchant): add flags https://github.com/lefticus/cppbestpractices/blob/master/02-Use_the_Tools_Available.md#msvc
endif()
Expand Down Expand Up @@ -78,4 +81,49 @@ function(add_default_flags target)
endif()
endif()

endfunction()
endfunction()

# discover compiler C++ standard support
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
check_cxx_compiler_flag("-std:c++14" COMPILER_SUPPORTS_CXX14)
check_cxx_compiler_flag("-std:c++17" COMPILER_SUPPORTS_CXX17)
check_cxx_compiler_flag("-std:c++20" COMPILER_SUPPORTS_CXX20)
check_cxx_compiler_flag("-std:c++23" COMPILER_SUPPORTS_CXX23)
if(COMPILER_SUPPORTS_CXX14)
set(CMAKE_CXX14_STANDARD_COMPILE_OPTION "-std:c++14")
set(CMAKE_CXX14_EXTENSION_COMPILE_OPTION "-std:c++14")
endif()
if(COMPILER_SUPPORTS_CXX17)
set(CMAKE_CXX17_STANDARD_COMPILE_OPTION "-std:c++17")
set(CMAKE_CXX17_EXTENSION_COMPILE_OPTION "-std:c++17")
endif()
if(COMPILER_SUPPORTS_CXX20)
set(CMAKE_CXX20_STANDARD_COMPILE_OPTION "-std:c++20")
set(CMAKE_CXX20_EXTENSION_COMPILE_OPTION "-std:c++20")
endif()
if(COMPILER_SUPPORTS_CXX23)
set(CMAKE_CXX23_STANDARD_COMPILE_OPTION "-std:c++23")
set(CMAKE_CXX23_EXTENSION_COMPILE_OPTION "-std:c++23")
endif()
elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "^(AppleClang|Clang|GNU)$")
check_cxx_compiler_flag("-std=c++14" COMPILER_SUPPORTS_CXX14)
check_cxx_compiler_flag("-std=c++17" COMPILER_SUPPORTS_CXX17)
check_cxx_compiler_flag("-std=c++20" COMPILER_SUPPORTS_CXX20)
check_cxx_compiler_flag("-std=c++23" COMPILER_SUPPORTS_CXX23)
if(COMPILER_SUPPORTS_CXX14)
set(CMAKE_CXX14_STANDARD_COMPILE_OPTION "-std=c++14")
set(CMAKE_CXX14_EXTENSION_COMPILE_OPTION "-std=gnu++14")
endif()
if(COMPILER_SUPPORTS_CXX17)
set(CMAKE_CXX17_STANDARD_COMPILE_OPTION "-std=c++17")
set(CMAKE_CXX17_EXTENSION_COMPILE_OPTION "-std=gnu++17")
endif()
if(COMPILER_SUPPORTS_CXX20)
set(CMAKE_CXX20_STANDARD_COMPILE_OPTION "-std=c++20")
set(CMAKE_CXX20_EXTENSION_COMPILE_OPTION "-std=gnu++20")
endif()
if(COMPILER_SUPPORTS_CXX23)
set(CMAKE_CXX23_STANDARD_COMPILE_OPTION "-std=c++23")
set(CMAKE_CXX23_EXTENSION_COMPILE_OPTION "-std=gnu++23")
endif()
endif()
6 changes: 3 additions & 3 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,14 @@ hunter_private_data(
# bootloader
dai_add_example(bootloader_version bootloader/bootloader_version.cpp ON)
dai_add_example(flash_bootloader bootloader/flash_bootloader.cpp OFF)
dai_add_example(bootloader_config bootloader/bootloader_config.cpp OFF)
dai_add_example(bootloader_config bootloader/bootloader_config.cpp ON read)
dai_add_example(poe_set_ip bootloader/poe_set_ip.cpp OFF)

# calibration
dai_add_example(calibration_flash calibration/calibration_flash.cpp OFF)
dai_add_example(calibration_flash_version5 calibration/calibration_flash_v5.cpp OFF)
dai_add_example(calibration_load calibration/calibration_load.cpp OFF)
dai_add_example(calibration_reader calibration/calibration_reader.cpp OFF)
dai_add_example(calibration_load calibration/calibration_load.cpp ON)
dai_add_example(calibration_reader calibration/calibration_reader.cpp ON)

target_compile_definitions(calibration_flash PRIVATE CALIB_PATH="${calib_v6}")
target_compile_definitions(calibration_flash_version5 PRIVATE CALIB_PATH="${calib_v5}" BOARD_PATH="${device_config}")
Expand Down
9 changes: 9 additions & 0 deletions examples/bootloader/bootloader_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ int main(int argc, char** argv) {
if(read) {
std::cout << "Current flashed configuration\n" << bl.readConfigData().dump(4) << std::endl;
} else {
std::cout << "Warning! Flashing bootloader config can potentially soft brick your device and should be done with caution." << std::endl;
std::cout << "Do not unplug your device while the bootloader config is flashing." << std::endl;
std::cout << "Type 'y' and press enter to proceed, otherwise exits: ";
std::cin.ignore();
if(std::cin.get() != 'y') {
std::cout << "Prompt declined, exiting..." << std::endl;
return -1;
}

bool success;
std::string error;
if(clear) {
Expand Down
7 changes: 4 additions & 3 deletions include/depthai/device/CalibrationHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "depthai-shared/common/EepromData.hpp"
#include "depthai-shared/common/Point2f.hpp"
#include "depthai-shared/common/Size2f.hpp"
#include "depthai/utility/Path.hpp"

namespace dai {
/**
Expand All @@ -21,7 +22,7 @@ class CalibrationHandler {
*
* @param eepromDataPath takes the full path to the json file containing the calibration and device info.
*/
explicit CalibrationHandler(std::string eepromDataPath);
explicit CalibrationHandler(dai::Path eepromDataPath);

/**
* Construct a new Calibration Handler object using the board
Expand All @@ -30,7 +31,7 @@ class CalibrationHandler {
* @param calibrationDataPath Full Path to the .calib binary file from the gen1 calibration. (Supports only Version 5)
* @param boardConfigPath Full Path to the board config json file containing device information.
*/
CalibrationHandler(std::string calibrationDataPath, std::string boardConfigPath);
CalibrationHandler(dai::Path calibrationDataPath, dai::Path boardConfigPath);

/**
* Construct a new Calibration Handler object from EepromData object.
Expand Down Expand Up @@ -284,7 +285,7 @@ class CalibrationHandler {
* @param destPath Full path to the json file in which raw calibration data will be stored
* @return True on success, false otherwise
*/
bool eepromToJsonFile(std::string destPath) const;
bool eepromToJsonFile(dai::Path destPath) const;

/**
* Set the Board Info object
Expand Down
30 changes: 9 additions & 21 deletions include/depthai/device/Device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ class Device : public DeviceBase {
/**
* Connects to any available device with a DEFAULT_SEARCH_TIME timeout.
* @param pipeline Pipeline to be executed on the device
* @param usb2Mode Boot device using USB2 mode firmware
* @param usb2Mode (bool) Boot device using USB2 mode firmware
*/
Device(const Pipeline& pipeline, bool usb2Mode);
template <typename T, std::enable_if_t<std::is_same<T, bool>::value, bool> = true>
Device(const Pipeline& pipeline, T usb2Mode) : DeviceBase(pipeline, usb2Mode ? UsbSpeed::HIGH : DeviceBase::DEFAULT_USB_SPEED) {}

/**
* Connects to any available device with a DEFAULT_SEARCH_TIME timeout.
Expand All @@ -47,14 +48,7 @@ class Device : public DeviceBase {
* @param pipeline Pipeline to be executed on the device
* @param pathToCmd Path to custom device firmware
*/
Device(const Pipeline& pipeline, const char* pathToCmd);

/**
* Connects to any available device with a DEFAULT_SEARCH_TIME timeout.
* @param pipeline Pipeline to be executed on the device
* @param pathToCmd Path to custom device firmware
*/
Device(const Pipeline& pipeline, const std::string& pathToCmd);
Device(const Pipeline& pipeline, const dai::Path& pathToCmd);

/**
* Connects to device specified by devInfo.
Expand All @@ -67,9 +61,11 @@ class Device : public DeviceBase {
* Connects to device specified by devInfo.
* @param pipeline Pipeline to be executed on the device
* @param devInfo DeviceInfo which specifies which device to connect to
* @param usb2Mode Boot device using USB2 mode firmware
* @param usb2Mode (bool) Boot device using USB2 mode firmware
*/
Device(const Pipeline& pipeline, const DeviceInfo& devInfo, bool usb2Mode);
template <typename T, std::enable_if_t<std::is_same<T, bool>::value, bool> = true>
Device(const Pipeline& pipeline, const DeviceInfo& devInfo, T usb2Mode)
: DeviceBase(pipeline, devInfo, usb2Mode ? UsbSpeed::HIGH : DeviceBase::DEFAULT_USB_SPEED) {}

/**
* Connects to device specified by devInfo.
Expand All @@ -85,15 +81,7 @@ class Device : public DeviceBase {
* @param devInfo DeviceInfo which specifies which device to connect to
* @param pathToCmd Path to custom device firmware
*/
Device(const Pipeline& pipeline, const DeviceInfo& devInfo, const char* pathToCmd);

/**
* Connects to device specified by devInfo.
* @param pipeline Pipeline to be executed on the device
* @param devInfo DeviceInfo which specifies which device to connect to
* @param pathToCmd Path to custom device firmware
*/
Device(const Pipeline& pipeline, const DeviceInfo& devInfo, const std::string& pathToCmd);
Device(const Pipeline& pipeline, const DeviceInfo& devInfo, const dai::Path& pathToCmd);

/**
* Connects to any available device with a DEFAULT_SEARCH_TIME timeout.
Expand Down
65 changes: 21 additions & 44 deletions include/depthai/device/DeviceBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <string>
#include <thread>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <vector>

Expand Down Expand Up @@ -129,7 +130,8 @@ class DeviceBase {
* @param pipeline Pipeline to be executed on the device
* @param usb2Mode Boot device using USB2 mode firmware
*/
DeviceBase(const Pipeline& pipeline, bool usb2Mode);
template <typename T, std::enable_if_t<std::is_same<T, bool>::value, bool> = true>
DeviceBase(const Pipeline& pipeline, T usb2Mode) : DeviceBase(pipeline, usb2Mode ? UsbSpeed::HIGH : DeviceBase::DEFAULT_USB_SPEED) {}

/**
* Connects to any available device with a DEFAULT_SEARCH_TIME timeout.
Expand All @@ -143,14 +145,7 @@ class DeviceBase {
* @param pipeline Pipeline to be executed on the device
* @param pathToCmd Path to custom device firmware
*/
DeviceBase(const Pipeline& pipeline, const char* pathToCmd);

/**
* Connects to any available device with a DEFAULT_SEARCH_TIME timeout.
* @param pipeline Pipeline to be executed on the device
* @param pathToCmd Path to custom device firmware
*/
DeviceBase(const Pipeline& pipeline, const std::string& pathToCmd);
DeviceBase(const Pipeline& pipeline, const dai::Path& pathToCmd);

/**
* Connects to device specified by devInfo.
Expand All @@ -165,7 +160,9 @@ class DeviceBase {
* @param devInfo DeviceInfo which specifies which device to connect to
* @param usb2Mode Boot device using USB2 mode firmware
*/
DeviceBase(const Pipeline& pipeline, const DeviceInfo& devInfo, bool usb2Mode);
template <typename T, std::enable_if_t<std::is_same<T, bool>::value, bool> = true>
DeviceBase(const Pipeline& pipeline, const DeviceInfo& devInfo, T usb2Mode)
: DeviceBase(pipeline, devInfo, usb2Mode ? UsbSpeed::HIGH : DeviceBase::DEFAULT_USB_SPEED) {}

/**
* Connects to device specified by devInfo.
Expand All @@ -181,15 +178,7 @@ class DeviceBase {
* @param devInfo DeviceInfo which specifies which device to connect to
* @param pathToCmd Path to custom device firmware
*/
DeviceBase(const Pipeline& pipeline, const DeviceInfo& devInfo, const char* pathToCmd);

/**
* Connects to device specified by devInfo.
* @param pipeline Pipeline to be executed on the device
* @param devInfo DeviceInfo which specifies which device to connect to
* @param pathToCmd Path to custom device firmware
*/
DeviceBase(const Pipeline& pipeline, const DeviceInfo& devInfo, const std::string& pathToCmd);
DeviceBase(const Pipeline& pipeline, const DeviceInfo& devInfo, const dai::Path& pathToCmd);

/**
* Connects to any available device with a DEFAULT_SEARCH_TIME timeout.
Expand All @@ -208,12 +197,12 @@ class DeviceBase {
* @param version OpenVINO version which the device will be booted with
* @param usb2Mode Boot device using USB2 mode firmware
*/
DeviceBase(OpenVINO::Version version, bool usb2Mode);
template <typename T, std::enable_if_t<std::is_same<T, bool>::value, bool> = true>
DeviceBase(OpenVINO::Version version, T usb2Mode) : DeviceBase(version, usb2Mode ? UsbSpeed::HIGH : DeviceBase::DEFAULT_USB_SPEED) {}

/**
* Connects to device specified by devInfo.
* @param version OpenVINO version which the device will be booted with
* @param devInfo DeviceInfo which specifies which device to connect to
* @param maxUsbSpeed Maximum allowed USB speed
*/
DeviceBase(OpenVINO::Version version, UsbSpeed maxUsbSpeed);
Expand All @@ -223,14 +212,7 @@ class DeviceBase {
* @param version OpenVINO version which the device will be booted with
* @param pathToCmd Path to custom device firmware
*/
DeviceBase(OpenVINO::Version version, const char* pathToCmd);

/**
* Connects to any available device with a DEFAULT_SEARCH_TIME timeout.
* @param version OpenVINO version which the device will be booted with
* @param pathToCmd Path to custom device firmware
*/
DeviceBase(OpenVINO::Version version, const std::string& pathToCmd);
DeviceBase(OpenVINO::Version version, const dai::Path& pathToCmd);

/**
* Connects to device specified by devInfo.
Expand All @@ -245,7 +227,9 @@ class DeviceBase {
* @param devInfo DeviceInfo which specifies which device to connect to
* @param usb2Mode Boot device using USB2 mode firmware
*/
DeviceBase(OpenVINO::Version version, const DeviceInfo& devInfo, bool usb2Mode);
template <typename T, std::enable_if_t<std::is_same<T, bool>::value, bool> = true>
DeviceBase(OpenVINO::Version version, const DeviceInfo& devInfo, T usb2Mode)
: DeviceBase(version, devInfo, usb2Mode ? UsbSpeed::HIGH : DeviceBase::DEFAULT_USB_SPEED) {}

/**
* Connects to device specified by devInfo.
Expand All @@ -261,15 +245,7 @@ class DeviceBase {
* @param devInfo DeviceInfo which specifies which device to connect to
* @param pathToCmd Path to custom device firmware
*/
DeviceBase(OpenVINO::Version version, const DeviceInfo& devInfo, const char* pathToCmd);

/**
* Connects to device specified by devInfo.
* @param version OpenVINO version which the device will be booted with
* @param devInfo DeviceInfo which specifies which device to connect to
* @param usb2Mode Path to custom device firmware
*/
DeviceBase(OpenVINO::Version version, const DeviceInfo& devInfo, const std::string& pathToCmd);
DeviceBase(OpenVINO::Version version, const DeviceInfo& devInfo, const dai::Path& pathToCmd);

/**
* Connects to any available device with custom config.
Expand Down Expand Up @@ -578,11 +554,12 @@ class DeviceBase {

private:
// private functions
void init(OpenVINO::Version version, bool usb2Mode, const std::string& pathToMvcmd);
void init(const Pipeline& pipeline, bool usb2Mode, const std::string& pathToMvcmd);
void init(OpenVINO::Version version, UsbSpeed maxUsbSpeed, const std::string& pathToMvcmd);
void init(const Pipeline& pipeline, UsbSpeed maxUsbSpeed, const std::string& pathToMvcmd);
void init2(Config cfg, const std::string& pathToMvcmd, tl::optional<const Pipeline&> pipeline);
void init(OpenVINO::Version version, bool usb2Mode, const dai::Path& pathToMvcmd);
void init(const Pipeline& pipeline, bool usb2Mode, const dai::Path& pathToMvcmd);
void init(OpenVINO::Version version, UsbSpeed maxUsbSpeed, const dai::Path& pathToMvcmd);
void init(const Pipeline& pipeline, UsbSpeed maxUsbSpeed, const dai::Path& pathToMvcmd);
void init2(Config cfg, const dai::Path& pathToMvcmd, tl::optional<const Pipeline&> pipeline);
void tryGetDevice();

DeviceInfo deviceInfo = {};

Expand Down
Loading

0 comments on commit 636cbb5

Please sign in to comment.