Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
TODO(): A macro that will fire a static assert if compiled on or after a specified date. Requires C++11.
/*
todo.h
TODO():
A macro that will fire a static assert if compiled on or after
a specified date.
Requires C++11 constexpr support.
Inspired by https://twitter.com/meetingcpp/status/930896368520060928
Example use:
// Causes an an error to be printed, containing the text:
// TODO 2017-11-16: Solve all the problems
TODO("2017-11-16", "Solve all the problems");
// no output until 2019-11-07, at which point will print:
// TODO Nov 7 2019: Solve all the other problems
TODO("Nov 7 2019", "Solve all the other problems");
See end of file for license information.
*/
#pragma once
namespace todo_impl {
constexpr int value(const char* y, const char* m, const char* d){
return
(y[0]*1000 + y[1]*100 + y[2]*10 + y[3]) * 512 +
(m[0]* 10 + m[1]) * 32 +
((d[0]|16)*10 + d[1]); // |16 will convert ' ' to '0'
}
constexpr const char* month(char a, char b, char c) {
return
a == 'j' && b == 'a' && c == 'n' ? "01" :
a == 'f' && b == 'e' && c == 'b' ? "02" :
a == 'm' && b == 'a' && c == 'r' ? "03" :
a == 'a' && b == 'p' && c == 'r' ? "04" :
a == 'm' && b == 'a' && c == 'y' ? "05" :
a == 'j' && b == 'u' && c == 'n' ? "06" :
a == 'j' && b == 'u' && c == 'l' ? "07" :
a == 'a' && b == 'u' && c == 'g' ? "08" :
a == 's' && b == 'e' && c == 'p' ? "09" :
a == 'o' && b == 'c' && c == 't' ? "10" :
a == 'n' && b == 'o' && c == 'v' ? "11" :
a == 'd' && b == 'e' && c == 'c' ? "12" :
throw "invalid month name";
}
constexpr int value(const char* d) {
return
// ISO8601: 2017-12-09
d[4] == '-' && d[7] == '-' && d[10] == '\0'
?
value(d, d+5, d+8):
// Middle-endian: Dec 9 2017
d[3] == ' ' && d[6] == ' ' && d[11] == '\0'
?
value(d+7, month(d[0]|32, d[1]|32, d[2]|32), d+4):
throw "invalid date format";
}
}
#define TODO(DATE, MESSAGE) \
static_assert(::todo_impl::value(__DATE__) < ::todo_impl::value(DATE), \
"TODO " DATE ": " MESSAGE)
TODO("9899-12-31", "Add support for a 5th year digit to prevent y10k bugs.");
/*
Copyright (c) 2017 Jonathan Adamczewski
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.