Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include <exception>
#include <functional>
#include <future>
namespace datastax {
class StringRef {
public:
StringRef(const char* str) {}
};
class NotImplementedException : public std::exception {};
template <typename T>
class Maybe {
public:
T get() { throw NotImplementedException(); }
};
class Runnable {
public:
virtual ~Runnable() = default;
virtual void run_and_delete() = 0;
};
template <typename F>
class FunctorRunnable : public Runnable {
public:
FunctorRunnable(F&& func)
: func_(std::move(func)) {}
virtual void run_and_delete() {
func_();
}
private:
F func_;
};
class ThreadManager {
public:
virtual ~ThreadManager() = default;
virtual void create_thread(std::unique_ptr<Runnable> runnable) = 0;
template <typename F>
void create_thread_from_func(F&& func) {
create_thread(std::unique_ptr<Runnable>(new FunctorRunnable<F>(std::forward<F>(func))));
}
virtual void join() = 0;
};
class ThreadPool {
public:
ThreadPool(size_t num_threads = 0) { }
ThreadPool(std::unique_ptr<ThreadManager> thread_manager, size_t num_threads = 0) { }
~ThreadPool() { join(); }
void join() { thread_manager_->join(); }
private:
std::unique_ptr<ThreadManager> thread_manager_;
};
class Session {};
typedef std::future<Session> SessionConnectFuture;
typedef std::function<void(Maybe<Session>)> SessionConnectCallback;
class SessionConfig {
public:
Session connect(ThreadPool& thread_pool) { throw NotImplementedException(); }
SessionConnectFuture connect_async(ThreadPool& thread_pool, StringRef keyspace = "") {
throw NotImplementedException();
}
void connect_async_callback(ThreadPool& thread_pool, SessionConnectCallback callback,
StringRef keyspace = "") {
throw NotImplementedException();
}
};
class SessionConfigBuilder {
public:
SessionConfigBuilder(StringRef contact_points) {}
SessionConfig build() const { throw NotImplementedException(); }
};
} // namespace datastax
using namespace datastax;
int main() {
ThreadPool thread_pool; // Destructor automatically joins threads
{ // Synchronous
Session session = SessionConfigBuilder("127.0.0.1").build().connect(thread_pool);
}
{ // Future
SessionConnectFuture future =
SessionConfigBuilder("127.0.0.1").build().connect_async(thread_pool);
Session session = future.get();
}
{ // Callback
SessionConfigBuilder("127.0.0.1")
.build()
.connect_async_callback(thread_pool,
[](Maybe<Session> result) { Session session = result.get(); });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment