Skip to content

Instantly share code, notes, and snippets.

@alepez
alepez / lambda_on_destructor.cpp
Created October 4, 2018 14:35
lambda_on_destructor.cpp
#include <iostream>
#include <memory>
struct Foo {
Foo(std::function<void()>&& fun) : f{std::move(fun)} { }
~Foo() { f(); }
std::function<void()> f;
};
int main() {
@alepez
alepez / gitlab-docker-migrate.md
Created July 25, 2018 15:33
migrare gitlab docker

Nell'host di destinazione, avvio un nuovo container GitLab:

docker run --detach --hostname git.decktutor.net --publish 443:443 --publish 80:80 --publish 22:22 --name gitlab  --restart always  --volume /srv/gitlab/config:/etc/gitlab  --volume /srv/gitlab/logs:/var/log/gitlab  --volume /srv/gitlab/data:/var/opt/gitlab  gitlab/gitlab-ce:10.8.4-ce.0

Copio gitlab.rb dalla sorgente alla destinazione /srv/gitlab/config/gitlab.rb

Nell'host di destinazione, riconfiguro gitlab con il nuovo gitlab.rb:

docker exec -ti gitlab gitlab-ctl reconfigure
@alepez
alepez / weak-ptr-global.cpp
Created June 5, 2018 15:31
weak-ptr-global (global is bad practice, but at least this is automatically released)
#include <cassert>
#include <iostream>
#include <memory>
#include <mutex>
#include <string>
#include <unordered_map>
template <typename T>
std::shared_ptr<T> get_global(int id) {
using Map = std::unordered_map<int, std::weak_ptr<T>>;
@alepez
alepez / qt-eigen-opengl-demo.cmake
Created April 13, 2018 16:29
cmakelist.txt qt eigen opengl demo
add_custom_target(demos)
find_package(Qt5Core REQUIRED)
find_package(Qt5Gui REQUIRED)
find_package(Qt5OpenGL REQUIRED)
find_package(OpenGL)
find_package(Eigen3)
set(CMAKE_AUTOMOC ON)
@alepez
alepez / fix_modular_sequence.cpp
Created March 29, 2018 12:48
Fix a sequence in a modular range, keeping direction
template <typename T>
T fix_modular_sequence(T&& seq, const typename T::value_type modulus) {
const auto threshold = modulus / 2.0;
auto it = seq.begin();
auto prev = *it;
while (it != seq.end()) {
++it;
auto& curr = *it;
auto diff = curr - prev;
if (std::abs(diff) > threshold) curr -= (diff < 0 ? -1 : 1) * modulus;
@alepez
alepez / core-dump.cpp
Created February 13, 2018 09:45
Enable Core dump in C/C++
#include <iostream>
#include <sys/resource.h>
int main() {
rlimit core_limits;
core_limits.rlim_cur = core_limits.rlim_max = RLIM_INFINITY;
setrlimit(RLIMIT_CORE, &core_limits);
std::cout << "begin\n";
int* x = 0;
@alepez
alepez / gol.md
Created February 10, 2018 07:35
gol.md
  • Qualsiasi cella viva con meno di due celle vive adiacenti muore, come per effetto d'isolamento;
  • Qualsiasi cella viva con due o tre celle vive adiacenti sopravvive alla generazione successiva;
  • Qualsiasi cella viva con più di tre celle vive adiacenti muore, come per effetto di sovrappopolazione;
  • Qualsiasi cella morta con esattamente tre celle vive adiacenti diventa una cella viva, come per effetto di riproduzione.
@alepez
alepez / list_screen_output_modes.sh
Created February 1, 2018 08:52
Shell script to programmatically list xrandr output modes
#!/bin/sh
output=""
xrandr | while read line; do
if echo "${line}" | grep 'connected' > /dev/null; then
if echo "${line}" | grep ' connected' > /dev/null; then
output="$( echo "${line}" | sed 's/\(.*\) connected.*/\1/' )"
fi
elif ! echo "${line}" | grep '^Screen' > /dev/null; then
@alepez
alepez / brother-top-margin-cut-off-FIX.md
Created January 26, 2018 22:17
Brother MFC-J480DW cuts off top margin

Brother MFC-J480DW cuts off top margin

In Linux, my Brother MFC-J480DW cuts off top margin when used with A4 paper, even if all settings are set to A4.

To make it work, I had to edit /opt/brother/Printers/mfcj480dw/inf/brmfcj480dwrc file (that's the path in Gentoo, it could be different in other distributions).

And change:

PaperType=Letter
@alepez
alepez / update-adblock-list
Created January 12, 2018 13:37
adblock: Generates dnsmasq rules for NetworkManager
#!/bin/sh
FAKE_SERVER_ADDRESS='127.8.8.8'
URL='http://pgl.yoyo.org/adservers/serverlist.php?hostformat=dnsmasq&showintro=0&mimetype=plaintext'
FILENAME='/etc/NetworkManager/dnsmasq.d/adblock'
## do not update if fresh (24h)
if test -e "${FILENAME}"; then
echo 'file exists'
if ! test "$( find "${FILENAME}" -mmin +1440 )"; then