Joedb 10.4.3
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
type_io.h
Go to the documentation of this file.
1#ifndef joedb_type_io_declared
2#define joedb_type_io_declared
3
4#include "joedb/index_types.h"
5#include "joedb/Blob.h"
6
7#include <iostream>
8#include <stdint.h>
9
10namespace joedb
11{
12 /// @addtogroup ui
13 /// @{
14
15 std::string read_string(std::istream &in);
16 void write_string(std::ostream &out, std::string_view s, bool json = false);
17 void write_sql_string(std::ostream &out, std::string_view s);
18
19 size_t utf8_display_size(std::string_view s);
20 uint32_t read_utf8_char(size_t &i, std::string_view s);
22 (
23 std::ostream &out,
24 std::string_view s,
25 size_t width,
26 bool flush_left = true
27 );
28
29 char get_hex_char_from_digit(uint8_t n);
30 uint8_t get_hex_digit_from_char(char c);
31 void write_hexa_character(std::ostream &out, uint8_t c);
32 void write_octal_character(std::ostream &out, uint8_t c);
33
34 int8_t read_int8(std::istream &in);
35 void write_int8(std::ostream &out, int8_t value);
36
37 bool read_boolean(std::istream &in);
38 void write_boolean(std::ostream &out, bool value);
39
40 void write_blob(std::ostream &out, const Blob blob);
41 Blob read_blob(std::istream &in);
42
43 inline std::ostream &operator<<(std::ostream &out, Table_Id table_id)
44 {
45 return out << to_underlying(table_id);
46 }
47 inline std::ostream &operator<<(std::ostream &out, Field_Id field_id)
48 {
49 return out << to_underlying(field_id);
50 }
51 inline std::ostream &operator<<(std::ostream &out, Record_Id record_id)
52 {
53 return out << to_underlying(record_id);
54 }
55
56 template<typename T> std::istream &read_underlying(std::istream &in, T &value)
57 {
58 typename underlying_type<T>::type underlying{};
59 if (in >> underlying)
60 value = T(underlying);
61 return in;
62 }
63
64 inline std::istream &operator>>(std::istream &in, Table_Id &table_id)
65 {
66 return read_underlying(in, table_id);
67 }
68 inline std::istream &operator>>(std::istream &in, Field_Id &field_id)
69 {
70 return read_underlying(in, field_id);
71 }
72 inline std::istream &operator>>(std::istream &in, Record_Id &record_id)
73 {
74 return read_underlying(in, record_id);
75 }
76
77 #define PRIMITIVE_IO(type, type_id)\
78 inline type read_##type_id(std::istream &in)\
79 {type value = type(); in >> value; return value;}\
80 inline void write_##type_id(std::ostream &out, type value)\
81 {out << value;}
82
83 PRIMITIVE_IO(int32_t, int32)
84 PRIMITIVE_IO(int64_t, int64)
86 PRIMITIVE_IO(float, float32)
87 PRIMITIVE_IO(double, float64)
88 PRIMITIVE_IO(int16_t, int16)
89
90 #undef PRIMITIVE_IO
91
92 /// @}
93}
94
95#endif
void write_octal_character(std::ostream &out, uint8_t c)
void write_justified(std::ostream &out, const std::string_view s, size_t width, bool flush_left)
Definition type_io.cpp:161
std::istream & operator>>(std::istream &in, Table_Id &table_id)
Definition type_io.h:64
uint8_t get_hex_digit_from_char(char c)
Definition type_io.cpp:68
void write_boolean(std::ostream &out, bool value)
Definition type_io.cpp:233
void write_sql_string(std::ostream &out, const std::string_view s)
Definition type_io.cpp:90
void write_hexa_character(std::ostream &out, uint8_t c)
Definition type_io.cpp:80
bool read_boolean(std::istream &in)
Definition type_io.cpp:215
int8_t read_int8(std::istream &in)
Definition type_io.cpp:206
void write_int8(std::ostream &out, int8_t value)
size_t utf8_display_size(const std::string_view s)
Definition type_io.cpp:104
std::ostream & operator<<(std::ostream &out, Table_Id table_id)
Definition type_io.h:43
void write_blob(std::ostream &out, Blob blob)
Definition type_io.cpp:243
#define PRIMITIVE_IO(type, type_id)
Definition type_io.h:77
std::istream & read_underlying(std::istream &in, T &value)
Definition type_io.h:56
uint32_t read_utf8_char(size_t &i, const std::string_view s)
Definition type_io.cpp:121
void write_string(std::ostream &out, std::string_view s, bool json)
char get_hex_char_from_digit(uint8_t n)
std::string read_string(std::istream &in)
Definition type_io.cpp:12
Blob read_blob(std::istream &in)
Definition type_io.cpp:250
constexpr index_t to_underlying(Record_Id id)
Definition index_types.h:59
typename std::underlying_type< T >::type type
Definition index_types.h:51