Merge branch 'feature/cxx_project_reordering' into 'master'

C++: examples folder and experimental component

See merge request espressif/esp-idf!7524
This commit is contained in:
Mahavir Jain 2020-02-18 12:48:57 +08:00
commit b181d9ab1d
33 changed files with 170 additions and 2 deletions

View file

@ -0,0 +1,2 @@
idf_component_register(SRCS "esp_exception.cpp"
INCLUDE_DIRS "include")

View file

@ -0,0 +1,20 @@
# Experimental C++ Component
*Warning:* This component is subject to change without notice. Don't consider it as a stable API.
It proposes future C++ interfaces of IDF components.
# Usage/Build
To use and build this component, add it as an extra component in your project's cmake file:
```cmake
set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/cxx/experimental/experimental_cpp_component)
```
# Tests
To build the tests, first add them to the unit test's CMakeLists.txt:
```cmake
set(EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/examples/cxx/experimental/experimental_cpp_component/")
```
Then go to the unit test app's directory and run:
```bash
idf.py -T experimental_cpp_component build
```

View file

@ -0,0 +1,11 @@
#ifdef __cpp_exceptions
#include "esp_exception.hpp"
namespace idf {
ESPException::ESPException(esp_err_t error) : error(error) { }
} // namespace idf
#endif // __cpp_exceptions

View file

@ -0,0 +1,44 @@
// Copyright 2019 Espressif Systems (Shanghai) PTE LTD
//
// 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 ESP_EXCEPTION_HPP_
#define ESP_EXCEPTION_HPP_
#ifdef __cpp_exceptions
#include "esp_err.h"
#include <exception>
namespace idf {
/**
* General exception class for exceptions on the ESP chips.
* All throwing code in IDF should use either this exception directly or a sub-classes.
*/
struct ESPException : public std::exception {
ESPException(esp_err_t error);
esp_err_t error;
};
/**
* Convenience macro to help converting IDF error codes into ESPException.
*/
#define CHECK_THROW(error_) if (error_ != ESP_OK) throw idf::ESPException(error_);
} // namespace idf
#endif // __cpp_exceptions
#endif // ESP_EXCEPTION_HPP_

View file

@ -0,0 +1,3 @@
idf_component_register(SRC_DIRS "."
PRIV_INCLUDE_DIRS .
PRIV_REQUIRES unity test_utils experimental_cpp_component)

View file

@ -0,0 +1,7 @@
#
#Component Makefile
#
COMPONENT_SRCDIRS := .
COMPONENT_ADD_LDFLAGS = -Wl,--whole-archive -l$(COMPONENT_NAME) -Wl,--no-whole-archive

View file

@ -0,0 +1,44 @@
#include <stdio.h>
#include "unity.h"
#include "unity_cxx.hpp"
#include "esp_exception.hpp"
#ifdef __cpp_exceptions
using namespace std;
using namespace idf;
#define TAG "CXX Exception Test"
TEST_CASE("TEST_THROW catches exception", "[cxx exception]")
{
TEST_THROW(throw ESPException(ESP_FAIL);, ESPException);
}
/* The following two test cases are expected to fail */
TEST_CASE("TEST_THROW asserts catching different exception", "[cxx exception][ignore]")
{
TEST_THROW(throw std::exception();, ESPException);
}
TEST_CASE("TEST_THROW asserts not catching any exception", "[cxx exception][ignore]")
{
TEST_THROW(printf(" ");, ESPException); // need statement with effect
}
TEST_CASE("CHECK_THROW continues on ESP_OK", "[cxx exception]")
{
esp_err_t error = ESP_OK;
CHECK_THROW(error);
}
TEST_CASE("CHECK_THROW throws", "[cxx exception]")
{
esp_err_t error = ESP_FAIL;
TEST_THROW(CHECK_THROW(error), ESPException);
}
#endif // __cpp_exceptions

View file

@ -0,0 +1,35 @@
#ifndef UNITY_CXX_H_
#define UNITY_CXX_H_
#include "unity.h"
#define STR(x) #x
/**
* Very simple helper macro to catch exceptions.
*
* @note
* * If there is any exception which not a child of std::exception, it will terminate the program!
* * If there is no exception, it will jump from the current frame without de-initializing
* destructors!
*/
#define TEST_THROW(expr_, exception_) \
do { \
bool caught = false; \
bool caught_different = false; \
try { \
expr_; \
} catch ( exception_ &e) { \
caught = true; \
} catch ( std::exception &e) { \
caught_different = true; \
} \
TEST_ASSERT_FALSE_MESSAGE(caught_different, "ERROR: Expected " STR(exception_) \
", but caught different exception."); \
TEST_ASSERT_TRUE_MESSAGE(caught, "ERROR: Expected " STR(exception_) \
", but no exception thrown."); \
} \
while (0)
#endif // UNITY_CXX_H_

View file

@ -2,7 +2,7 @@
# While we support GNU Make & CMake together, check the same examples are present for both
CMAKE_EXAMPLE_PATHS=$( find ${IDF_PATH}/examples/ -type f -name CMakeLists.txt | grep -v "/components/" | grep -v "/common_components/" | grep -v "/main/" | grep -v "/build_system/cmake/" | grep -v "/mb_example_common/")
CMAKE_EXAMPLE_PATHS=$( find ${IDF_PATH}/examples/ -type f -name CMakeLists.txt | grep -v "/components/" | grep -v "/common_components/" | grep -v "/cxx/experimental/experimental_cpp_component/" | grep -v "/main/" | grep -v "/build_system/cmake/" | grep -v "/mb_example_common/")
MAKE_EXAMPLE_PATHS=$( find ${IDF_PATH}/examples/ -type f -name Makefile | grep -v "/build_system/cmake/")
CMAKE_EXAMPLE_PATHS="$(/usr/bin/dirname $CMAKE_EXAMPLE_PATHS | sort -n)"

View file

@ -2,5 +2,7 @@
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)
set(EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/examples/cxx/experimental/experimental_cpp_component/")
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(unit-test-app)
project(unit-test-app)