Skip to content

Commit

Permalink
Travis config for OSX. Add convenience methods in reply class. Test r…
Browse files Browse the repository at this point in the history
…eply class. Small code clean.
  • Loading branch information
Cylix committed Oct 16, 2016
1 parent 236e062 commit 14815e1
Show file tree
Hide file tree
Showing 26 changed files with 267 additions and 59 deletions.
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ compiler:

os:
- linux
- osx

services:
- redis-server
Expand All @@ -19,8 +20,15 @@ addons:
- g++-4.8
- clang

before_install:
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew update; fi
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew install gcc48 --enable-cxx; fi
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew install llvm --with-clang; fi

install:
- if [ "$CXX" = "g++" ]; then export CXX="g++-4.8" CC="gcc-4.8"; fi
- ./install_deps.sh
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew install redis; fi
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then (redis-server&); fi

script: mkdir build && cd build && cmake .. -DBUILD_TESTS=true -DBUILD_EXAMPLES=true && make && ./bin/cpp_redis_tests
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ client.connect();

client.set("hello", "42");
client.get("hello", [](cpp_redis::reply& reply) {
std::cout << reply.as_string() << std::endl;
std::cout << reply << std::endl;
});

client.commit();
# or client.sync_commit(); for synchronous call
```
`cpp_redis::redis_client` [full documentation](https://github.com/Cylix/cpp_redis/wiki/Redis-Client) and [detailed example](https://github.com/Cylix/cpp_redis/wiki/Examples#redis-client).
More about [cpp_redis::reply](https://github.com/Cylix/cpp_redis/wiki/Replies).


`cpp_redis::redis_subscriber`:
Expand Down
8 changes: 4 additions & 4 deletions examples/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@ class my_logger : public cpp_redis::logger_iface {

public:
void
debug(const std::string& msg, const std::string& file, unsigned int line) {
debug(const std::string& msg, const std::string& file, std::size_t line) {
std::cout << "debug: " << msg << " @ " << file << ":" << line << std::endl;
}

void
info(const std::string& msg, const std::string& file, unsigned int line) {
info(const std::string& msg, const std::string& file, std::size_t line) {
std::cout << "info: " << msg << " @ " << file << ":" << line << std::endl;
}

void
warn(const std::string& msg, const std::string& file, unsigned int line) {
warn(const std::string& msg, const std::string& file, std::size_t line) {
std::cout << "warn: " << msg << " @ " << file << ":" << line << std::endl;
}

void
error(const std::string& msg, const std::string& file, unsigned int line) {
error(const std::string& msg, const std::string& file, std::size_t line) {
std::cerr << "error: " << msg << " @ " << file << ":" << line << std::endl;
}
};
Expand Down
14 changes: 11 additions & 3 deletions examples/redis_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,23 @@ main(void) {

// same as client.send({ "SET", "hello", "42" }, ...)
client.set("hello", "42", [](cpp_redis::reply& reply) {
std::cout << "set hello 42: " << reply.as_string() << std::endl;
std::cout << "set hello 42: " << reply << std::endl;
// if (reply.is_string())
// do_something_with_string(reply.as_string())
});

// same as client.send({ "DECRBY", "hello", 12 }, ...)
client.decrby("hello", 12, [](cpp_redis::reply& reply) {
std::cout << "decrby hello 12: " << reply.as_integer() << std::endl;
std::cout << "decrby hello 12: " << reply << std::endl;
// if (reply.is_integer())
// do_something_with_integer(reply.as_integer())
});

// same as client.send({ "GET", "hello" }, ...)
client.get("hello", [](cpp_redis::reply& reply) {
std::cout << "get hello: " << reply.as_string() << std::endl;
std::cout << "get hello: " << reply << std::endl;
// if (reply.is_string())
// do_something_with_string(reply.as_string())
});

// commands are pipelined and only sent when client.commit() is called
Expand Down
2 changes: 1 addition & 1 deletion includes/cpp_redis/builders/array_builder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class array_builder : public builder_iface {

private:
integer_builder m_int_builder;
unsigned int m_array_size;
std::size_t m_array_size;

std::unique_ptr<builder_iface> m_current_builder;

Expand Down
24 changes: 12 additions & 12 deletions includes/cpp_redis/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ class logger_iface {
logger_iface& operator=(const logger_iface&) = default;

public:
virtual void debug(const std::string& msg, const std::string& file, unsigned int line) = 0;
virtual void info(const std::string& msg, const std::string& file, unsigned int line) = 0;
virtual void warn(const std::string& msg, const std::string& file, unsigned int line) = 0;
virtual void error(const std::string& msg, const std::string& file, unsigned int line) = 0;
virtual void debug(const std::string& msg, const std::string& file, std::size_t line) = 0;
virtual void info(const std::string& msg, const std::string& file, std::size_t line) = 0;
virtual void warn(const std::string& msg, const std::string& file, std::size_t line) = 0;
virtual void error(const std::string& msg, const std::string& file, std::size_t line) = 0;
};

//! default logger class provided by the library
Expand All @@ -46,10 +46,10 @@ class logger : public logger_iface {
logger& operator=(const logger&) = default;

public:
void debug(const std::string& msg, const std::string& file, unsigned int line);
void info(const std::string& msg, const std::string& file, unsigned int line);
void warn(const std::string& msg, const std::string& file, unsigned int line);
void error(const std::string& msg, const std::string& file, unsigned int line);
void debug(const std::string& msg, const std::string& file, std::size_t line);
void info(const std::string& msg, const std::string& file, std::size_t line);
void warn(const std::string& msg, const std::string& file, std::size_t line);
void error(const std::string& msg, const std::string& file, std::size_t line);

private:
log_level m_level;
Expand All @@ -61,10 +61,10 @@ class logger : public logger_iface {
extern std::unique_ptr<logger_iface> active_logger;

//! convenience functions used internaly to call the logger
void debug(const std::string& msg, const std::string& file, unsigned int line);
void info(const std::string& msg, const std::string& file, unsigned int line);
void warn(const std::string& msg, const std::string& file, unsigned int line);
void error(const std::string& msg, const std::string& file, unsigned int line);
void debug(const std::string& msg, const std::string& file, std::size_t line);
void info(const std::string& msg, const std::string& file, std::size_t line);
void warn(const std::string& msg, const std::string& file, std::size_t line);
void error(const std::string& msg, const std::string& file, std::size_t line);

//! convenience macro to log with file and line information
#ifdef __CPP_REDIS_LOGGING_ENABLED
Expand Down
8 changes: 4 additions & 4 deletions includes/cpp_redis/network/io_service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class io_service {

public:
//! ctor & dtor
io_service(size_t nb_workers);
io_service(std::size_t nb_workers);
virtual ~io_service(void) = default;

//! copy ctor & assignment operator
Expand Down Expand Up @@ -53,18 +53,18 @@ class io_service {
virtual bool async_write(_sock_t sock, const std::vector<char>& buffer, std::size_t write_size, const write_callback_t& callback) = 0;

public:
size_t get_nb_workers(void) const;
std::size_t get_nb_workers(void) const;

private:
//! listen for incoming events and notify
virtual void process_io(void) = 0;

private:
size_t m_nb_workers;
std::size_t m_nb_workers;
};

//! multi-platform instance builder
std::shared_ptr<network::io_service> create_io_service(size_t nb_workers = __CPP_REDIS_DEFAULT_NB_IO_SERVICE_WORKERS);
std::shared_ptr<network::io_service> create_io_service(std::size_t nb_workers = __CPP_REDIS_DEFAULT_NB_IO_SERVICE_WORKERS);

} //! network

Expand Down
2 changes: 1 addition & 1 deletion includes/cpp_redis/network/redis_connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class redis_connection {
//! handle connection
typedef std::function<void(redis_connection&)> disconnection_handler_t;
typedef std::function<void(redis_connection&, reply&)> reply_callback_t;
void connect(const std::string& host = "127.0.0.1", unsigned int port = 6379,
void connect(const std::string& host = "127.0.0.1", std::size_t port = 6379,
const disconnection_handler_t& disconnection_handler = nullptr,
const reply_callback_t& reply_callback = nullptr);
void disconnect(void);
Expand Down
2 changes: 1 addition & 1 deletion includes/cpp_redis/network/tcp_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class tcp_client {
//! handle connection & disconnection
typedef std::function<void(tcp_client&)> disconnection_handler_t;
typedef std::function<bool(tcp_client&, const std::vector<char>& buffer)> receive_handler_t;
void connect(const std::string& host, unsigned int port,
void connect(const std::string& host, std::size_t port,
const disconnection_handler_t& disconnection_handler = nullptr,
const receive_handler_t& receive_handler = nullptr);
void disconnect(void);
Expand Down
6 changes: 3 additions & 3 deletions includes/cpp_redis/network/unix/io_service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace unix {
class io_service : public network::io_service {
public:
//! ctor & dtor
io_service(size_t nb_workers);
io_service(std::size_t nb_workers);
~io_service(void);

//! copy ctor & assignment operator
Expand Down Expand Up @@ -68,8 +68,8 @@ class io_service : public network::io_service {

private:
//! poll fds sets handling (init, rd/wr handling)
unsigned int init_sets(struct pollfd* fds);
void process_sets(struct pollfd* fds, unsigned int nfds);
std::size_t init_sets(struct pollfd* fds);
void process_sets(struct pollfd* fds, std::size_t nfds);

void read_fd(int fd);
void write_fd(int fd);
Expand Down
2 changes: 1 addition & 1 deletion includes/cpp_redis/network/windows/io_service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ typedef enum _enIoOperation {
class io_service : public network::io_service {
public:
//! ctor & dtor
io_service(size_t nb_workers);
io_service(std::size_t nb_workers);
~io_service(void);

private:
Expand Down
2 changes: 1 addition & 1 deletion includes/cpp_redis/redis_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class redis_client {
public:
//! handle connection
typedef std::function<void(redis_client&)> disconnection_handler_t;
void connect(const std::string& host = "127.0.0.1", unsigned int port = 6379,
void connect(const std::string& host = "127.0.0.1", std::size_t port = 6379,
const disconnection_handler_t& disconnection_handler = nullptr);
void disconnect(void);
bool is_connected(void);
Expand Down
2 changes: 1 addition & 1 deletion includes/cpp_redis/redis_subscriber.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class redis_subscriber {
public:
//! handle connection
typedef std::function<void(redis_subscriber&)> disconnection_handler_t;
void connect(const std::string& host = "127.0.0.1", unsigned int port = 6379,
void connect(const std::string& host = "127.0.0.1", std::size_t port = 6379,
const disconnection_handler_t& disconnection_handler = nullptr);
void disconnect(void);
bool is_connected(void);
Expand Down
12 changes: 12 additions & 0 deletions includes/cpp_redis/reply.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <iostream>
#include <string>
#include <vector>

Expand Down Expand Up @@ -52,6 +53,14 @@ class reply {
bool is_integer(void) const;
bool is_null(void) const;

//! convenience function for error handling
bool ok(void) const;
bool ko(void) const;
const std::string& error(void) const;

//! convenience implicit conversion, same as !is_null()
operator bool(void) const;

//! Value getters
const std::vector<reply>& as_array(void) const;
const std::string& as_string(void) const;
Expand All @@ -75,3 +84,6 @@ class reply {
};

} //! cpp_redis

//! support for output
std::ostream& operator<<(std::ostream& os, const cpp_redis::reply& reply);
2 changes: 1 addition & 1 deletion sources/builders/bulk_string_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ bulk_string_builder::fetch_size(std::string& buffer) {

void
bulk_string_builder::fetch_str(std::string& buffer) {
if (buffer.size() < static_cast<unsigned int>(m_str_size) + 2) // also wait for end sequence
if (buffer.size() < static_cast<std::size_t>(m_str_size) + 2) // also wait for end sequence
return;

if (buffer[m_str_size] != '\r' || buffer[m_str_size + 1] != '\n') {
Expand Down
2 changes: 1 addition & 1 deletion sources/builders/integer_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ integer_builder::operator<<(std::string& buffer) {
if (end_sequence == std::string::npos)
return *this;

unsigned int i;
std::size_t i;
for (i = 0; i < end_sequence; i++) {
//! check for negative numbers
if (!i && m_negative_multiplicator == 1 && buffer[i] == '-') {
Expand Down
16 changes: 8 additions & 8 deletions sources/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,57 +16,57 @@ logger::logger(log_level level)
: m_level(level) {}

void
logger::debug(const std::string& msg, const std::string& file, unsigned int line) {
logger::debug(const std::string& msg, const std::string& file, std::size_t line) {
if (m_level >= log_level::debug) {
std::lock_guard<std::mutex> lock(m_mutex);
std::cout << "[" << black << "DEBUG" << normal << "][cpp_redis][" << file << ":" << line << "] " << msg << std::endl;
}
}

void
logger::info(const std::string& msg, const std::string& file, unsigned int line) {
logger::info(const std::string& msg, const std::string& file, std::size_t line) {
if (m_level >= log_level::info) {
std::lock_guard<std::mutex> lock(m_mutex);
std::cout << "[" << blue << "INFO " << normal << "][cpp_redis][" << file << ":" << line << "] " << msg << std::endl;
}
}

void
logger::warn(const std::string& msg, const std::string& file, unsigned int line) {
logger::warn(const std::string& msg, const std::string& file, std::size_t line) {
if (m_level >= log_level::warn) {
std::lock_guard<std::mutex> lock(m_mutex);
std::cout << "[" << yellow << "WARN " << normal << "][cpp_redis][" << file << ":" << line << "] " << msg << std::endl;
}
}

void
logger::error(const std::string& msg, const std::string& file, unsigned int line) {
logger::error(const std::string& msg, const std::string& file, std::size_t line) {
if (m_level >= log_level::error) {
std::lock_guard<std::mutex> lock(m_mutex);
std::cerr << "[" << red << "ERROR" << normal << "][cpp_redis][" << file << ":" << line << "] " << msg << std::endl;
}
}

void
debug(const std::string& msg, const std::string& file, unsigned int line) {
debug(const std::string& msg, const std::string& file, std::size_t line) {
if (active_logger)
active_logger->debug(msg, file, line);
}

void
info(const std::string& msg, const std::string& file, unsigned int line) {
info(const std::string& msg, const std::string& file, std::size_t line) {
if (active_logger)
active_logger->info(msg, file, line);
}

void
warn(const std::string& msg, const std::string& file, unsigned int line) {
warn(const std::string& msg, const std::string& file, std::size_t line) {
if (active_logger)
active_logger->warn(msg, file, line);
}

void
error(const std::string& msg, const std::string& file, unsigned int line) {
error(const std::string& msg, const std::string& file, std::size_t line) {
if (active_logger)
active_logger->error(msg, file, line);
}
Expand Down
6 changes: 3 additions & 3 deletions sources/network/io_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ io_service::set_global_instance(const std::shared_ptr<network::io_service>& io_s
global_instance = io_service;
}

io_service::io_service(size_t nb_workers)
io_service::io_service(std::size_t nb_workers)
: m_nb_workers(nb_workers) {}

size_t
std::size_t
io_service::get_nb_workers(void) const {
return m_nb_workers;
}

std::shared_ptr<network::io_service>
create_io_service(size_t nb_workers) {
create_io_service(std::size_t nb_workers) {
#ifdef _WIN32
return std::make_shared<windows::io_service>(nb_workers);
#else
Expand Down
2 changes: 1 addition & 1 deletion sources/network/redis_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ redis_connection::~redis_connection(void) {
}

void
redis_connection::connect(const std::string& host, unsigned int port,
redis_connection::connect(const std::string& host, std::size_t port,
const disconnection_handler_t& client_disconnection_handler,
const reply_callback_t& client_reply_callback) {
__CPP_REDIS_LOG(debug, "cpp_redis::network::redis_connection attempts to connect");
Expand Down
2 changes: 1 addition & 1 deletion sources/network/tcp_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ tcp_client::setup_socket(void) {
}

void
tcp_client::connect(const std::string& host, unsigned int port,
tcp_client::connect(const std::string& host, std::size_t port,
const disconnection_handler_t& disconnection_handler,
const receive_handler_t& receive_handler) {
__CPP_REDIS_LOG(debug, "cpp_redis::network::tcp_client attempts to connect");
Expand Down
Loading

0 comments on commit 14815e1

Please sign in to comment.