Skip to content

Commit

Permalink
add windows in c-cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
skyshaw committed Aug 4, 2013
1 parent b657b1c commit 4319301
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 0 deletions.
8 changes: 8 additions & 0 deletions c-cpp/misc/c++11/static_assert.cpp
@@ -0,0 +1,8 @@
int a[100][100];
int (*getArray())[100] {
return a;
}
int main() {
int (*pArray)[100] = getArray();
return 0;
}
42 changes: 42 additions & 0 deletions c-cpp/windows/alertable_io.cpp
@@ -0,0 +1,42 @@
#include <Windows.h>
#include <cassert>
#include <cstdio>

template <typename F>
struct ScopeExit {
ScopeExit(F f) : f(f) {}
~ScopeExit() { f(); }
F f;
};

template <typename F>
ScopeExit<F> MakeScopeExit(F f) {
return ScopeExit<F>(f);
};
#define STRING_JOIN2(arg1, arg2) DO_STRING_JOIN2(arg1, arg2)
#define DO_STRING_JOIN2(arg1, arg2) arg1 ## arg2
#define SCOPE_EXIT(code) \
auto STRING_JOIN2(scope_exit_, __LINE__) = MakeScopeExit([&](){code;})

struct OverlappedBuffer {
OVERLAPPED o;
char b[64];
};

int main() {

auto fn = L"C:\\Users\\skyshaw\\Desktop\\data.txt";
auto f = CreateFile(fn, GENERIC_READ, FILE_SHARE_READ, nullptr,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
assert(fn);
SCOPE_EXIT(CloseHandle(f));

OverlappedBuffer ob = {};
ReadFileEx(f, ob.b, sizeof(ob.b), &ob.o, [](DWORD e, DWORD c, OVERLAPPED* o) {
assert(e == ERROR_SUCCESS);
auto ob = reinterpret_cast<OverlappedBuffer*>(o);
std::printf("> %.*s\n", c, ob->b);
});
SleepEx(INFINITE, true);
return 0;
}
38 changes: 38 additions & 0 deletions c-cpp/windows/thread.cpp
@@ -0,0 +1,38 @@
#include <Windows.h>
#include <cassert>
#include <cstdio>

template <typename F>
struct ScopeExit {
ScopeExit(F f) : f(f) {}
~ScopeExit() { f(); }
F f;
};

template <typename F>
ScopeExit<F> MakeScopeExit(F f) {
return ScopeExit<F>(f);
};
#define STRING_JOIN2(arg1, arg2) DO_STRING_JOIN2(arg1, arg2)
#define DO_STRING_JOIN2(arg1, arg2) arg1 ## arg2
#define SCOPE_EXIT(code) \
auto STRING_JOIN2(scope_exit_, __LINE__) = MakeScopeExit([&](){code;})


int main() {
auto t = CreateThread(nullptr, 0, [](void*) -> DWORD {
auto fn = L"C:\\Users\\skyshaw\\Desktop\\data.txt";
auto f = CreateFile(fn, GENERIC_READ, FILE_SHARE_READ, nullptr,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
assert(fn);
SCOPE_EXIT(CloseHandle(f));
char b[64];
DWORD c;
ReadFile(f, b, sizeof(b), &c, nullptr);
std::printf("> %.*s\n", c, b);
return 0;
}, nullptr, 0, nullptr);
assert(t);
WaitForSingleObject(t, INFINITE);
return 0;
}
34 changes: 34 additions & 0 deletions c-cpp/windows/windows_io.cpp
@@ -0,0 +1,34 @@
#include <Windows.h>
#include <cassert>
#include <cstdio>

template <typename F>
struct ScopeExit {
ScopeExit(F f) : f(f) {}
~ScopeExit() { f(); }
F f;
};

template <typename F>
ScopeExit<F> MakeScopeExit(F f) {
return ScopeExit<F>(f);
};
#define STRING_JOIN2(arg1, arg2) DO_STRING_JOIN2(arg1, arg2)
#define DO_STRING_JOIN2(arg1, arg2) arg1 ## arg2
#define SCOPE_EXIT(code) \
auto STRING_JOIN2(scope_exit_, __LINE__) = MakeScopeExit([&](){code;})


int main() {
auto fn = L"C:\\Users\\skyshaw\\Desktop\\data.txt";
auto f = CreateFile(fn, GENERIC_READ, FILE_SHARE_READ, nullptr,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
assert(fn);
SCOPE_EXIT(CloseHandle(f));

char b[64];
DWORD c;
ReadFile(f, b, sizeof(b), &c, nullptr);
std::printf("> %.*s\n", c, b);
return 0;
}

0 comments on commit 4319301

Please sign in to comment.