Skip to content

Commit 4c1b54d

Browse files
author
expnkx
committed
c_interface
1 parent 2977a95 commit 4c1b54d

File tree

7 files changed

+205
-12
lines changed

7 files changed

+205
-12
lines changed

CMakeLists.txt

-8
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,6 @@ add_definitions(-Xclang -fconcepts-ts -ffast-math)
1717
endif()
1818

1919
set(CMAKE_CXX_STANDARD 20)
20-
add_library(fast_io SHARED src/fast_io_c_interface.cc)
21-
add_library(fast_io_static STATIC src/fast_io_c_interface.cc)
2220

23-
set_target_properties(fast_io PROPERTIES OUTPUT_NAME "fast_io_c_interface")
24-
set_target_properties(fast_io_static PROPERTIES OUTPUT_NAME "fast_io_c_interface")
2521

26-
install(TARGETS fast_io
27-
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
28-
install(TARGETS fast_io_static
29-
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
3022
INSTALL (DIRECTORY ${CMAKE_SOURCE_DIR}/include/ DESTINATION include)

examples/0030.c_interface/print.c

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include"../../include/fast_io_c_interface/fast_io_c_stdio_interface.h"
2+
3+
int main()
4+
{
5+
PRINT(stdout,"Hello World\n");
6+
PRINTLN(stdout,53454378ULL);
7+
PRINTLN(stdout,53454378.643);
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#pragma once
2+
#include<stdio.h>
3+
//This is only for C11 for better type safety. However. macro is dangerous. Use at your own risk
4+
5+
#ifdef __cplusplus
6+
extern "C"
7+
{
8+
#endif
9+
10+
void cxx_fast_io_fp_print_char(FILE*,char);
11+
void cxx_fast_io_fp_println_char(FILE*,char);
12+
void cxx_fast_io_fp_unlocked_print_char(FILE*,char);
13+
void cxx_fast_io_fp_unlocked_println_char(FILE*,char);
14+
void cxx_fast_io_fp_print_short(FILE*,short);
15+
void cxx_fast_io_fp_println_short(FILE*,short);
16+
void cxx_fast_io_fp_unlocked_print_short(FILE*,short);
17+
void cxx_fast_io_fp_unlocked_println_short(FILE*,short);
18+
void cxx_fast_io_fp_print_int(FILE*,int);
19+
void cxx_fast_io_fp_println_int(FILE*,int);
20+
void cxx_fast_io_fp_unlocked_print_int(FILE*,int);
21+
void cxx_fast_io_fp_unlocked_println_int(FILE*,int);
22+
void cxx_fast_io_fp_print_long(FILE*,long);
23+
void cxx_fast_io_fp_println_long(FILE*,long);
24+
void cxx_fast_io_fp_unlocked_print_long(FILE*,long);
25+
void cxx_fast_io_fp_unlocked_println_long(FILE*,long);
26+
void cxx_fast_io_fp_print_long_long(FILE*,long long);
27+
void cxx_fast_io_fp_println_long_long(FILE*,long long);
28+
void cxx_fast_io_fp_unlocked_print_long_long(FILE*,long long);
29+
void cxx_fast_io_fp_unlocked_println_long_long(FILE*,long long);
30+
void cxx_fast_io_fp_print_char_unsigned(FILE*,char unsigned);
31+
void cxx_fast_io_fp_println_char_unsigned(FILE*,char unsigned);
32+
void cxx_fast_io_fp_unlocked_print_char_unsigned(FILE*,char unsigned);
33+
void cxx_fast_io_fp_unlocked_println_char_unsigned(FILE*,char unsigned);
34+
void cxx_fast_io_fp_print_short_unsigned(FILE*,short unsigned);
35+
void cxx_fast_io_fp_println_short_unsigned(FILE*,short unsigned);
36+
void cxx_fast_io_fp_unlocked_print_short_unsigned(FILE*,short unsigned);
37+
void cxx_fast_io_fp_unlocked_println_short_unsigned(FILE*,short unsigned);
38+
void cxx_fast_io_fp_print_int_unsigned(FILE*,int unsigned);
39+
void cxx_fast_io_fp_println_int_unsigned(FILE*,int unsigned);
40+
void cxx_fast_io_fp_unlocked_print_int_unsigned(FILE*,int unsigned);
41+
void cxx_fast_io_fp_unlocked_println_int_unsigned(FILE*,int unsigned);
42+
void cxx_fast_io_fp_print_long_unsigned(FILE*,long unsigned);
43+
void cxx_fast_io_fp_println_long_unsigned(FILE*,long unsigned);
44+
void cxx_fast_io_fp_unlocked_print_long_unsigned(FILE*,long unsigned);
45+
void cxx_fast_io_fp_unlocked_println_long_unsigned(FILE*,long unsigned);
46+
void cxx_fast_io_fp_print_long_long_unsigned(FILE*,long long unsigned);
47+
void cxx_fast_io_fp_println_long_long_unsigned(FILE*,long long unsigned);
48+
void cxx_fast_io_fp_unlocked_print_long_long_unsigned(FILE*,long long unsigned);
49+
void cxx_fast_io_fp_unlocked_println_long_long_unsigned(FILE*,long long unsigned);
50+
void cxx_fast_io_fp_print_double(FILE*,double);
51+
void cxx_fast_io_fp_println_double(FILE*,double);
52+
void cxx_fast_io_fp_unlocked_print_double(FILE*,double);
53+
void cxx_fast_io_fp_unlocked_println_double(FILE*,double);
54+
void cxx_fast_io_fp_print_c_str(FILE*,char const*);
55+
void cxx_fast_io_fp_println_c_str(FILE*,char const*);
56+
void cxx_fast_io_fp_unlocked_print_c_str(FILE*,char const*);
57+
void cxx_fast_io_fp_unlocked_println_c_str(FILE*,char const*);
58+
59+
60+
#ifdef __cplusplus
61+
}
62+
#else
63+
64+
#define PRINT(fp,x) _Generic((x) ,char:cxx_fast_io_fp_print_char\
65+
,short:cxx_fast_io_fp_print_short\
66+
,int:cxx_fast_io_fp_print_int\
67+
,long:cxx_fast_io_fp_print_long\
68+
,long long:cxx_fast_io_fp_print_long_long\
69+
,char unsigned:cxx_fast_io_fp_print_char_unsigned\
70+
,short unsigned:cxx_fast_io_fp_print_short_unsigned\
71+
,int unsigned:cxx_fast_io_fp_print_int_unsigned\
72+
,long unsigned:cxx_fast_io_fp_print_long_unsigned\
73+
,long long unsigned:cxx_fast_io_fp_print_long_long_unsigned\
74+
,double:cxx_fast_io_fp_print_double\
75+
,char*:cxx_fast_io_fp_print_c_str)(fp,x)
76+
77+
#define PRINTLN(fp,x) _Generic((x) ,char:cxx_fast_io_fp_println_char\
78+
,short:cxx_fast_io_fp_println_short\
79+
,int:cxx_fast_io_fp_println_int\
80+
,long:cxx_fast_io_fp_println_long\
81+
,long long:cxx_fast_io_fp_println_long_long\
82+
,char unsigned:cxx_fast_io_fp_println_char_unsigned\
83+
,short unsigned:cxx_fast_io_fp_println_short_unsigned\
84+
,int unsigned:cxx_fast_io_fp_println_int_unsigned\
85+
,long unsigned:cxx_fast_io_fp_println_long_unsigned\
86+
,long long unsigned:cxx_fast_io_fp_println_long_long_unsigned\
87+
,double:cxx_fast_io_fp_println_double\
88+
,char*:cxx_fast_io_fp_println_c_str)(fp,x)
89+
90+
#define PRINT_UNLOCKED(fp,x) _Generic((x) ,char:cxx_fast_io_fp_print_unlocked_char\
91+
,short:cxx_fast_io_fp_print_unlocked_short\
92+
,int:cxx_fast_io_fp_print_unlocked_int\
93+
,long:cxx_fast_io_fp_print_unlocked_long\
94+
,long long:cxx_fast_io_fp_print_unlocked_long_long\
95+
,char unsigned:cxx_fast_io_fp_print_unlocked_char_unsigned\
96+
,short unsigned:cxx_fast_io_fp_print_unlocked_short_unsigned\
97+
,int unsigned:cxx_fast_io_fp_print_unlocked_int_unsigned\
98+
,long unsigned:cxx_fast_io_fp_print_unlocked_long_unsigned\
99+
,long long unsigned:cxx_fast_io_fp_print_unlocked_long_long_unsigned\
100+
,double:cxx_fast_io_fp_print_unlocked_double\
101+
,char*:cxx_fast_io_fp_print_unlocked_c_str)(fp,x)
102+
103+
#define PRINTLN_UNLOCKED(fp,x) _Generic((x) ,char:cxx_fast_io_fp_unlocked_println_char\
104+
,short:cxx_fast_io_fp_unlocked_println_short\
105+
,int:cxx_fast_io_fp_unlocked_println_int\
106+
,long:cxx_fast_io_fp_unlocked_println_long\
107+
,long long:cxx_fast_io_fp_unlocked_println_long_long\
108+
,char unsigned:cxx_fast_io_fp_unlocked_println_char_unsigned\
109+
,short unsigned:cxx_fast_io_fp_unlocked_println_short_unsigned\
110+
,int unsigned:cxx_fast_io_fp_unlocked_println_int_unsigned\
111+
,long unsigned:cxx_fast_io_fp_unlocked_println_long_unsigned\
112+
,long long unsigned:cxx_fast_io_fp_unlocked_println_long_long_unsigned\
113+
,double:cxx_fast_io_fp_unlocked_println_double\
114+
,char*:cxx_fast_io_fp_unlocked_println_c_str)(fp,x)
115+
#endif

include/fast_io_core_impl/integers/integer.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ inline constexpr Iter process_integer_output(Iter iter,int_type i)
2929
*iter=u8'-';
3030
++iter;
3131
}
32-
return iter+algo_decision::output_unsigned<base>(iter,-static_cast<std::make_unsigned_t<std::remove_cvref_t<int_type>>>(i));
32+
return iter+algo_decision::output_unsigned<base>(iter,static_cast<std::make_unsigned_t<std::remove_cvref_t<int_type>>>(-static_cast<std::make_unsigned_t<std::remove_cvref_t<int_type>>>(i)));
3333
}
3434
else
3535
return iter+algo_decision::output_unsigned<base>(iter,static_cast<std::make_unsigned_t<std::remove_cvref_t<int_type>>>(i));
@@ -56,7 +56,8 @@ inline constexpr Iter process_integer_output(Iter iter,int_type i)
5656
*iter=u8'-';
5757
++iter;
5858
}
59-
return iter+algo_decision::output_unsigned(iter,-static_cast<std::make_unsigned_t<std::remove_cvref_t<int_type>>>(i));
59+
return iter+algo_decision::output_unsigned(iter,
60+
static_cast<std::make_unsigned_t<std::remove_cvref_t<int_type>>>(-static_cast<std::make_unsigned_t<std::remove_cvref_t<int_type>>>(i)));
6061
}
6162
else
6263
return iter+algo_decision::output_unsigned(iter,static_cast<std::make_unsigned_t<std::remove_cvref_t<int_type>>>(i));
@@ -81,7 +82,7 @@ inline constexpr Iter process_integer_output(Iter iter,int_type i)
8182
*iter=u8'-';
8283
++iter;
8384
}
84-
return iter+algo_decision::output_unsigned<base,uppercase>(iter,-static_cast<std::make_unsigned_t<std::remove_cvref_t<int_type>>>(i));
85+
return iter+algo_decision::output_unsigned<base,uppercase>(iter,static_cast<std::make_unsigned_t<std::remove_cvref_t<int_type>>>(-static_cast<std::make_unsigned_t<std::remove_cvref_t<int_type>>>(i)));
8586
}
8687
else
8788
return iter+algo_decision::output_unsigned<base,uppercase>(iter,static_cast<std::make_unsigned_t<std::remove_cvref_t<int_type>>>(i));

