int main() { ResourceType x1; const ResourceType & x2(x1); { auto x = make_guard(x1); //Allowable } { auto x = make_guard(x2); //Allowable } { auto x = make_guard(foo()); //Allowable too. } }
void sql_database::load_queries(const std::unordered_map<std::string, std::string> & queries) { auto & c = get_free_connection(); auto guard = make_guard(std::bind(&std::mutex::unlock, &c.lock)); for(auto && it : queries) { auto res = PQprepare(c.conn, it.first.c_str(), it.second.c_str(), 0, nullptr); auto res_guard = make_guard(std::bind(PQclear, res)); auto status = PQresultStatus(res); if (status == PGRES_FATAL_ERROR) { throw sql_error { "sql query failed due to "_s + PQresultErrorMessage(res) + ", query: " + it.first }; } } }
void LogMessages(Settings settings) { LogSources sources(true); sources.AddDBWinReader(false); if (HasGlobalDBWinReaderRights()) sources.AddDBWinReader(true); sources.SetAutoNewLine(settings.autonewline); std::ofstream fs; if (!settings.filename.empty()) { OpenLogFile(fs, WStr(settings.filename)); fs.flush(); } auto guard = make_guard([&fs, &settings]() { if (!settings.filename.empty()) { fs.flush(); fs.close(); std::cout << "Log file closed.\n"; } }); std::string separator = settings.tabs ? "\t" : " "; while (!g_quit) { auto lines = sources.GetLines(); int linenumber = 0; for (auto it = lines.begin(); it != lines.end(); ++it) { if (settings.console) { if (settings.linenumber) { ++linenumber; std::cout << std::setw(5) << std::setfill('0') << linenumber << std::setfill(' ') << separator; } OutputDetails(settings, *it); std::cout << separator << it->message.c_str() << "\n"; } if (!settings.filename.empty()) { WriteLogFileMessage(fs, it->time, it->systemTime, it->pid, it->processName, it->message); } } if (settings.flush) { std::cout.flush(); fs.flush(); } Sleep(250); } std::cout.flush(); }
void sql_database::async_send_query(boost::asio::io_service::work &, sql_callback cb, const query_list & sql) { auto & c = get_free_connection(); auto guard = make_guard(std::bind(&std::mutex::unlock, &c.lock)); std::string error; auto qres = std::make_shared<query_result>(send_query(c, sql, error)); _main_service.post(std::bind(cb, std::move(qres), error)); }
query_result sql_database::query(const query_list & sql) { if (sql.empty()) return { }; auto & c = get_free_connection(); auto guard = make_guard(std::bind(&std::mutex::unlock, &c.lock)); std::string error; auto result = send_query(c, sql, error); if (!error.empty()) throw sql_error { error.c_str() }; return result; }
void sql_database::init(const std::string & host, uint16_t port, const std::string & user, const std::string & password, const std::string & name) { for (auto && c : _connections) { auto temp = PQsetdbLogin(host.c_str(), boost::lexical_cast<std::string>(port).c_str(), nullptr, nullptr, name.c_str(), user.c_str(), password.c_str()); auto guard = make_guard(std::bind(&PQfinish, temp)); if (PQstatus(temp) == CONNECTION_BAD) throw sql_error { "connection failed due to: "_s + PQerrorMessage(temp) }; guard.dismiss(); if (c.conn != nullptr) PQfinish(c.conn); c.conn = temp; } }
udp_socket::udp_socket( uint32_t addr, uint64_t port, bool broadcast ) { // Create the socket _fd = ::socket( PF_INET, SOCK_DGRAM, IPPROTO_UDP ); if ( _fd < 0 ) error( errno, "Error creating server socket" ); // Close if we exit prematurely auto guard = make_guard( [&]() { ::close( _fd ); _fd = -1; } ); // Set the socket to be reusable immediately after closing if ( port > 0 ) { int opt = 1; if ( setsockopt( _fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt) ) != 0 ) error( errno, "Error reusing address" ); } if ( broadcast ) { int opt = 1; // Turn on broadcasting if ( setsockopt( _fd, SOL_SOCKET, SO_BROADCAST, &opt, sizeof(opt) ) != 0 ) error( errno, "Could not set broadcast" ); } // Bind the socket if( port != 0 ) { struct sockaddr_in servaddr; size_t servsize = sizeof(servaddr); memset( (void *)(&servaddr), 0, servsize ); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = addr; servaddr.sin_port = htons( port ); if ( ::bind( _fd, (struct sockaddr *)&servaddr, servsize ) != 0 ) error( errno, format( "Error binding socket ({0})", ip_lookup( addr ) ) ); } guard.commit(); }
int listener::listen() { if (listening()) { return e_busy; } ll_sys_failed_return(_fd = ::socket(AF_INET, SOCK_STREAM, 0)); auto guard = make_guard([this](){ _fd.close(); }); int n = 1; ll_sys_failed_return(::setsockopt(_fd, SOL_SOCKET, SO_REUSEADDR, &n, sizeof(int))); ll_sys_failed_return(::bind(_fd, _addr, _addr.length())); ll_sys_failed_return(::listen(_fd, _backlog)); ll_failed_return(_reactor->open(_fd, reactor::poll_in | reactor::poll_err, &listener::accept_handler, this)); guard.dismiss(); if (ll_failed(do_emit(_fd, reactor::poll_open, _addr))) { close(); return fail; } return ok; }