Atlas Game Manager
A game manager for f95 and dlsite written in c++
Loading...
Searching...
No Matches
extractors.hpp
Go to the documentation of this file.
1//
2// Created by kj16609 on 9/8/23.
3//
4
5#pragma once
6#ifndef ATLASGAMEMANAGER_EXTRACTORS_HPP
7#define ATLASGAMEMANAGER_EXTRACTORS_HPP
8
9#include <concepts>
10#include <sqlite3.h>
11
12template < std::uint64_t, typename T >
13void extract( sqlite3_stmt*, T& ) = delete;
14
15template < std::uint64_t index, typename T >
16 requires std::is_integral_v< T >
17void extract( sqlite3_stmt* stmt, T& t ) noexcept
18{
19 //If the size of type T is bigger then 32 bits. Use int64
20 if constexpr ( sizeof( T ) > ( 32 / 8 ) )
21 {
22 if constexpr ( std::same_as< T, sqlite3_int64 > )
23 t = sqlite3_column_int64( stmt, index );
24 else
25 t = static_cast< T >( sqlite3_column_int64( stmt, index ) );
26 }
27 else
28 {
29 if constexpr ( std::same_as< T, int > )
30 t = sqlite3_column_int( stmt, index );
31 else
32 t = static_cast< T >( sqlite3_column_int( stmt, index ) );
33 }
34}
35
36template < std::uint64_t index, typename T >
37 requires std::is_same_v< T, std::u8string >
38void extract( sqlite3_stmt* stmt, std::u8string& t ) noexcept
39{
40 const unsigned char* const txt { sqlite3_column_text( stmt, index ) };
41
42 if ( txt == nullptr )
43 {
44 t = {};
45 return;
46 }
47 else
48 {
49 const auto len { strlen( reinterpret_cast< const char* const >( txt ) ) };
50 t = { reinterpret_cast< const char8_t* >( txt ), len };
51 return;
52 }
53}
54
55template < std::uint64_t index, typename T >
56 requires std::is_same_v< T, QString >
57void extract( sqlite3_stmt* stmt, QString& t ) noexcept
58{
59 const unsigned char* const txt { sqlite3_column_text( stmt, index ) };
60
61 if ( txt == nullptr )
62 {
63 t = {};
64 return;
65 }
66 else
67 {
68 t = QString::fromUtf8( txt );
69 return;
70 }
71}
72
73template < std::uint64_t index, typename T >
74 requires std::is_same_v< T, std::filesystem::path >
75void extract( sqlite3_stmt* stmt, std::filesystem::path& t ) noexcept
76{
77 std::u8string str;
79 t = { std::move( str ) };
80}
81
82template < std::uint64_t index, typename T >
83 requires std::is_same_v< T, std::string >
84void extract( sqlite3_stmt* stmt, std::string& t ) noexcept
85{
86 QString str;
87 extract< index, QString >( stmt, str );
88 t = { str.toStdString() };
89}
90
91template < std::uint64_t index, typename T >
92 requires std::is_same_v< T, std::vector< std::byte > >
93void extract( sqlite3_stmt* stmt, std::vector< std::byte >& t ) noexcept
94{
95 const void* data { sqlite3_column_blob( stmt, index ) };
96 const std::size_t size { static_cast< std::size_t >( sqlite3_column_bytes( stmt, index ) ) };
97
98 t.resize( size );
99 std::memcpy( t.data(), data, size );
100}
101
102template < std::uint64_t index, typename... Args >
103void extractRow( sqlite3_stmt* stmt, std::tuple< Args... >& tpl ) noexcept
104{
105 auto& ref { std::get< index >( tpl ) };
106 extract< index, std::remove_reference_t< decltype( ref ) > >( stmt, ref );
107
108 if constexpr ( index < sizeof...( Args ) - 1 ) extractRow< index + 1, Args... >( stmt, tpl );
109}
110#endif //ATLASGAMEMANAGER_EXTRACTORS_HPP
void extractRow(sqlite3_stmt *stmt, std::tuple< Args... > &tpl) noexcept
Definition extractors.hpp:103
void extract(sqlite3_stmt *, T &)=delete
Definition extractors.hpp:17