Sign in to follow this  
thedustbustr

c++ automatic runtime generation of constant

Recommended Posts

I want to set SVN::Tag without having to call MakeTag myself in my program. I just want to include svn.h and have SVN::Tag already populated. Additionally, I want to be able to 'touch' a single file (svn.cpp) to force the pre-build step to fill in the true constants. Obviously this isn't valid C++ (static order initialization and its not syntactically correct either) but it demonstrates my intent. Am I stuck making this a singleton class? I don't like it because then my use case isn't as simple as reading a constant.
#pragma once
#include <string>

namespace SVN {

	const static std::string Revision = "$WCREV$";
	const static std::string Modified = "$WCMODS?Modified:$";
	const static std::string Date     = "$WCDATE$";
	const static std::string Range    = "$WCRANGE$";
	const static std::string Mixed    = "$WCMIXED?Mixed revision WC:$";
	const static std::string URL      = "$WCURL$";
	const static std::string Tag = MakeTag();

}

void MakeTag()
{
	size_t tag_pos = 1 + SVN::URL.find_last_of('/');
	SVN::Tag = SVN::URL.substr(tag_pos);
}



Share this post


Link to post
Share on other sites
What would be the problem with the obvious solution?

#pragma once
#include <string>

namespace SVN {

std::string Revision() { return "$WCREV$"; }
std::string Modified() { return "$WCMODS?Modified:$"; }
std::string Date() { return "$WCDATE$"; }
std::string Range() { return "$WCRANGE$"; }
std::string Mixed() { return "$WCMIXED?Mixed revision WC:$"; }
std::string URL() { return "$WCURL$"; }
std::string Tag(){
size_t tag_pos = 1 + SVN::URL.find_last_of('/');
return SVN::URL.substr(tag_pos);
}

}




If you really really want a compile-time constant, you might be able to get boost.spirit to fill it in for you. But it seems like overkill when the above should work.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this