Browse Source

Added defer() function to util

tags/v0.6.0
Andrew Belt 6 years ago
parent
commit
8c3bd70744
2 changed files with 44 additions and 10 deletions
  1. +32
    -0
      include/util/common.hpp
  2. +12
    -10
      src/bridge.cpp

+ 32
- 0
include/util/common.hpp View File

@@ -11,6 +11,9 @@
#include <condition_variable> #include <condition_variable>
#include <mutex> #include <mutex>


////////////////////
// Handy macros
////////////////////


/** Surrounds raw text with quotes /** Surrounds raw text with quotes
Example: Example:
@@ -51,6 +54,9 @@ Example:


namespace rack { namespace rack {


////////////////////
// Template hacks
////////////////////


/** C#-style property constructor /** C#-style property constructor
Example: Example:
@@ -68,6 +74,32 @@ T *construct(F f, V v, Args... args) {
return o; return o;
} }


/** Delays code until the scope is destructed
From http://www.gingerbill.org/article/defer-in-cpp.html

Example:
file = fopen(...);
defer({
fclose(file);
});
*/
template <typename F>
struct DeferWrapper {
F f;
DeferWrapper(F f) : f(f) {}
~DeferWrapper() { f(); }
};

template <typename F>
DeferWrapper<F> deferWrapper(F f) {
return DeferWrapper<F>(f);
}

#define DEFER_1(x, y) x##y
#define DEFER_2(x, y) DEFER_1(x, y)
#define DEFER_3(x) DEFER_2(x, __COUNTER__)
#define defer(code) auto DEFER_3(_defer_) = deferWrapper([&]() code)

//////////////////// ////////////////////
// Random number generator // Random number generator
// random.cpp // random.cpp


+ 12
- 10
src/bridge.cpp View File

@@ -234,6 +234,9 @@ static void serverRun() {
#ifdef ARCH_WIN #ifdef ARCH_WIN
WSADATA wsaData; WSADATA wsaData;
err = WSAStartup(MAKEWORD(2,2), &wsaData); err = WSAStartup(MAKEWORD(2,2), &wsaData);
defer({
WSACleanup();
});
if (err) { if (err) {
warn("Could not initialize Winsock"); warn("Could not initialize Winsock");
return; return;
@@ -250,6 +253,9 @@ static void serverRun() {
hints.ai_protocol = IPPROTO_TCP; hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE; hints.ai_flags = AI_PASSIVE;
err = getaddrinfo(NULL, "5000", &hints, &result); err = getaddrinfo(NULL, "5000", &hints, &result);
defer({
freeaddrinfo(result);
});
#else #else
struct sockaddr_in addr; struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr)); memset(&addr, 0, sizeof(addr));
@@ -273,6 +279,10 @@ static void serverRun() {
warn("Bridge server socket() failed"); warn("Bridge server socket() failed");
return; return;
} }
defer({
close(server);
});



// Bind socket to address // Bind socket to address
#ifdef ARCH_WIN #ifdef ARCH_WIN
@@ -282,14 +292,14 @@ static void serverRun() {
#endif #endif
if (err) { if (err) {
warn("Bridge server bind() failed"); warn("Bridge server bind() failed");
goto serverRun_cleanup;
return;
} }


// Listen for clients // Listen for clients
err = listen(server, 20); err = listen(server, 20);
if (err) { if (err) {
warn("Bridge server listen() failed"); warn("Bridge server listen() failed");
goto serverRun_cleanup;
return;
} }
info("Bridge server started"); info("Bridge server started");


@@ -319,15 +329,7 @@ static void serverRun() {
clientThread.detach(); clientThread.detach();
} }


// Cleanup
serverRun_cleanup:
err = close(server);
(void) err;
info("Bridge server closed"); info("Bridge server closed");
#ifdef ARCH_WIN
freeaddrinfo(result);
WSACleanup();
#endif
} }






Loading…
Cancel
Save