Skip to content

Instantly share code, notes, and snippets.

@fcostin
Created July 24, 2013 00:15
Show Gist options
  • Save fcostin/6067195 to your computer and use it in GitHub Desktop.
Save fcostin/6067195 to your computer and use it in GitHub Desktop.
// goal: typeDefinitions = {"FIDO" : {864 : ["SUPER_864"]}, "SPOT" : {432 : ["LUCKY_SPOT_432"], 576 : ["MERRY_SPOT_576"]}}
// works with g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
//
// g++ -Wall -Werror -pedantic -Wconversion --std=c++0x % && ./a.out
#include <vector>
#include <map>
#include <tuple>
#include <utility>
#include <string>
#include <iostream>
#include <assert.h>
#include <cxxabi.h>
using namespace std;
// shorthand pair constructor as i am lazy
template<typename K, typename V>
pair<K, V> P(const K& key, const V& value) {
return make_pair(key, value);
}
// variadic templates
// shorthand vector constructor (base case - empty)
template<typename Head, typename... Tail>
vector<Head> V(const Tail&... tail) {
vector<Head> e;
return e;
}
// shorthand vector constructor (non-empty case)
template<typename Head, typename... Tail>
vector<Head> V(const Head& head, const Tail&... tail) {
vector<Head> a;
a.push_back(head);
auto b = V<Head, Tail...>(tail...);
a.insert(a.end(), b.begin(), b.end());
return a;
}
template<typename K, typename V, typename... Tail>
map<K, V> M(const Tail&... tail) {
map<K, V> e;
return e;
}
// shorthand map constructor (non-empty empty)
template<typename K, typename V, typename... Tail>
map<K, V> M(const pair<K, V>& head, const Tail&... tail) {
map<K, V> a;
a.insert(head);
map<K, V> b = M<K, V>(tail...);
for (auto i = b.begin(); i != b.end(); ++i) {
a.insert(*i);
}
return a;
}
template <typename T>
string identify(const T & t) {
int status;
char *buff = abi::__cxa_demangle(typeid(t).name(), 0, 0, &status);
assert(status == 0);
string name(buff);
free(buff);
return name;
}
void demo() {
vector<pair<pair<int, string>, double> > w0 = V(P(P(1, string("hello")), 12.34), P(P(2, string("world!")), 56.78));
auto w1 = V(P(P(1, string("hello")), 12.34), P(P(2, string("world!")), 56.78));
auto w2 = P(1, string("foo"));
auto w3 = M<int, double>();
auto w4 = M(P(1, string("foo")));
auto w5 = M(P(1, 1), P(2, 2));
assert (w5.size() == 2);
auto w6 = M(
P(
string("FIDO"),
M(
P(
864,
V(string("SUPER_864"))
)
)
),
P(
string("SPOT"),
M(
P(
432,
V(string("LUCKY_SPOT_432"))
),
P(
576,
V(string("MERRY_SPOT_576"))
)
)
)
);
cout << "w6 has type " << identify(w6) << endl;
assert (w6.size() == 2);
assert (w6.begin()->second.size() == 1);
assert (w6.begin()->second.begin()->second.size() == 1);
assert (w6.begin()->second.begin()->second[0][0] == 'S');
assert ((++w6.begin())->second.size() == 2);
assert ((++w6.begin())->second.begin()->second.size() == 1);
assert ((++w6.begin())->second.begin()->second[0][0] == 'L');
assert ((++(++w6.begin())->second.begin())->second.size() == 1);
assert ((++(++w6.begin())->second.begin())->second[0][0] == 'M');
}
int main() {
demo();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment