YSTest: NativeAPI.h 源文件

YSTest PreAlpha_b900_20201012 The YSLib Test Project

NativeAPI.h
浏览该文件的文档.
1 /*
2 © 2011-2017, 2019 FrankHB.
3
4 This file is part of the YSLib project, and may only be used,
5 modified, and distributed under the terms of the YSLib project
6 license, LICENSE.TXT. By continuing to use, modify, or distribute
7 this file you indicate that you have read the license and
8 understand and accept it fully.
9 */
10
28 #ifndef YCL_INC_NativeAPI_h_
29 #define YCL_INC_NativeAPI_h_ 1
30
31 #include "YModules.h"
32 #include YFM_YCLib_Platform // for yconstfn, YF_API;
33 #include <ystdex/type_op.hpp> // for ystdex::make_signed_t, std::is_signed;
34 #include YFM_YBaseMacro // for DefBitmaskEnum, PDefH, ImplRet;
35
36 #ifndef YF_Platform
37 # error "Unknown platform found."
38 #endif
39
52
54
61 #if YCL_Win32
62 # define YCL_ReservedGlobal(_n) _##_n
63 #else
64 # define YCL_ReservedGlobal(_n) _n
65 #endif
66 #define YCL_CallGlobal(_n, ...) ::YCL_ReservedGlobal(_n)(__VA_ARGS__)
68
69
70
71 #include <stdio.h>
72 #if YCL_API_Has_dirent_h
73 # include <dirent.h>
74 #endif
75 // TODO: Test whether <fcntl.h> is available.
76 #include <fcntl.h>
77 #if YCL_API_Has_semaphore_h
78 # include <semaphore.h>
79 #endif
80 #if YCL_API_Has_unistd_h
81 # include <unistd.h>
83
84 namespace platform
85 {
86 using ssize_t = ::ssize_t;
87 } // namespace platform_ex;
88 # else
89 namespace platform
90 {
91 # if YCL_Win32
92 using ssize_t = int;
93 # else
94 using ssize_t = ystdex::make_signed_t<std::size_t>;
95 # endif
96 } // namespace platform;
97
98 static_assert(std::is_signed<platform::ssize_t>(),
99 "Invalid signed size type found.");
101 #endif
102
103
104 #if YCL_Linux || YCL_OS_X
105 # include <sys/mman.h>
106 #endif
107
108
109 #if YCL_Win32 || YCL_API_POSIXFileSystem
110 # include <sys/stat.h> // for struct ::stat;
111
112 namespace platform
113 {
114
116 enum class Mode
117 # if YCL_Win32
118 : unsigned short
119 # else
120 : ::mode_t
121 # endif
122 {
123 # if YCL_Win32
124 FileType = _S_IFMT,
125 Directory = _S_IFDIR,
126 Character = _S_IFCHR,
127 FIFO = _S_IFIFO,
128 Regular = _S_IFREG,
129 UserRead = _S_IREAD,
130 UserWrite = _S_IWRITE,
131 UserExecute = _S_IEXEC,
132 GroupRead = _S_IREAD >> 3,
133 GroupWrite = _S_IWRITE >> 3,
134 GroupExecute = _S_IEXEC >> 3,
135 OtherRead = _S_IREAD >> 6,
136 OtherWrite = _S_IWRITE >> 6,
137 OtherExecute = _S_IEXEC >> 6,
138 # else
139 FileType = S_IFMT,
140 Directory = S_IFDIR,
141 Character = S_IFCHR,
142 Block = S_IFBLK,
143 Regular = S_IFREG,
144 Link = S_IFLNK,
145 Socket = S_IFSOCK,
146 FIFO = S_IFIFO,
147 UserRead = S_IRUSR,
148 UserWrite = S_IWUSR,
149 UserExecute = S_IXUSR,
150 GroupRead = S_IRGRP,
151 GroupWrite = S_IWGRP,
152 GroupExecute = S_IXGRP,
153 OtherRead = S_IROTH,
154 OtherWrite = S_IWOTH,
155 OtherExecute = S_IXOTH,
156 # endif
157 UserReadWrite = UserRead | UserWrite,
158 User = UserReadWrite | UserExecute,
159 GroupReadWrite = GroupRead | GroupWrite,
160 Group = GroupReadWrite | GroupExecute,
161 OtherReadWrite = OtherRead | OtherWrite,
162 Other = OtherReadWrite | OtherExecute,
163 Read = UserRead | GroupRead | OtherRead,
164 Write = UserWrite | GroupWrite | OtherWrite,
165 Execute = UserExecute | GroupExecute | OtherExecute,
166 ReadWrite = Read | Write,
167 Access = ReadWrite | Execute,
169
170 # if !YCL_Win32
171 SetUserID = S_ISUID,
172 SetGroupID = S_ISGID,
173 # else
174 SetUserID = 0,
175 SetGroupID = 0,
176 # endif
177 # if YCL_Linux || _XOPEN_SOURCE
178 VTX = S_ISVTX,
179 # else
180 VTX = 0,
181 # endif
182 PMode = SetUserID | SetGroupID | VTX | Access,
183 All = PMode | FileType
185 };
186
188
189 DefBitmaskEnum(Mode)
191
192 yconstfn PDefH(bool, HasExtraMode, Mode m)
194 ImplRet(bool(m & ~(Mode::Access | Mode::FileType)))
196
197
198
200 enum class OpenMode : int
202 {
203 #define YCL_Impl_OMode(_n, _nm) _n = YCL_ReservedGlobal(O_##_nm)
204 #if YCL_Win32
205 # define YCL_Impl_OMode_POSIX(_n, _nm) _n = 0
206 # define YCL_Impl_OMode_Win32(_n, _nm) YCL_Impl_OMode(_n, _nm)
207 #else
208 # define YCL_Impl_OMode_POSIX(_n, _nm) YCL_Impl_OMode(_n, _nm)
209 # define YCL_Impl_OMode_Win32(_n, _nm) _n = 0
210 #endif
211 #if O_CLOEXEC
212 YCL_Impl_OMode_POSIX(CloseOnExec, CLOEXEC),
213 #endif
214 YCL_Impl_OMode(Create, CREAT),
215 #if O_DIRECTORY
216 YCL_Impl_OMode_POSIX(Directory, DIRECTORY),
217 #else
218 // XXX: Platform %DS does not support this.
219 Directory = 0,
220 #endif
221 YCL_Impl_OMode(Exclusive, EXCL),
222 CreateExclusive = Create | Exclusive,
223 YCL_Impl_OMode_POSIX(NoControllingTerminal, NOCTTY),
224 #if O_NOFOLLOW
225 YCL_Impl_OMode_POSIX(NoFollow, NOFOLLOW),
226 #else
227 // NOTE: Platform %DS does not support links.
228 // NOTE: Platform %Win32 does not support links for these APIs.
229 NoFollow = 0,
230 #endif
231 YCL_Impl_OMode(Truncate, TRUNC),
232 #if O_TTY_INIT
233 YCL_Impl_OMode_POSIX(TerminalInitialize, TTY_INIT),
234 #endif
235 YCL_Impl_OMode(Append, APPEND),
236 #if O_DSYNC
237 YCL_Impl_OMode_POSIX(DataSynchronized, DSYNC),
238 #endif
239 YCL_Impl_OMode_POSIX(Nonblocking, NONBLOCK),
240 #if O_RSYNC
241 YCL_Impl_OMode_POSIX(ReadSynchronized, RSYNC),
242 #endif
243 YCL_Impl_OMode_POSIX(Synchronized, SYNC),
244 #if O_EXEC
245 YCL_Impl_OMode_POSIX(Execute, EXEC),
246 #endif
247 YCL_Impl_OMode(Read, RDONLY),
248 YCL_Impl_OMode(ReadWrite, RDWR),
249 #if O_SEARCH
250 YCL_Impl_OMode_POSIX(Search, SEARCH),
251 #endif
252 YCL_Impl_OMode(Write, WRONLY),
253 ReadWriteAppend = ReadWrite | Append,
254 ReadWriteTruncate = ReadWrite | Truncate,
255 WriteAppend = Write | Append,
256 WriteTruncate = Write | Truncate,
257 YCL_Impl_OMode_Win32(Text, TEXT),
258 YCL_Impl_OMode_Win32(Binary, BINARY),
259 Raw = Binary,
260 ReadRaw = Read | Raw,
261 ReadWriteRaw = ReadWrite | Raw,
262 // NOTE: On GNU/Hurd %O_ACCMODE can be zero.
263 #if O_ACCMODE
264 YCL_Impl_OMode_POSIX(AccessMode, ACCMODE),
265 #else
266 AccessMode = Read | Write | ReadWrite,
267 #endif
268 YCL_Impl_OMode_Win32(WText, WTEXT),
269 YCL_Impl_OMode_Win32(U16Text, U16TEXT),
270 YCL_Impl_OMode_Win32(U8Text, U8TEXT),
271 YCL_Impl_OMode_Win32(NoInherit, NOINHERIT),
272 YCL_Impl_OMode_Win32(Temporary, TEMPORARY),
273 YCL_Impl_OMode_Win32(ShortLived, SHORT_LIVED),
274 CreateTemporary = Create | Temporary,
275 CreateShortLived = Create | ShortLived,
276 YCL_Impl_OMode_Win32(Sequential, SEQUENTIAL),
277 YCL_Impl_OMode_Win32(Random, RANDOM),
278 #if O_NDELAY
279 YCL_Impl_OMode(NoDelay, NDELAY),
280 #endif
281
283 #if O_LARGEFILE
284 YCL_Impl_OMode(LargeFile, LARGEFILE),
286 #else
287 LargeFile = 0,
288 #endif
289 #if _O_OBTAIN_DIR
290 YCL_Impl_OMode(ObtainDirectory, OBTAIN_DIR),
291 #else
292 ObtainDirectory = Directory,
294 #endif
295
296 None = 0
297 #undef YCL_Impl_OMode_POSIX
298 #undef YCL_Impl_OMode_Win32
299 #undef YCL_Impl_OMode
300 };
301
303 DefBitmaskEnum(OpenMode)
305
306 } // namespace platform;
307
309 namespace platform_ex
310 {
311
325 YF_API YB_NONNULL(2, 4) void
326 cstat(struct ::stat&, const char*, bool, const char*);
327 YF_API YB_NONNULL(3) void
328 cstat(struct ::stat&, int, const char*);
330
332 YF_API YB_NONNULL(2) int
333 estat(struct ::stat&, const char*, bool) ynothrowv;
334 inline PDefH(int, estat, struct ::stat& st, int fd) ynothrow
335 ImplRet(::fstat(fd, &st))
337
338 } // namespace platform_ex;
339 #endif
340
341
348
350 #undef ctermid
351 #undef dprintf
352 #undef fdopen
353 #undef fileno
355 #undef flockfile
356 #undef fmemopen
357 #undef fseeko
358 #undef ftello
359 #undef ftrylockfile
360 #undef funlockfile
361 #undef getc_unlocked
362 #undef getchar_unlocked
363 #undef getdelim
364 #undef getline
365 #undef open_memstream
366 #undef pclose
367 #undef popen
368 #undef putc_unlocked
369 #undef putchar_unlocked
370 #undef renameat
371 #undef tempnam
372 #undef vdprintf
373
374
376 #undef alphasort
377 #undef closedir
378 #undef dirfd
379 #undef fdopendir
380 #undef opendir
381 #undef readdir
382 #undef readdir_r
383 #undef rewinddir
384 #undef scandir
385 #undef seekdir
386 #undef telldir
387
388
390 #undef creat
391 #undef fcntl
392 #undef open
393 #undef openat
394 #undef posix_fadvise
395 #undef posix_fallocate
396
397 #undef sem_close
399 #undef sem_destroy
400 #undef sem_getvalue
401 #undef sem_init
402 #undef sem_open
403 #undef sem_post
404 #undef sem_timedwait
405 #undef sem_trywait
406 #undef sem_unlink
407 #undef sem_wait
408
410 #undef _exit
411 #undef access
412 #undef alarm
413 #undef chdir
414 #undef chown
415 #undef close
416 #undef confstr
417 #undef crypt
418 #undef dup
419 #undef dup2
420 #undef encrypt
421 #undef execl
422 #undef execle
423 #undef execlp
424 #undef execv
425 #undef execve
426 #undef execvp
427 #undef faccessat
428 #undef fchdir
429 #undef fchown
430 #undef fchownat
431 #undef fdatasync
432 #undef fexecve
433 #undef fork
434 #undef fpathconf
435 #undef fsync
436 #undef ftruncate
437 #undef getcwd
438 #undef getegid
439 #undef geteuid
440 #undef getgid
441 #undef getgroups
442 #undef gethostid
443 #undef gethostname
444 #undef getlogin
445 #undef getlogin_r
446 #undef getopt
447 #undef getpgid
448 #undef getpgrp
449 #undef getpid
450 #undef getppid
451 #undef getsid
452 #undef getuid
453 #undef isatty
454 #undef lchown
455 #undef link
456 #undef linkat
457 #undef lockf
458 #undef lseek
459 #undef nice
460 #undef pathconf
461 #undef pause
462 #undef pipe
463 #undef pread
464 #undef pwrite
465 #undef read
466 #undef readlink
467 #undef readlinkat
468 #undef rmdir
469 #undef setegid
470 #undef seteuid
471 #undef setgid
472 #undef setpgid
473 #undef setpgrp
474 #undef setregid
475 #undef setreuid
476 #undef setsid
477 #undef setuid
478 #undef sleep
479 #undef swab
480 #undef symlink
481 #undef symlinkat
482 #undef sync
483 #undef sysconf
484 #undef tcgetpgrp
485 #undef tcsetpgrp
486 #undef truncate
487 #undef ttyname
488 #undef ttyname_r
489 #undef unlink
490 #undef unlinkat
491 #undef write
492
493
495 #undef mlock
496 #undef mlockall
497 #undef mmap
498 #undef mprotect
499 #undef msync
500 #undef munlock
501 #undef munlockall
502 #undef munmap
503 #undef posix_madvise
504 #undef posix_mem_offset
505 #undef posix_typed_mem_get_info
506 #undef posix_typed_mem_open
507 #undef shm_open
508 #undef shm_unlink
509
510
512 #undef chmod
513 #undef fchmod
514 #undef fchmodat
515 #undef fstat
516 #undef fstatat
517 #undef futimens
518 #undef lstat
519 #undef mkdir
520 #undef mkdirat
521 #undef mkfifo
522 #undef mkfifoat
523 #undef mknod
524 #undef mknodat
525 #undef stat
526 #undef umask
527 #undef utimensat
528
529
530
531
532 #if YCL_DS
533 # include <nds.h>
534 # include <ystdex/base.h> // for ystdex::nonmovable;
535
536 namespace platform_ex
537 {
538
548 inline void
549 DMAFillWordsAsync(std::uint8_t chan, std::uint32_t val, void* p_dst,
550 std::uint32_t size)
551 {
552 DMA_FILL(chan) = std::uint32_t(val);
553 DMA_SRC(3) = std::uint32_t(&DMA_FILL(3));
554 DMA_DEST(3) = std::uint32_t(p_dst);
555
556 DMA_CR(3) = DMA_SRC_FIX | DMA_COPY_WORDS | size >> 2;
557 }
558
559
564 class YF_API FileSystem final
565 : private ystdex::noncopyable, private ystdex::nonmovable
566 {
567 public:
568 enum class RootKind
569 {
570 Null,
571 SD,
572 FAT
573 };
574
575 private:
576 RootKind root;
577 const char* init_dev;
578
579 public:
587 FileSystem(size_t = 16);
592 ~FileSystem();
593 };
594
595 } // namespace platform_ex;
596
597
598 #elif YCL_Win32
599
600 # ifndef UNICODE
601 # define UNICODE 1
602 # endif
603
604 # ifndef WINVER
605 # define WINVER 0x0501
607 # endif
608
609 # ifndef WIN32_LEAN_AND_MEAN
610 # define WIN32_LEAN_AND_MEAN 1
612 # endif
613
614 # ifndef NOMINMAX
615 # define NOMINMAX 1
617 # endif
618
619 # include <Windows.h>
620 # if __has_include(<specstrings_undef.h>)
621 # include <specstrings_undef.h>
622 # else
623 // NOTE: Workaround for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97362.
624 # undef __deref
625 # endif
626 # include <direct.h>
627 # include <io.h> // for ::_get_osfhandle;
628
630
631 # undef CopyFile
633 # undef CreateHardLink
635 # undef CreateSymbolicLink
637 # undef DialogBox
639 # undef DrawText
641 # undef ExpandEnvironmentStrings
643 # undef FindWindow
645 # undef FormatMessage
647 # undef GetMessage
649 # undef GetObject
651 # undef PostMessage
653 # undef SetEnvironmentVariable
655
656
657 extern "C"
658 {
659
660 # if defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)
661 YF_API struct ::tm* __cdecl __MINGW_NOTHROW
663 _gmtime32(const ::__time32_t*);
664 # endif
665
666 } // extern "C";
667
668 namespace platform_ex
669 {
670
675 inline PDefH(::HANDLE, ToHandle, int fd) ynothrow
676 ImplRet(::HANDLE(::_get_osfhandle(fd)))
677
678 } // namespace platform_ex;
679
680 #elif YCL_Android
681
685 extern "C"
686 {
687
692 # if !(__ANDROID_API__ >= 21)
693 int
694 linkat(int, const char*, int, const char*, int) yimpl(ynothrow);
695 # endif
696
697 # ifndef UTIME_NOW
698 # define UTIME_NOW ((1L << 30) - 1L)
699 # endif
700 # ifndef UTIME_OMIT
701 # define UTIME_OMIT ((1L << 30) - 2L)
702 # endif
703
705
706 # if !(__ANDROID_API__ >= 19)
707 int
708 futimens(int, const ::timespec times[2]) yimpl(ynothrow);
709 # endif
710
711 # if !(__ANDROID_API__ >= 12)
712 int
713 utimensat(int, const char*, const ::timespec[2], int) yimpl(ynothrow);
714 # endif
715
716
717 } // extern "C";
718 #endif
719
720 #endif
721
size
size_t size
Definition: memory_resource.cpp:295
platform::ssize_t
ystdex::make_signed_t< std::size_t > ssize_t
Definition: NativeAPI.h:94
platform_ex::Windows::PDefH
YB_ATTR_nodiscard YB_STATELESS yconstfn PDefH(bool, IsDirectory, FileAttributes attr) ynothrow ImplRet(attr &Directory) YB_ATTR_nodiscard YB_STATELESS inline PDefH(bool
判断 FileAttributes 是否指定目录。
base.h
基类实用设施。
platform::mode_t
::mode_t mode_t
文件模式类型。
Definition: FileIO.h:116
platform_ex
平台扩展命名空间。
Definition: Android.h:46
YSLib::IO::int
YF_API int
Definition: File.h:51
CHRLib::CharSet::Null
@ Null
空字符集。
Definition: Encoding.h:63
platform::FileMappingOption::ReadWrite
@ ReadWrite
platform::if
if(!p) p
platform_ex::YB_NONNULL
YB_ATTR_nodiscard YB_NONNULL(1) inline PDefH(u16string
构造适合表示路径的 char16_t 字符串。
YF_API
#define YF_API
Definition: Platform.h:56
platform_ex::Windows::unsigned
YF_API unsigned
Definition: NLS.h:52
NPL::tm
tm
Definition: NPLA.h:1941
ystdex::cpp2014::make_signed_t
typename make_signed< _type >::type make_signed_t
Definition: meta.hpp:165
ystdex::bases::nonmovable
不可转移对象：禁止继承此类的对象调用默认原型的转移构造函数和转移赋值操作符。
Definition: base.h:90
platform::ImplRet
YB_ATTR_nodiscard const char *s ImplRet(Nonnull(s)) YB_ATTR_nodiscard inline PDefH(const string &
platform_ex::Windows::Temporary
@ Temporary
Definition: Win32/include/YCLib/MinGW32.h:470
type_op.hpp
C++ 类型操作。
platform::KeyCategory::None
@ None
表示未分配或保留的键。
Definition: Keys.h:101
platform_ex::Windows::Directory
@ Directory
Definition: Win32/include/YCLib/MinGW32.h:466
platform
默认平台命名空间。
Definition: Container.h:56
YModules.h
YSLib 用户界面模块配置文件。
ynothrowv
#define ynothrowv
YSLib 无异常抛出保证验证：有条件地使用无异常抛出规范。
Definition: ydef.h:1219
platform::cstat
cstat(st, path, follow_link, yfsig)
platform::PDefH
YB_ATTR_nodiscard PDefH(string, MakePathString, u16string_view sv) ImplRet(MakePathString(sv.data())) using FileNodeID
文件节点标识类型。
platform::KeyCategory::Character
@ Character
表示产生字符的键。
Definition: Keys.h:111
DefBitmaskEnum
#define DefBitmaskEnum(_tEnum)
Definition: YBaseMacro.h:505
NPL::Access
YB_ATTR_nodiscard YB_PURE const TermNode &term ynothrow const TermNode &term ynothrow const TermNode &term ynothrow const TermNode &term ynothrow const TermNode &term ynothrow ImplRet(!term.Value) using YSLib YB_ATTR_nodiscard YB_PURE _type & Access(TermNode &term)
Definition: SContext.h:555
const
const Decapsulate const Decapsulate &y ynothrow ContextNode &ctx const
Definition: NPLA1Forms.cpp:1629
yimpl
#define yimpl(...)
实现标签。
Definition: ydef.h:413
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:1199
ystdex::bases::noncopyable
不可复制对象：禁止派生类调用默认原型的复制构造函数和复制赋值操作符。
Definition: base.h:50
platform_ex::ImplRet
YB_ATTR_nodiscard const char16_t *s ImplRet(platform::Nonnull(s)) YB_ATTR_nodiscard inline PDefH(const u16string &
yconstfn
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:1073