src/fast_io_c_interface.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include"../include/fast_io_device.h"
2-
#include"../include/fast_io_c_interface.h"
2+
#include"../include/fast_io_c_interface/fast_io_c_interface.h"
33
#include"../include/fast_io_legacy_impl/c/impl.h"
44

55
namespace fast_io

src/fast_io_c_stdio_interface.cc

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include"../include/fast_io_hosted.h"
2+
#include"../include/fast_io_legacy_impl/c/impl.h"
3+
4+
namespace fast_io
5+
{
6+
namespace
7+
{
8+
namespace details2
9+
{
10+
template<bool unlocked,bool line,typename value_type>
11+
constexpr inline void c_interface_print(std::FILE* d,value_type value)
12+
{
13+
std::conditional_t<unlocked,c_io_observer_unlocked,c_io_observer> iob{d};
14+
if constexpr(line)
15+
println(iob,value);
16+
else
17+
print(iob,value);
18+
}
19+
}
20+
21+
}
22+
23+
24+
extern "C"
25+
{
26+
void cxx_fast_io_fp_print_char(std::FILE* d,char value){details2::c_interface_print<false,false>(d,value);}
27+
void cxx_fast_io_fp_println_char(std::FILE* d,char value){details2::c_interface_print<false,true>(d,value);}
28+
void cxx_fast_io_fp_unlocked_print_char(std::FILE* d,char value){details2::c_interface_print<true,false>(d,value);}
29+
void cxx_fast_io_fp_unlocked_println_char(std::FILE* d,char value){details2::c_interface_print<true,true>(d,value);}
30+
void cxx_fast_io_fp_print_short(std::FILE* d,short value){details2::c_interface_print<false,false>(d,value);}
31+
void cxx_fast_io_fp_println_short(std::FILE* d,short value){details2::c_interface_print<false,true>(d,value);}
32+
void cxx_fast_io_fp_unlocked_print_short(std::FILE* d,short value){details2::c_interface_print<true,false>(d,value);}
33+
void cxx_fast_io_fp_unlocked_println_short(std::FILE* d,short value){details2::c_interface_print<true,true>(d,value);}
34+
void cxx_fast_io_fp_print_int(std::FILE* d,int value){details2::c_interface_print<false,false>(d,value);}
35+
void cxx_fast_io_fp_println_int(std::FILE* d,int value){details2::c_interface_print<false,true>(d,value);}
36+
void cxx_fast_io_fp_unlocked_print_int(std::FILE* d,int value){details2::c_interface_print<true,false>(d,value);}
37+
void cxx_fast_io_fp_unlocked_println_int(std::FILE* d,int value){details2::c_interface_print<true,true>(d,value);}
38+
void cxx_fast_io_fp_print_long(std::FILE* d,long value){details2::c_interface_print<false,false>(d,value);}
39+
void cxx_fast_io_fp_println_long(std::FILE* d,long value){details2::c_interface_print<false,true>(d,value);}
40+
void cxx_fast_io_fp_unlocked_print_long(std::FILE* d,long value){details2::c_interface_print<true,false>(d,value);}
41+
void cxx_fast_io_fp_unlocked_println_long(std::FILE* d,long value){details2::c_interface_print<true,true>(d,value);}
42+
void cxx_fast_io_fp_print_long_long(std::FILE* d,long long value){details2::c_interface_print<false,false>(d,value);}
43+
void cxx_fast_io_fp_println_long_long(std::FILE* d,long long value){details2::c_interface_print<false,true>(d,value);}
44+
void cxx_fast_io_fp_unlocked_print_long_long(std::FILE* d,long long value){details2::c_interface_print<true,false>(d,value);}
45+
void cxx_fast_io_fp_unlocked_println_long_long(std::FILE* d,long long value){details2::c_interface_print<true,true>(d,value);}
46+
void cxx_fast_io_fp_print_char_unsigned(std::FILE* d,char unsigned value){details2::c_interface_print<false,false>(d,value);}
47+
void cxx_fast_io_fp_println_char_unsigned(std::FILE* d,char unsigned value){details2::c_interface_print<false,true>(d,value);}
48+
void cxx_fast_io_fp_unlocked_print_char_unsigned(std::FILE* d,char unsigned value){details2::c_interface_print<true,false>(d,value);}
49+
void cxx_fast_io_fp_unlocked_println_char_unsigned(std::FILE* d,char unsigned value){details2::c_interface_print<true,true>(d,value);}
50+
void cxx_fast_io_fp_print_short_unsigned(std::FILE* d,short unsigned value){details2::c_interface_print<false,false>(d,value);}
51+
void cxx_fast_io_fp_println_short_unsigned(std::FILE* d,short unsigned value){details2::c_interface_print<false,true>(d,value);}
52+
void cxx_fast_io_fp_unlocked_print_short_unsigned(std::FILE* d,short unsigned value){details2::c_interface_print<true,false>(d,value);}
53+
void cxx_fast_io_fp_unlocked_println_short_unsigned(std::FILE* d,short unsigned value){details2::c_interface_print<true,true>(d,value);}
54+
void cxx_fast_io_fp_print_int_unsigned(std::FILE* d,int unsigned value){details2::c_interface_print<false,false>(d,value);}
55+
void cxx_fast_io_fp_println_int_unsigned(std::FILE* d,int unsigned value){details2::c_interface_print<false,true>(d,value);}
56+
void cxx_fast_io_fp_unlocked_print_int_unsigned(std::FILE* d,int unsigned value){details2::c_interface_print<true,false>(d,value);}
57+
void cxx_fast_io_fp_unlocked_println_int_unsigned(std::FILE* d,int unsigned value){details2::c_interface_print<true,true>(d,value);}
58+
void cxx_fast_io_fp_print_long_unsigned(std::FILE* d,long unsigned value){details2::c_interface_print<false,false>(d,value);}
59+
void cxx_fast_io_fp_println_long_unsigned(std::FILE* d,long unsigned value){details2::c_interface_print<false,true>(d,value);}
60+
void cxx_fast_io_fp_unlocked_print_long_unsigned(std::FILE* d,long unsigned value){details2::c_interface_print<true,false>(d,value);}
61+
void cxx_fast_io_fp_unlocked_println_long_unsigned(std::FILE* d,long unsigned value){details2::c_interface_print<true,true>(d,value);}
62+
void cxx_fast_io_fp_print_long_long_unsigned(std::FILE* d,long long unsigned value){details2::c_interface_print<false,false>(d,value);}
63+
void cxx_fast_io_fp_println_long_long_unsigned(std::FILE* d,long long unsigned value){details2::c_interface_print<false,true>(d,value);}
64+
void cxx_fast_io_fp_unlocked_print_long_long_unsigned(std::FILE* d,long long unsigned value){details2::c_interface_print<true,false>(d,value);}
65+
void cxx_fast_io_fp_unlocked_println_long_long_unsigned(std::FILE* d,long long unsigned value){details2::c_interface_print<true,true>(d,value);}
66+
void cxx_fast_io_fp_print_double(std::FILE* d,double value){details2::c_interface_print<false,false>(d,value);}
67+
void cxx_fast_io_fp_println_double(std::FILE* d,double value){details2::c_interface_print<false,true>(d,value);}
68+
void cxx_fast_io_fp_unlocked_print_double(std::FILE* d,double value){details2::c_interface_print<true,false>(d,value);}
69+
void cxx_fast_io_fp_unlocked_println_double(std::FILE* d,double value){details2::c_interface_print<true,true>(d,value);}
70+
void cxx_fast_io_fp_print_c_str(std::FILE* d,char const* value){details2::c_interface_print<false,false>(d,value);}
71+
void cxx_fast_io_fp_println_c_str(std::FILE* d,char const* value){details2::c_interface_print<false,true>(d,value);}
72+
void cxx_fast_io_fp_unlocked_print_c_str(std::FILE* d,char const* value){details2::c_interface_print<true,false>(d,value);}
73+
void cxx_fast_io_fp_unlocked_println_c_str(std::FILE* d,char const* value){details2::c_interface_print<true,true>(d,value);}
74+
75+
76+
}
77+
}

0 commit comments

Comments
 (0)