Skip to content

Instantly share code, notes, and snippets.

In these days of git everywhere, you may forget CMake and use git clean -d -f -x, that will remove all files not under source control.
@alepez
alepez / curl.cpp
Created November 28, 2014 15:10
curl download c++11
#include <cstdio>
#include <curl/curl.h>
#include <curl/easy.h>
#include <string>
#include <cassert>
#include <stdexcept>
#include <sstream>
#include <iostream>
void download(const std::string& url, const std::string& filename) {
@alepez
alepez / format_time.cpp
Created February 19, 2015 13:55
format any time template
inline std::string formatTime(const time_t t) {
std::tm timeinfo = *::gmtime(&t);
char buffer[16];
std::strftime(buffer, sizeof(buffer), "%Y%m%d-%H%M%S", &timeinfo);
return std::string { buffer };
}
template <typename T>
inline std::string formatTime(T&& t) {
using clock = typename std::remove_reference<T>::type::clock;
@alepez
alepez / sticky.js
Created February 21, 2015 14:35
sticky sidebar
function _makeSticky($sticky) {
var $parent = $sticky.parent();
var parentOffset = null;
var myOffset = null;
$(window).bind('scroll', function () {
var scroll = $(document.body).scrollTop();
if (myOffset === null) {
parentOffset = $parent.offset().top;
myOffset = $sticky.position().top;
}
@alepez
alepez / pickRandom.cpp
Last active August 29, 2015 14:16
pickRandom
template<typename T>
T pickRandom(std::vector<T>&& vec) {
return vec[::rand() % vec.size()];
}
template<typename T>
T pickRandom(std::initializer_list<T>&& list) {
return pickRandom(std::vector<T>(std::move(list)));
}
@alepez
alepez / universal-constructor.cpp
Created February 25, 2015 14:53
Universal constructor template, excluding copy/move constructor
class Bar {
template<typename T, typename = typename std::enable_if< !std::is_same<Foo, typename std::decay<T>::type >::value >>
inline Bar(T&& v) { }
};
@alepez
alepez / makeLazy.js
Created February 27, 2015 16:45
makeLazy
var makeLazy = function (fn, delay) {
var timeHandler;
return function () {
clearTimeout(timeHandler);
timeHandler = setTimeout(fn.bind.apply(fn, [null].concat(Array.prototype.slice.call(arguments))), delay);
};
};
@alepez
alepez / local-storage-value-handler.js
Created February 28, 2015 10:28
local storage value handler
var permanentValue = function (name) {
var that = {};
var prefix = 'debug-';
that.store = function (val) {
localStorage.setItem(prefix + name, val);
return val;
};
that.load = function () {
return localStorage.getItem(prefix + name);
};
@alepez
alepez / call-function-only-if-defined.js
Created March 19, 2015 16:43
call function only if exists
(this.props.onChange || Function)();
@alepez
alepez / automatic-test.js
Created March 21, 2015 16:22
automati javascript test
/*==============================================================================
= Automatic test runner. Add tests below =
==============================================================================*/
$(function () {
/*========== Test runner ==========*/
var Test = function () {
var that = {},
actions = [],
index = -1, elapsedTime = 0;