Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have source code that 'times out' (becomes invalid after a certain moment)?

Tags:

c++

c

temporary

We are currently busy migrating from Visual Studio 2005 to Visual Studio 2010 (using unmanaged C/C++). This means that about half of our developers are already using Visual Studio 2010, while the other half is still using Visual Studio 2005. Recently, I came into a situation where a certain construction can be written in a clean way in Visual Studio 2010, but requires less-clean source code in Visual Studio 2005. Because not all developers have already Visual Studio 2010 on their machine, I have to write the code like this:

#if _MSC_VER >= 1600
   // clean version of the source code
#else
   // less clean version
   // of the source code
   // requiring multiple lines of code
   // and requiring some dirty static_casts
#endif

Since all developers will migrate to Visual Studio 2010 by the end of this year, I want this code to 'disappear' automatically after a certain moment. Keeping the 'less clean version' in the source code results in unreadable source code in the long-term.

Of course, I know that code doesn't automatically disappear, so I actually want an automatic alarm bell after a certain moment. Something like this:

#if _MSC_VER >= 1600
   // clean version of the source code
#else
   // less clean version
   // of the source code
   // requiring multiple lines of code
   // and requiring some dirty static_casts
#endif
#if compilation_date is after 1 november 2010
#   error "Remove Visual Studio 2005 compatibility code from this file"
#endif

That way, if we forget about this, we are automatically notified of this after 1 november 2010.

This trick probably requires the use of DATE, but since this needs to be handled by the precompiler, you can't perform string-manipulations or use the C date/time functions.

I also considered the alternative idea of just sending myself a delayed mail, but I was wondering if there wasn't a solution that could be built in in the source code.

like image 250
Patrick Avatar asked Sep 01 '10 07:09

Patrick


1 Answers

In case of GNU make I'd do it like this:

CFLAGS += -DCURDATE=$(shell date +%Y%m%d)

It will add a macro CURDATE to compiler flags, that contains the current time in YYYYMMDD format.

So in source you could do something like this:

#if CURDATE > 20101101
#error "Do whatever you have to do"
#endif

Can you do something like this in VS?

like image 174
qrdl Avatar answered Oct 10 '22 07:10

qrdl