Skip to content

Commit 572cd21

Browse files
- PIMPL usisng spimpl.h
- Fix a compilation in Windows (no gcov) - All is const - More sonar refactors
1 parent 2f8092a commit 572cd21

22 files changed

+729
-115
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ project( consulcpp VERSION 0.1.1 DESCRIPTION "A C++ library that implements the
99

1010
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
1111
include( CTest )
12-
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
12+
if(CMAKE_BUILD_TYPE STREQUAL "Debug" AND UNIX)
1313
include( CodeCoverage )
1414
append_coverage_compiler_flags()
1515
setup_target_for_coverage_gcovr(

README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ int main( int argc, char * argv[] )
1818
{
1919
consulcpp::Consul consul;
2020

21-
if( consul.connect() ){
21+
if( consulcpp::Consul consul; consul.connect() ) {
2222
consulcpp::Service service;
2323
consulcpp::ServiceCheck check;
2424

2525
service.mName = "demo";
2626
service.mAddress = consul.address();
27-
service.mPort = 50051;
27+
service.mPort = 9990;
2828

2929
check.mInterval = "10s";
3030
check.mGRPC = fmt::format( "{}:{}/Health", service.mAddress, service.mPort );
@@ -34,8 +34,7 @@ int main( int argc, char * argv[] )
3434

3535
consulcpp::Session session = consul.sessions().create();
3636

37-
consulcpp::Leader::Status leader = consul.leader().acquire( service, session );
38-
if( leader == consulcpp::Leader::Status::Yes ){
37+
if( consulcpp::Leader::Status leader = consul.leader().acquire( service, session ); leader == consulcpp::Leader::Status::Yes ){
3938
// I'm the leader
4039
}else{
4140
// I'm a follower
@@ -70,8 +69,8 @@ target_link_libraries(main PRIVATE consulcpp)
7069

7170
## Other Consul Clients in C++
7271

72+
- [Ppconsul](https://github.com/oliora/ppconsul): C++ client for Consul. You should probably use this instead :)
7373
- [oatpp-consul](https://github.com/oatpp/oatpp-consul): C++ Consul integration for oatpp applications
74-
- [Ppconsul](https://github.com/oliora/ppconsul): C++ client for Consul
7574

7675
## Quality Checks
7776

cmake/Tools.cmake

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ function(sonarqube_setup)
5858
else()
5959
find_program( buildwrapper_tmp build-wrapper-linux-x86-64 )
6060
endif()
61+
if( UNIX )
62+
set( BUILD_TARGET coverage )
63+
endif()
64+
6165
if( buildwrapper_tmp )
6266
set( BUILDWRAPPER_EXECUTABLE ${buildwrapper_tmp})
6367
unset( buildwrapper_tmp )
@@ -67,8 +71,8 @@ function(sonarqube_setup)
6771
${CMAKE_MAKE_PROGRAM} clean
6872
COMMAND
6973
${BUILDWRAPPER_EXECUTABLE}
70-
--out-dir bw-output
71-
${CMAKE_MAKE_PROGRAM} coverage
74+
--out-dir bw-output
75+
${CMAKE_MAKE_PROGRAM} ${BUILD_TARGET}
7276
COMMENT
7377
"Run sonarqube build"
7478
WORKING_DIRECTORY

examples/leader/main.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@ void TestService( consulcpp::Service & service )
4949
spdlog::critical( "Cannot start gRPC server on {}:{}", service.mAddress, service.mPort );
5050
}
5151
}
52+
5253
int main( int argc, char * argv[] )
5354
{
54-
unsigned short port = 50051;
55+
unsigned short port = 9990;
5556

5657
if( argc > 1 ) {
5758
if( auto maybePort = consulcpp::utils::asPort( argv[1] ); maybePort ){

include/consulcpp/Catalog.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <optional>
77

88
#include "consulcpp/Export.h"
9+
#include "consulcpp/spimpl.h"
910

1011
namespace consulcpp {
1112

@@ -18,15 +19,14 @@ class ConsulCPP_API Catalog
1819
{
1920
public:
2021
explicit Catalog( Consul & consul );
21-
~Catalog();
2222

2323
/*! Looks for a service in the catalog
2424
*/
2525
std::vector<Service> find( std::string_view name, const std::vector<std::string> & tags = {} ) const;
2626

2727
private:
2828
struct Private;
29-
std::unique_ptr<Private> d;
29+
spimpl::impl_ptr<Private> d;
3030
};
3131

3232
}

include/consulcpp/Consul.h

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <memory>
55

66
#include "consulcpp/Export.h"
7+
#include "consulcpp/spimpl.h"
78

89
namespace consulcpp {
910

@@ -20,15 +21,12 @@ class KV;
2021
class ConsulCPP_API Consul
2122
{
2223
public:
23-
//! Creates a consul services object pointing to a local agent at http://127.0.0.1:8500
24-
Consul();
25-
//! Creates a consul services object pointing to a local agent at "agentAddress"
26-
explicit Consul( std::string_view agentAddress );
27-
~Consul();
24+
//! Creates a consul services object
25+
Consul();
2826

29-
/*! Connects to the local agent
27+
/*! Connects to an agent
3028
*/
31-
[[nodiscard]] bool connect();
29+
[[nodiscard]] bool connect( std::string_view agentAddress = "http://127.0.0.1:8500" );
3230

3331
//! Address where consul agent is running
3432
[[nodiscard]] std::string address() const;
@@ -38,19 +36,19 @@ class ConsulCPP_API Consul
3836
[[nodiscard]] std::string agentAPIVersion() const;
3937

4038
// Services access
41-
[[nodiscard]] Services & services() const;
39+
[[nodiscard]] const Services & services() const;
4240
// Catalog access
43-
[[nodiscard]] Catalog & catalog() const;
41+
[[nodiscard]] const Catalog & catalog() const;
4442
// Sessions access
45-
[[nodiscard]] Sessions & sessions() const;
43+
[[nodiscard]] const Sessions & sessions() const;
4644
// Leader election access
47-
[[nodiscard]] Leader & leader() const;
45+
[[nodiscard]] const Leader & leader() const;
4846
// KV Storer access
49-
[[nodiscard]] KV & kv() const;
47+
[[nodiscard]] const KV & kv() const;
5048

5149
private:
5250
struct Private;
53-
std::unique_ptr<Private> d;
51+
spimpl::impl_ptr<Private> d;
5452
};
5553

5654
}

include/consulcpp/KV.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <optional>
66

77
#include "consulcpp/Export.h"
8+
#include "consulcpp/spimpl.h"
89

910
namespace consulcpp {
1011

@@ -16,7 +17,6 @@ class ConsulCPP_API KV
1617
{
1718
public:
1819
explicit KV( Consul & consul );
19-
~KV();
2020

2121
/*! Returns the value of a single key
2222
*/
@@ -32,7 +32,7 @@ class ConsulCPP_API KV
3232

3333
private:
3434
struct Private;
35-
std::unique_ptr<Private> d;
35+
spimpl::impl_ptr<Private> d;
3636
};
3737

3838
}

include/consulcpp/Leader.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <memory>
55

66
#include "consulcpp/Export.h"
7+
#include "consulcpp/spimpl.h"
78

89
namespace consulcpp {
910

@@ -17,7 +18,6 @@ class ConsulCPP_API Leader
1718
{
1819
public:
1920
explicit Leader( Consul & consul );
20-
~Leader();
2121

2222
/*! Leader status:
2323
- Yes: you are the leader
@@ -43,7 +43,7 @@ class ConsulCPP_API Leader
4343

4444
private:
4545
struct Private;
46-
std::unique_ptr<Private> d;
46+
spimpl::impl_ptr<Private> d;
4747
};
4848

4949
}

include/consulcpp/Service.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ extern ConsulCPP_API void from_json( const nlohmann::json & j, ServiceCheck & s
5858
*/
5959
struct ConsulCPP_API Service
6060
{
61+
Service() = default;
62+
Service( const Service & a ) = default;
63+
Service( Service && a) noexcept = default;
64+
~Service() = default;
65+
Service & operator=( const Service & s ) = default;
66+
Service & operator=( Service && a) noexcept = default;
67+
6168
// In Consul an empty id is valid and the name is used instead
6269
[[nodiscard]] const std::string & id() const
6370
{

include/consulcpp/Services.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <optional>
77

88
#include "consulcpp/Export.h"
9+
#include "consulcpp/spimpl.h"
910

1011
namespace consulcpp {
1112

@@ -18,7 +19,6 @@ class ConsulCPP_API Services
1819
{
1920
public:
2021
explicit Services( Consul & consul );
21-
~Services();
2222

2323
/*! Registers a new service.
2424
*/
@@ -33,7 +33,7 @@ class ConsulCPP_API Services
3333

3434
private:
3535
struct Private;
36-
std::unique_ptr<Private> d;
36+
spimpl::impl_ptr<Private> d;
3737
};
3838

3939
}

include/consulcpp/Sessions.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <memory>
55

66
#include "consulcpp/Export.h"
7+
#include "consulcpp/spimpl.h"
78

89
namespace consulcpp {
910

@@ -20,7 +21,6 @@ class ConsulCPP_API Sessions
2021
{
2122
public:
2223
explicit Sessions( Consul & consul );
23-
~Sessions();
2424

2525
/*! Creates a new session
2626
*/
@@ -32,7 +32,7 @@ class ConsulCPP_API Sessions
3232

3333
private:
3434
struct Private;
35-
std::unique_ptr<Private> d;
35+
spimpl::impl_ptr<Private> d;
3636
};
3737

3838
}

0 commit comments

Comments
 (0)