Skip to content

Instantly share code, notes, and snippets.

View slyshykO's full-sized avatar
🇺🇦
UA

Oleksiy Slyshyk slyshykO

🇺🇦
UA
  • Ukraine, Kyiv
View GitHub Profile
@slyshykO
slyshykO / format_time.cpp
Created August 28, 2024 17:15
Format current time in this form: "hh:mm:ss.SSS" (where "SSS" are milliseconds) (C++)
//-----------------------------------------------------------------------------
// Format current time (calculated as an offset in current day) in this form:
//
// "hh:mm:ss.SSS" (where "SSS" are milliseconds)
//-----------------------------------------------------------------------------
std::string now_str()
{
// Get current time from the clock, using microseconds resolution
const boost::posix_time::ptime now =
boost::posix_time::microsec_clock::local_time();
@slyshykO
slyshykO / async_process.py
Created August 28, 2024 16:32
async stdout & stderr for process in python
#!/usr/bin/python3
# encoding: utf-8
import platform
import asyncio
@asyncio.coroutine
def _read_stream(stream, cb):
while True:
line = yield from stream.readline()
@slyshykO
slyshykO / enumerate.hpp
Created August 28, 2024 16:30
c++ enumerate
// http://reedbeta.com/blog/python-like-enumerate-in-cpp17/?fbclid=IwAR3b_OJ61ol7pU525NxcIzureSBU1emoSQtvVG3L_q_pCvBcO4yKyyHU6OA
#include <iostream>
#include <tuple>
#include <vector>
template <typename T,
typename TIter = decltype(std::begin(std::declval<T>())),
typename = decltype(std::end(std::declval<T>()))>
@slyshykO
slyshykO / unused.cpp
Created August 28, 2024 16:29
UNUSED++
#ifndef __cplusplus
#define UNUSED(x) ((void)(x))
#else
template <typename... Ts>
inline constexpr void ignore_unused(Ts const& ...){}
template <typename... Ts>
inline constexpr void ignore_unused(){}
#define UNUSED(x) ignore_unused(x)
@slyshykO
slyshykO / raii_file.cpp
Last active August 28, 2024 16:23
RAII for file
#include <memory>
int main()
{
std::unique_ptr<std::FILE, decltype(&std::fclose)> f(std::fopen(name.c_str(), "r"), &std::fclose);
return 0;
}
@slyshykO
slyshykO / raii_malloc.cpp
Created April 30, 2018 13:14
RAII malloc
#include <memory>
int main()
{
auto Data =
std::unique_ptr<double, decltype(free)*>{
reinterpret_cast<double*>(malloc(sizeof(double)*50)),
free };
return 0;
}
@slyshykO
slyshykO / snprintf.cpp
Last active March 14, 2018 17:44
snprintf with zero ending
template <uint32_t N, typename... Args>
void _snprintf(char (&buf)[N], const char* format, Args&&... args)
{
snprintf(buf, N, format, args...);
buf[N-1] = '\0';
};
@slyshykO
slyshykO / FastFunc.hpp
Created February 20, 2018 12:44 — forked from vittorioromeo/FastFunc.hpp
Don Clugston's fast delegate C++11 implementation
#ifndef SSVU_FASTFUNC
#define SSVU_FASTFUNC
#include <cstring>
#include <type_traits>
#include <cassert>
#include <cstddef>
#include <memory>
#include <new>
#include <utility>