Skip to content

Commit 4b7ad47

Browse files
committed
Begin with the library implementation
1 parent 23e8c10 commit 4b7ad47

17 files changed

+201
-22
lines changed

.clang-tidy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Checks: >
1414
1515
1616
CheckOptions:
17-
- { key: readability-identifier-naming.NamespaceCase, value: CamelCase }
17+
- { key: readability-identifier-naming.NamespaceCase, value: lower_case }
1818
- { key: readability-identifier-naming.ClassCase, value: CamelCase }
1919
- { key: readability-identifier-naming.StructCase, value: CamelCase }
2020
- { key: readability-identifier-naming.TemplateParameterCase, value: CamelCase }

CMakeLists.txt

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,55 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
1313

1414
include(CTest)
1515
include(FetchContent)
16+
include(GNUInstallDirs)
17+
include(GenerateExportHeader)
1618
include(dependencies)
1719

1820
declare_dependencies()
1921

2022
find_package(Threads REQUIRED)
2123
find_package(Protobuf REQUIRED)
22-
find_package(stdexec CONFIG REQUIRED)
2324

2425
# only for asio. TODO: make using standalone asio possible
2526
find_package(Boost CONFIG REQUIRED)
2627

2728
add_subdirectory(cppesphomeapi)
29+
add_subdirectory(example)
30+
31+
include(CMakePackageConfigHelpers)
32+
33+
install(TARGETS cppesphomeapi
34+
EXPORT cppesphomeapiTargets
35+
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
36+
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
37+
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
38+
INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
39+
FILE_SET headers
40+
FILE_SET generated_headers
41+
)
42+
43+
set(config_install_dir "${CMAKE_INSTALL_LIBDIR}/cmake/cppesphomeapi")
44+
45+
install(EXPORT cppesphomeapiTargets
46+
FILE cppesphomeapiTargets.cmake
47+
NAMESPACE quite::
48+
DESTINATION "${config_install_dir}"
49+
)
50+
51+
configure_package_config_file(
52+
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/Config.cmake.in"
53+
"${CMAKE_CURRENT_BINARY_DIR}/cppesphomeapiConfig.cmake"
54+
INSTALL_DESTINATION "${config_install_dir}"
55+
)
56+
57+
write_basic_package_version_file(
58+
"${CMAKE_CURRENT_BINARY_DIR}/cppesphomeapiConfigVersion.cmake"
59+
VERSION "${CMAKE_PROJECT_VERSION}"
60+
COMPATIBILITY SameMajorVersion
61+
)
62+
63+
install(FILES
64+
"${CMAKE_CURRENT_BINARY_DIR}/cppesphomeapiConfig.cmake"
65+
"${CMAKE_CURRENT_BINARY_DIR}/cppesphomeapiConfigVersion.cmake"
66+
DESTINATION "${config_install_dir}"
67+
)

cmake/Config.cmake.in

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@PACKAGE_INIT@
2+
3+
include("${CMAKE_CURRENT_LIST_DIR}/cppesphomeapiTargets.cmake")
4+
5+
check_required_components(cppesphomeapi)

cmake/dependencies.cmake

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
11
macro(declare_dependencies)
22
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
33
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
4-
5-
set(project_build_testing ${BUILD_TESTING})
6-
set(BUILD_TESTING OFF)
7-
set(STDEXEC_BUILD_EXAMPLES OFF)
8-
FetchContent_Declare(
9-
stdexec
10-
GIT_REPOSITORY https://github.com/nvidia/stdexec.git
11-
GIT_TAG c4b905342c8335dca5f49617997b0997908a8111
12-
FIND_PACKAGE_ARGS
13-
)
14-
FetchContent_MakeAvailable(stdexec)
15-
164
set(BUILD_TESTING ${project_build_testing})
175
if(BUILD_TESTING)
186
set(CATCH_INSTALL_DOCS OFF)
@@ -22,8 +10,6 @@ macro(declare_dependencies)
2210
GIT_TAG v3.7.1
2311
FIND_PACKAGE_ARGS
2412
)
25-
FetchContent_MakeAvailable(Catch2 )
13+
FetchContent_MakeAvailable(Catch2)
2614
endif()
27-
28-
2915
endmacro()

