YSTest: NativeAPI.h 源文件

YSTest PreAlpha_b800_20170810 The YSLib Test Project

- 首页
- 相关页面
- 模块
- 命名空间
- 类
- 文件

- 文件列表
- 文件成员

NativeAPI.h
浏览该文件的文档.
1 /*
2 © 2011-2017 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
199
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 // FIXME: 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 # include <direct.h>
621 # include <io.h> // for ::_get_osfhandle;
622
624
625 # undef CopyFile
627 # undef CreateHardLink
629 # undef CreateSymbolicLink
631 # undef DialogBox
633 # undef DrawText
635 # undef ExpandEnvironmentStrings
637 # undef FindWindow
639 # undef FormatMessage
641 # undef GetMessage
643 # undef GetObject
645 # undef PostMessage
647 # undef SetEnvironmentVariable
649
650
651 extern "C"
652 {
653
654 # if defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)
655 YF_API struct ::tm* __cdecl __MINGW_NOTHROW
657 _gmtime32(const ::__time32_t*);
658 # endif
659
660 } // extern "C";
661
662 namespace platform_ex
663 {
664
669 inline PDefH(::HANDLE, ToHandle, int fd) ynothrow
670 ImplRet(::HANDLE(::_get_osfhandle(fd)))
671
672 } // namespace platform_ex;
673
674 #elif YCL_Android
675
679 extern "C"
680 {
681
686 int
687 linkat(int, const char*, int, const char*, int) ynothrow;
688
689 # ifndef UTIME_NOW
690 # define UTIME_NOW ((1L << 30) - 1L)
691 # endif
692 # ifndef UTIME_OMIT
693 # define UTIME_OMIT ((1L << 30) - 2L)
694 # endif
695
697
698 int
699 futimens(int, const ::timespec times[2]) ynothrow;
700
701 int
702 utimensat(int, const char*, const ::timespec[2], int) ynothrow;
704
705 } // extern "C";
706 #endif
707
708 #endif
709
yconstfn
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:680
DefBitmaskEnum
#define DefBitmaskEnum(_tEnum)
Definition: YBaseMacro.h:481
platform
默认平台命名空间。
Definition: Container.h:47
Text
platform::PDefH
const string const char *sig PDefH(string, ComposeMessageWithSignature, const char *msg, const char *sig) ImplRet(string(Nonnull(msg))+" @ "+Nonnull(sig)) template< class _type > inline YB_NONNULL(2) string ComposeMessageWithSignature(const _type &msg
platform::KeyCategory::None
表示未分配或保留的键。
Definition: Keys.h:101
platform::KeyCategory::Character
表示产生字符的键。
Definition: Keys.h:111
CHRLib::CharSet::Null
空字符集。
Definition: Encoding.h:63
platform::ImplRet
const string const char *sig ImplRet(msg+" @ "+Nonnull(sig)) inline YB_NONNULL(1
platform_ex::Windows::unsigned
YF_API unsigned
Definition: NLS.h:52
platform_ex::ImplRet
const char16_t *s ImplRet(platform::Nonnull(s)) inline PDefH(const u16string &
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:766
platform::if
if(!p) p
YF_API
#define YF_API
Definition: Platform.h:56
platform::FileMappingOption::ReadWrite
type_op.hpp
C++ 类型操作。
YModules.h
base.h
基类实用设施。
ynothrowv
#define ynothrowv
YSLib 无异常抛出保证验证：有条件地使用无异常抛出规范。
Definition: ydef.h:786
ystdex::nonmovable
Definition: base.h:81
platform_ex::YB_NONNULL
YB_NONNULL(1) PDefH(u16string
构造适合表示路径的 char16_t 字符串。
platform::cstat
cstat(st, path, follow_link, yfsig)
platform_ex
平台扩展命名空间。
Definition: Android.h:45
ystdex::noncopyable
不可复制对象：禁止派生类调用默认原型的复制构造函数和复制赋值操作符。
Definition: base.h:41
ystdex::size
auto size(const _tRange &c) -> decltype(c.size())
类容器访问。
Definition: range.hpp:233
platform_ex::Windows::Temporary
Definition: MinGW32.h:456
YSLib::IO::int
YF_API int
Definition: File.h:51
platform::ssize_t
ystdex::make_signed_t< std::size_t > ssize_t
Definition: NativeAPI.h:94
platform::mode_t
::mode_t mode_t
文件模式类型。
Definition: FileIO.h:114
platform_ex::Windows::Directory
Definition: MinGW32.h:452