Skip to content

Instantly share code, notes, and snippets.

std::vector<std::string>
sorted(std::vector<std::string> names)
{
std::sort(names);
return names;
}
// names is an lvalue; a copy is required so we don't modify names
std::vector<std::string> sorted_names1 = sorted( names );
@foobit
foobit / get_widget.cpp
Created April 1, 2014 19:32
from: Herb Sutter's - Going Native 2013
std::shared_ptr<widget> get_widget(int id)
{
static std::map<int, std::weak_ptr<widget>> cache;
static std::mutex m;
std::lock_guard<std::mutex> hold(m);
auto& wp = cache[id];
auto sp = wp.lock();
if (!sp)
{
class Stopwatch {
using clock = std::chrono::high_resolution_clock;
bool is_running() const { return stop_time_ == clock::time_point::min(); }
clock::time_point end_time() const { return is_running() ? clock::now() : stop_time_; }
clock::time_point begin_time_{clock::now()}, stop_time_{clock::time_point::min()};
public:
void stop() { if (is_running()) stop_time_ = clock::now(); }
clock::duration elapsed() const { return end_time() - begin_time_; }
};
@foobit
foobit / IsNumeric.cpp
Created May 21, 2014 03:05
C++11 fancy iteration
bool isNumeric(const std::string& input)
{
return std::all_of(input.begin(), input.end(), ::isdigit);
}
@foobit
foobit / image to raw
Created April 30, 2012 15:38
UIImage/NSImage to raw buffer
NSString* file = [[NSString alloc] initWithUTF8String:pth.c_str()];
CGImageRef image = [UIImage imageWithContentsOfFile:file].CGImage;
[file release];
m_width = CGImageGetWidth(image);
m_height = CGImageGetHeight(image);
m_poww = std::max<int>(math::high_pow2(m_width), 16);
m_powh = std::max<int>(math::high_pow2(m_height), 16);
int pitch = m_poww * 4;
@foobit
foobit / gist:3783859
Created September 25, 2012 19:17
iOS resign and validate
#!/bin/bash
# apply
codesign -f -s "iPhone Developer" -v MyApp.app
# check it
codesign -d --entitlements - MyApp.app
@foobit
foobit / gist:3840555
Created October 5, 2012 15:34
C++11 mutex, wait_event
#include <future>
class wait_event
{
std::condition_variable cond;
std::mutex m;
public:
void signal()
{
std::lock_guard<std::mutex> lock(m);
@foobit
foobit / gist:4519483
Created January 12, 2013 17:38
multi_function - multicast observer. Based on Generalizing Observer By Herb Sutter http://www.drdobbs.com/cpp/generalizing-observer/184403873
// Dr. Dobb's
// Generalizing Observer By Herb Sutter
// http://www.drdobbs.com/cpp/generalizing-observer/184403873
//
#pragma once
#include <list>
#include <functional>
@foobit
foobit / gist:4618064
Last active December 11, 2015 14:59
64bit (OSX and *nix) vsnprintf trashes va_list args. Must va_start between requesting length and performing expansion
std::string string_format(const char* s, ...)
{
va_list args;
va_start(args, s);
std::string result;
int len = vsnprintf(nullptr, 0, s, args);
if (len > 0)
{
@foobit
foobit / gist:5065376
Created March 1, 2013 15:26
set "git push" to only push current branch
git config --global push.default tracking