Skip to content

Instantly share code, notes, and snippets.

@g-pechorin
Created September 5, 2014 14:28
Show Gist options
  • Save g-pechorin/c8a55dca38ddd4dc7fa4 to your computer and use it in GitHub Desktop.
Save g-pechorin/c8a55dca38ddd4dc7fa4 to your computer and use it in GitHub Desktop.
WIP crc32 in TMP
/**
* We need string length - not "hard"
*/
template<const char text[]>
struct tmp_strlen
{
template<const char v, const int index>
struct tmp_strlen_
{
enum _strlen
{
value = tmp_strlen_<text[index + 1], index + 1>::value
};
};
template<const int index>
struct tmp_strlen_<'\0', index>
{
enum _strlen
{
value = index
};
};
enum _strlen
{
value = tmp_strlen_<text[0], 0>::value
};
};
/**
* Okay - now for the crc32 ... which I don't actually remember off the top of my head
*/
template<const char text[]>
struct tmp_crc
{
enum _crc : unsigned int
{
value = tmp_strlen<text>::value
};
};
/**
* Macro - we need to declare these suckers as global constants
*/
#define HASH_ID(name, string) \
constexpr const char _HASH_ID##name[] = string; \
constexpr const unsigned int name = tmp_crc< _HASH_ID##name >::value
/**
* Test program below ...
*/
#include <iostream>
#include <string>
#include <vector>
#include <cstdint>
HASH_ID(foo, "foobar");
constexpr const char g_unique_name[] = "hello";
template<const char text[]>
struct hash_sum
{
enum _values
{
length = tmp_strlen<g_unique_name>::value,
crc = sizeof(text),
};
};
int main()
{
std::cout << "heldhgorld!\n";
std::cout << "foo<\"hello\">::crc = " << hash_sum<g_unique_name>::crc << std::endl;
std::cout << "foo<\"hello\">::length = " << hash_sum<g_unique_name>::length << std::endl;
std::cout << "foo = " << foo << std::endl;
return(EXIT_SUCCESS);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment