Skip to content

Commit 9117e3a

Browse files
ZahidZafarborgbyte
andauthored
Template Function code refactor (#61)
* Refactor to in-class member initializer * Refactor member thread as unique_ptr * Refactor to use function wrapper Co-authored-by: Borgbyte <[email protected]>
1 parent bef2feb commit 9117e3a

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

examples/example_integration.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ int main() {
4040
ct.alwaysUsePost(true);
4141
ct.setDeviceID("test-device-id");
4242

43-
void (*logger_function)(Countly::LogLevel level, const std::string & message);
43+
Countly::LoggerFunction logger_function;
4444
logger_function = printLog;
4545
ct.setLogger(logger_function);
4646
// OS, OS_version, device, resolution, carrier, app_version);

include/countly.hpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "countly/constants.hpp"
55

66
#include <chrono>
7+
#include <functional>
78
#include <iterator>
89
#include <map>
910
#include <memory>
@@ -42,14 +43,16 @@ class Countly {
4243

4344
enum LogLevel {DEBUG, INFO, WARNING, ERROR, FATAL};
4445

45-
void setLogger(void (*fun)(LogLevel level, const std::string& message));
46+
using LoggerFunction = std::function<void(LogLevel, const std::string&)>;
47+
void setLogger(LoggerFunction fun);
4648

4749
struct HTTPResponse {
4850
bool success;
4951
json data;
5052
};
5153

52-
void setHTTPClient(HTTPResponse (*fun)(bool use_post, const std::string& url, const std::string& data));
54+
using HTTPClientFunction = std::function<HTTPResponse(bool, const std::string&, const std::string&)>;
55+
void setHTTPClient(HTTPClientFunction fun);
5356

5457
void setMetrics(const std::string& os, const std::string& os_version, const std::string& device, const std::string& resolution, const std::string& carrier, const std::string& app_version);
5558

@@ -245,8 +248,8 @@ class Countly {
245248

246249
void updateLoop();
247250

248-
void (*logger_function)(LogLevel level, const std::string& message) = nullptr;
249-
HTTPResponse (*http_client_function)(bool is_post, const std::string& url, const std::string& data) = nullptr;
251+
LoggerFunction logger_function;
252+
HTTPClientFunction http_client_function;
250253

251254
std::string host;
252255

@@ -259,7 +262,7 @@ class Countly {
259262
bool is_sdk_initialized = false;
260263

261264
std::chrono::system_clock::time_point last_sent_session_request;
262-
265+
263266
json session_params;
264267
std::string salt;
265268

src/countly.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ void Countly::setSalt(const std::string& value) {
5959
mutex.unlock();
6060
}
6161

62-
void Countly::setLogger(void (*fun)(Countly::LogLevel level, const std::string& message)) {
62+
void Countly::setLogger(LoggerFunction fun) {
6363
mutex.lock();
6464
logger_function = fun;
6565
mutex.unlock();
6666
}
6767

68-
void Countly::setHTTPClient(Countly::HTTPResponse (*fun)(bool use_post, const std::string& url, const std::string& data)) {
68+
void Countly::setHTTPClient(HTTPClientFunction fun) {
6969
mutex.lock();
7070
http_client_function = fun;
7171
mutex.unlock();
@@ -781,7 +781,7 @@ void Countly::setDatabasePath(const std::string& path) {
781781
#endif
782782

783783
void Countly::log(Countly::LogLevel level, const std::string& message) {
784-
if (logger_function != nullptr) {
784+
if (logger_function) {
785785
logger_function(level, message);
786786
}
787787
}
@@ -828,14 +828,14 @@ Countly::HTTPResponse Countly::sendHTTP(std::string path, std::string data) {
828828
response.success = false;
829829

830830
#ifdef COUNTLY_USE_CUSTOM_HTTP
831-
if (http_client_function == nullptr) {
831+
if (!http_client_function) {
832832
log(Countly::LogLevel::FATAL, "Missing HTTP client function");
833833
return response;
834834
}
835835

836836
return http_client_function(use_post, path, data);
837837
#else
838-
if (http_client_function != nullptr) {
838+
if (http_client_function) {
839839
return http_client_function(use_post, path, data);
840840
}
841841
#ifdef _WIN32

0 commit comments

Comments
 (0)