Skip to content

Instantly share code, notes, and snippets.

@zno5
zno5 / ScopeGuard.h
Created September 18, 2017 06:07
Call a function automatically on scope exit
/*
Usage:
class Test;
bool Finalize ();
bool Finalize (int i);
bool Finalize (int i, int j);
{
@zno5
zno5 / count_param_pack.cpp
Created August 23, 2017 07:24
count the number of arguments
template <typename ... Ts>
struct ParamPack;
template <>
struct ParamPack<> {
static constexpr size_t count = 0;
};
template <typename T, typename ... Ts>
struct ParamPack<T, Ts...> {
@zno5
zno5 / map_emplace_example.cpp
Created March 27, 2017 05:10
map, emplace w/o creating a object temporarily
struct Item {
int m_val1;
int m_val2;
Item (int val1, int val2) {
m_val1 = val1;
m_val2 = val2;
}
};
@zno5
zno5 / MySingleton.cpp
Created November 4, 2016 15:30
singleton class using std::call_once
#include <mutex>
class MySingleton {
public:
static MySingleton * GetInstance() {
std::call_once(
m_flag,
[] () { m_instance = new MySingleton(); }
);
@zno5
zno5 / Tokenize.cpp
Last active October 2, 2016 07:32
Tokenize a string.
std::vector<std::string> Tokenize (const std::string & str, char delim) {
if (str.empty() || !delim) {
return {};
}
std::vector<std::string> out;
std::string token;
std::stringstream ss(str);
while (std::getline(ss, token, delim)) {
@zno5
zno5 / utf8.cpp
Last active October 3, 2016 07:29
Some functions to work with UTF-8 string : you can check if a string is valid UTF-8, get the length of a UTF-8 string and replace things in a UTF-8 string.
//
// utf8.cpp
//
// Created by Jinho Bak on 9/30/16.
// Copyright (c) 2016 Jinho Bak. All rights reserved.
//
#include <vector>
#include <string>
@zno5
zno5 / ToUnderlyingType.cpp
Created September 2, 2016 01:18
Cast to underlying type. (if a variable is a enumeration type)
// for C++14
template <typename E>
constexpr auto ToUType(E arg) noexcept
{
return static_cast<std::underlying_type_t<E>>(arg);
}
@zno5
zno5 / ArraySize.cpp
Created August 26, 2016 09:55
Get size of array using template
// from Effective Modern C++
template <typename T, std::size_t N>
constexpr std::size_t arrSize(T (&) [N]) noexcept
{
return N;
}
@zno5
zno5 / AuthenticateAppleGameCenter.cpp
Last active August 21, 2016 11:01
Authenticate AppleGameCenter user.
/*
https://developer.apple.com/library/ios/documentation/GameKit/Reference/GKLocalPlayer_Ref/index.html#//apple_ref/occ/instm/GKLocalPlayer/generateIdentityVerificationSignatureWithCompletionHandler:
*/
////////////////////////////////////////////////////////////////////////////////
void GetVerifiedData(
std::array<uint8_t, SHA256_DIGEST_LENGTH> & verifiedData,
const std::string & playerId,
const std::string & bundleId,
int64_t timestamp,
@zno5
zno5 / GetStringByteSize.cpp
Last active September 2, 2016 01:07
Get bytes size of string
template <
typename T,
typename V = typename std::decay_t<T>::value_type,
typename = std::enable_if_t<
std::is_base_of<std::basic_string<V>, std::decay_t<T> >::value
>
>
auto GetStringByteSize(const T & str) noexcept
{
// ignore null char