cppesphomeapi/CMakeLists.txt

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,35 @@
1-
add_executable(cppesphomeapi main.cpp)
2-
target_link_libraries(cppesphomeapi PUBLIC
1+
add_library(cppesphomeapi)
2+
generate_export_header(cppesphomeapi EXPORT_FILE_NAME cppesphomeapi/cppesphomeapi_export.hpp)
3+
target_include_directories(cppesphomeapi
4+
PUBLIC
5+
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>"
6+
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
7+
)
8+
9+
target_sources(cppesphomeapi
10+
PUBLIC
11+
FILE_SET headers
12+
TYPE HEADERS
13+
BASE_DIRS include
14+
FILES
15+
include/cppesphomeapi/api_client.hpp
16+
FILE_SET generated_headers
17+
TYPE HEADERS
18+
BASE_DIRS ${CMAKE_CURRENT_BINARY_DIR}
19+
FILES
20+
${CMAKE_CURRENT_BINARY_DIR}/cppesphomeapi/cppesphomeapi_export.hpp
21+
${CMAKE_CURRENT_BINARY_DIR}/cppesphomeapi/version.hpp
22+
)
23+
24+
25+
target_link_libraries(cppesphomeapi PUBLIC
326
Threads::Threads
427
protobuf::libprotobuf
528
Boost::headers
6-
STDEXEC::stdexec
729
)
8-
# workaround for link errors... https://github.com/grpc/grpc/issues/34046
30+
31+
## PROTOBUF START ##
32+
# workaround for link errors... https://github.com/grpc/grpc/issues/34046
933
if (Protobuf_VERSION VERSION_GREATER_EQUAL 4)
1034
find_package(absl REQUIRED)
1135
target_link_libraries (cppesphomeapi
@@ -21,4 +45,13 @@ protobuf_generate_cpp(
2145
"${proto_inc_dir}/api_options.proto"
2246
)
2347
target_sources(cppesphomeapi PRIVATE ${PROTO_SRCS} ${PROTO_HDRS})
24-
target_include_directories(cppesphomeapi PRIVATE "${CMAKE_CURRENT_BINARY_DIR}")
48+
## PROTOBUF END ##
49+
50+
# configure version file
51+
configure_file (
52+
"${CMAKE_CURRENT_SOURCE_DIR}/version.hpp.in"
53+
"${CMAKE_CURRENT_BINARY_DIR}/cppesphomeapi/version.hpp"
54+
@ONLY
55+
)
56+
57+
add_subdirectory(src)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#ifndef CPPESPHOMEAPI_API_CLIENT_HPP
2+
#define CPPESPHOMEAPI_API_CLIENT_HPP
3+
#include <cstdint>
4+
#include <memory>
5+
#include <string>
6+
#include <cppesphomeapi/cppesphomeapi_export.hpp>
7+
8+
namespace cppesphomeapi
9+
{
10+
class CPPESPHOMEAPI_EXPORT ApiClient
11+
{
12+
public:
13+
ApiClient(std::string hostname, std::uint16_t port = 6053, std::string password = "");
14+
~ApiClient();
15+
16+
ApiClient(const ApiClient &) = delete;
17+
ApiClient(ApiClient &&) = delete;
18+
ApiClient &operator=(const ApiClient &) = delete;
19+
ApiClient &operator=(ApiClient &&) = delete;
20+
21+
private:
22+
class Impl;
23+
std::unique_ptr<Impl> impl_;
24+
};
25+
} // namespace cppesphomeapi
26+
#endif
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifndef CPPESPHOMEAPI_ASYNC_RESULT_HPP
2+
#define CPPESPHOMEAPI_ASYNC_RESULT_HPP
3+
#include "detail/awaitable.hpp"
4+
#include "result.hpp"
5+
6+
namespace cppesphomeapi
7+
{
8+
template <typename T>
9+
using AsyncResult = awaitable<Result<T>>;
10+
}
11+
#endif
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef CPPESPHOMEAPI_AWAITABLE_HPP
2+
#define CPPESPHOMEAPI_AWAITABLE_HPP
3+
#include <boost/asio/awaitable.hpp>
4+
5+
namespace cppesphomeapi
6+
{
7+
using boost::asio::awaitable;
8+
}
9+
10+
#endif
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifndef CPPESPHOMEAPI_RESULT_HPP
2+
#define CPPESPHOMEAPI_RESULT_HPP
3+
#include <expected>
4+
#include <string>
5+
6+
namespace cppesphomeapi
7+
{
8+
9+
enum ApiErrorCode
10+
{
11+
ParseError,
12+
UnexpectedMessage
13+
};
14+
15+
struct ApiError
16+
{
17+
ApiErrorCode code;
18+
std::string message;
19+
};
20+
21+
template <typename T>
22+
using Result = std::expected<T, ApiError>;
23+
} // namespace cppesphomeapi
24+
#endif

cppesphomeapi/src/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
target_sources(cppesphomeapi PRIVATE
2+
api_client.cpp
3+
make_unexpected_result.cpp
4+
)

0 commit comments

Comments
 (0)