1
0
Fork 0
mirror of https://github.com/KingDuckZ/dindexer.git synced 2024-11-25 00:53:43 +00:00

Make the string for pqtypes at build-time.

This commit is contained in:
King_DuckZ 2016-01-04 18:03:49 +00:00
parent c4a2433a04
commit 8b566c9fdb
4 changed files with 282 additions and 21 deletions

View file

@ -67,15 +67,12 @@ namespace pq {
using std::remove_reference;
auto make_pgparams = [&parArgs..., this](){
using implem::type_to_pqtypes_name;
std::string types;
int unpack[] {0, (types += type_to_pqtypes_name<typename remove_cv<typename remove_reference<Args>::type>::type>(), types += ' ', 0)...};
if (not types.empty()) {
types.resize(types.size() - 1);
}
static_cast<void>(unpack);
using implem::make_pqtypes_name;
using implem::concat_strings;
auto types_bt = concat_strings(make_pqtypes_name<typename remove_cv<typename remove_reference<Args>::type>::type>()...);
static_assert(types_bt.size() > 0, "Invalid empty types string (function called with no arguments?)");
const std::string types(types_bt.data(), types_bt.size());
return this->make_params(&types, implem::get_pqlib_c_type_struct<typename remove_cv<typename remove_reference<Args>::type>::type>().conv(parArgs)...);
};
PGParams pgparams = make_pgparams();

View file

@ -18,6 +18,7 @@
#ifndef idEC73C3E4D64D44ABA0DB7D41FA8A7EB7
#define idEC73C3E4D64D44ABA0DB7D41FA8A7EB7
#include "pq/implem/string_bt.hpp"
#include <chrono>
#include <type_traits>
#include <boost/utility/string_ref.hpp>
@ -25,7 +26,7 @@
namespace pq {
namespace implem {
template <typename T>
const char* type_to_pqtypes_name ( void );
struct type_to_pqtypes_name;
template <typename T>
struct get_pqlib_c_type_struct {
@ -66,18 +67,121 @@ namespace pq {
return get_pqlib_c_type_struct<T>::conv(parParam);
}
template <> inline const char* type_to_pqtypes_name<std::string>() { return "%text"; }
template <> inline const char* type_to_pqtypes_name<boost::string_ref>() { return "%text"; }
template <> inline const char* type_to_pqtypes_name<bool>() { return "%bool"; }
template <> inline const char* type_to_pqtypes_name<float>() { return "%float4"; }
template <> inline const char* type_to_pqtypes_name<double>() { return "%float8"; }
template <> inline const char* type_to_pqtypes_name<int16_t>() { return "%int2"; }
template <> inline const char* type_to_pqtypes_name<int32_t>() { return "%int4"; }
template <> inline const char* type_to_pqtypes_name<int64_t>() { return "%int8"; }
template <> inline const char* type_to_pqtypes_name<uint16_t>() { return "%int2"; }
template <> inline const char* type_to_pqtypes_name<uint32_t>() { return "%int4"; }
template <> inline const char* type_to_pqtypes_name<uint64_t>() { return "%int8"; }
template <> inline const char* type_to_pqtypes_name<std::chrono::system_clock::time_point>() { return "%timestamptz"; }
template <>
struct type_to_pqtypes_name<std::string> {
constexpr static const char* name = "text";
};
template <>
struct type_to_pqtypes_name<boost::string_ref> {
constexpr static const char* name = "text";
};
template <>
struct type_to_pqtypes_name<bool> {
constexpr static const char* name = "bool";
};
template <>
struct type_to_pqtypes_name<float> {
constexpr static const char* name = "float4";
};
template <>
struct type_to_pqtypes_name<double> {
constexpr static const char* name = "float8";
};
template <>
struct type_to_pqtypes_name<int16_t> {
constexpr static const char* name = "int2";
};
template <>
struct type_to_pqtypes_name<int32_t> {
constexpr static const char* name = "int4";
};
template <>
struct type_to_pqtypes_name<int64_t> {
constexpr static const char* name = "int8";
};
template <>
struct type_to_pqtypes_name<uint16_t> {
constexpr static const char* name = "int2";
};
template <>
struct type_to_pqtypes_name<uint32_t> {
constexpr static const char* name = "int4";
};
template <>
struct type_to_pqtypes_name<uint64_t> {
constexpr static const char* name = "int8";
};
template <>
struct type_to_pqtypes_name<std::chrono::system_clock::time_point> {
constexpr static const char* name = "timestamptz";
};
constexpr inline std::size_t strlen (const char* parStr) {
return (*parStr ? 1 + strlen(parStr + 1) : 0);
}
template <typename T>
struct type_to_pqtypes_name_impl : public type_to_pqtypes_name<T> {
constexpr static const std::size_t size = strlen(type_to_pqtypes_name<T>::name) + 1;
};
template <typename T>
constexpr bt::string<type_to_pqtypes_name_impl<T>::size + 1> make_pqtypes_name ( const char parPrefix='%' );
template <typename T>
inline constexpr
bt::string<type_to_pqtypes_name_impl<T>::size + 1> make_pqtypes_name (const char parPrefix) {
return bt::make_string<2>({parPrefix, '\0'}) + bt::string<type_to_pqtypes_name_impl<T>::size>(type_to_pqtypes_name_impl<T>::name);
}
template <std::size_t... Sizes>
struct total_string_len;
template <>
struct total_string_len<> {
constexpr static const std::size_t result = 1;
};
template <std::size_t S1, std::size_t... Sizes>
struct total_string_len<S1, Sizes...> {
constexpr static const std::size_t result = S1 - 1 + total_string_len<Sizes...>::result;
};
inline constexpr
bt::string<1> spaced_concat_strings() {
return bt::string<1>("");
}
template <std::size_t S1>
inline constexpr
bt::string<S1 + 1> spaced_concat_strings (bt::string<S1> parA1) {
return bt::make_string(" ") + parA1;
}
template <std::size_t S1, std::size_t S2, std::size_t... Sizes>
constexpr inline
bt::string<total_string_len<S1, S2, Sizes...>::result + 2 + sizeof...(Sizes)> spaced_concat_strings (bt::string<S1> parA1, bt::string<S2> parA2, bt::string<Sizes>... parArgs) {
return (
bt::make_string(" ") + parA1 +
bt::make_string(" ") + parA2 +
spaced_concat_strings(parArgs...)
);
}
inline constexpr
bt::string<1> concat_strings() {
return bt::string<1>("");
}
template <std::size_t S1>
inline constexpr
bt::string<S1> concat_strings (bt::string<S1> parA1) {
return parA1;
}
template <std::size_t S1, std::size_t S2, std::size_t... Sizes>
constexpr inline
bt::string<total_string_len<S1, S2, Sizes...>::result + 1 + sizeof...(Sizes)> concat_strings (bt::string<S1> parA1, bt::string<S2> parA2, bt::string<Sizes>... parArgs) {
return (
parA1 +
bt::make_string(" ") + parA2 +
spaced_concat_strings(parArgs...)
);
}
} //namespace implem
} //namespace pq

View file

@ -0,0 +1,48 @@
/* Copyright 2015, Michele Santullo
* This file is part of "dindexer".
*
* "dindexer" is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* "dindexer" is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with "dindexer". If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef id4FAEF395B9ED47CB9D6B50B54C9A289A
#define id4FAEF395B9ED47CB9D6B50B54C9A289A
#include <cstddef>
namespace pq {
namespace bt {
template <std::size_t... I>
struct index_seq {
};
namespace implem {
template <std::size_t MIN, std::size_t MAX, std::size_t... I>
struct range_builder;
template <std::size_t MIN, std::size_t... I>
struct range_builder<MIN, MIN, I...> {
typedef index_seq<I...> type;
};
template <std::size_t MIN, std::size_t N, std::size_t... I>
struct range_builder : public range_builder<MIN, N - 1, N - 1, I...> {
};
} //namespace implem
template <std::size_t MIN, std::size_t MAX>
using index_range = typename implem::range_builder<MIN, MAX>::type;
} //namespace bt
} //namespace pq
#endif

View file

@ -0,0 +1,112 @@
/* Copyright 2015, Michele Santullo
* This file is part of "dindexer".
*
* "dindexer" is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* "dindexer" is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with "dindexer". If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef id170B0E6C34D14EBA9B92A35977BDBFB3
#define id170B0E6C34D14EBA9B92A35977BDBFB3
#include "pq/implem/sequence_bt.hpp"
#include <cstddef>
#include <iostream>
#include <stdexcept>
namespace pq {
namespace bt {
template <std::size_t S>
class string;
template <std::size_t S>
std::ostream& operator<< ( std::ostream& parStream, const string<S>& parString );
template <std::size_t S>
class string {
public:
friend std::ostream& operator<< <>( std::ostream& parStream, const string<S>& parString );
constexpr string ( const char* parString );
constexpr std::size_t size ( void ) const { return S - 1; }
template <std::size_t S2>
constexpr string<S + S2 - 1> operator+ ( const string<S2>& parOther ) const;
constexpr char operator[] ( std::size_t parIndex ) const;
template <typename... Args>
constexpr string ( Args... );
constexpr const char (&data_arr() const)[S] { return m_data; }
constexpr const char* data() const { return m_data; }
private:
template <std::size_t... I>
constexpr string ( const index_seq<I...>&, const char* parString );
const char m_data[S];
};
namespace implem {
template <std::size_t S, std::size_t S2, std::size_t... I>
constexpr string<S + S2 - 1> concat ( const index_seq<I...>&, const string<S>& parLeft, const string<S2>& parRight ) {
return string<S + S2 - 1>(
(I < S - 1 ? parLeft[I] : (I < S + S2 - 2 ? parRight[I - (S - 1)] : '\0'))...
);
}
} //namespace implem
template <std::size_t S>
template <std::size_t... I>
constexpr string<S>::string (const index_seq<I...>&, const char* parString) :
m_data{parString[I]...}
{
}
template <std::size_t S>
inline constexpr string<S>::string (const char* parString) :
string(index_range<0, S>(), parString)
{
}
template <std::size_t S>
template <typename... Args>
inline constexpr string<S>::string (Args... parArgs) :
m_data{parArgs...}
{
}
template <std::size_t S>
template <std::size_t S2>
constexpr inline string<S + S2 - 1> string<S>::operator+ (const string<S2>& parOther) const {
return implem::concat(index_range<0, S + S2 - 1>(), string<S>(m_data), parOther);
}
template <std::size_t S>
inline std::ostream& operator<< (std::ostream& parStream, const string<S>& parString) {
parStream << parString.m_data;
return parStream;
}
template <std::size_t S>
constexpr char string<S>::operator[] (std::size_t parIndex) const {
return (parIndex < S ? m_data[parIndex] : throw std::out_of_range(""));
}
template <std::size_t S>
constexpr string<S> make_string (const char (&parData)[S]) {
return string<S>(parData);
}
} //namespace bt
} //namespace pq
#endif