From 8d787261f12dfe711bc3bb4e088bf9bf42592b49 Mon Sep 17 00:00:00 2001
From: znone <glyc@sina.com.cn>
Date: Tue, 11 Sep 2018 03:07:25 +0000
Subject: [PATCH] 可以通过 qtl::custom_bind 把不同数据集用不同的绑定函数绑定到同一结构。

---
 include/qtl_common.hpp |   83 +++++++++++++++++++++++++++++++++++++++++
 1 files changed, 83 insertions(+), 0 deletions(-)

diff --git a/include/qtl_common.hpp b/include/qtl_common.hpp
index eb616a1..6388a17 100644
--- a/include/qtl_common.hpp
+++ b/include/qtl_common.hpp
@@ -278,6 +278,12 @@
 		command.bind_field(index, value, length);
 }
 
+template<typename Command, typename T>
+inline void bind_field(Command& command, const char* name, std::reference_wrapper<T>&& value)
+{
+	return bind_field(command, value.get());
+}
+
 namespace detail
 {
 
@@ -618,6 +624,15 @@
 	}
 };
 
+template<typename Command, typename T>
+struct record_binder<Command, std::reference_wrapper<T>>
+{
+	inline void operator()(Command& command, std::reference_wrapper<T>&& value) const
+	{
+		bind_field(command, static_cast<size_t>(0), std::forward<typename std::remove_reference<T>::type>(value.get()));
+	}
+};
+
 template<typename Command, typename... Types>
 struct record_binder<Command, std::tuple<Types...>>
 {
@@ -643,6 +658,74 @@
 {
 };
 
+template<typename T, typename Pred>
+struct custom_binder_type : public T
+{
+	typedef T value_type;
+
+	explicit custom_binder_type(Pred pred) : m_pred(pred) { }
+	custom_binder_type(value_type&& v, Pred pred)
+		: value_type(std::forward<value_type>(v)), m_pred(pred)
+	{
+	}
+	template<typename... Args>
+	custom_binder_type(Pred pred, Args&&... args)
+		: value_type(std::forward<Args>(args)...), m_pred(pred)
+	{
+	}
+
+	template<typename Command>
+	void bind(Command& command)
+	{
+		m_pred(std::forward<T>(*this), command); // Pred maybe member function
+	}
+
+private:
+	Pred m_pred;
+};
+
+template<typename T, typename Pred>
+struct custom_binder_type<std::reference_wrapper<T>, Pred> : public std::reference_wrapper<T>
+{
+	typedef std::reference_wrapper<T> value_type;
+
+	explicit custom_binder_type(Pred pred) : m_pred(pred) { }
+	custom_binder_type(value_type&& v, Pred pred)
+		: value_type(std::forward<value_type>(v)), m_pred(pred)
+	{
+	}
+	template<typename... Args>
+	custom_binder_type(Pred pred, Args&&... args)
+		: T(std::forward<Args>(args)...), m_pred(pred)
+	{
+	}
+
+	template<typename Command>
+	void bind(Command& command)
+	{
+		m_pred(std::forward<T>(*this), command); // Pred maybe member function
+	}
+
+private:
+	Pred m_pred;
+};
+
+
+template<typename T, typename Pred>
+inline custom_binder_type<T, Pred> custom_bind(T&& v, Pred pred)
+{
+	return custom_binder_type<T, Pred>(std::forward<T>(v), pred);
+}
+
+template<typename Command, typename T, typename Pred>
+struct record_binder<Command, custom_binder_type<T, Pred>>
+{
+	void operator()(Command& command, custom_binder_type<T, Pred>&& values) const
+	{
+		values.bind(command);
+	}
+};
+
 template<typename Command, typename T>
 inline void bind_record(Command& command, T&& value)
 {

--
Gitblit v1.9.3