Currently I've written up some C++11 code that uses variadic templates to allow for a variable number of arguments with type information. This code works fine as is, but now I've realized that C++0x/11 won't work as I need to merge this into a larger code base & getting it to compile with "-std=c++0x" is easier said than done. So, I'm wondering whats the closest I can come to the following C++11 code without using C++11 features.
Function calling:
So, in addition to some basic parameters, the user specifies any number of "fields" that all contain 3 parts: 1) A string identifier 2) how they want the data presented (standard format string) and 3) the default value. Later they may post events using the string identifier and providing new data (which will be formatted with the previously provided format string).
The code that enables this looks something like:
With this I more or less have an easy way of letting users define a table (a processor cache in this case), where they can specify any number of "Fields" per entry, along with how they want the data to be formatted, and being able to submit data updates at a later time.
The 2 options I currently see using "standard" c++ are...
1) Give up on variable number of parameters and go to code like this (with the last parameter being a template parameter
2) Defining a *large* number of versions of the function myself
I probably need to format/word this post better... I can code something that "works", but am just wondering is there a better way?
Function calling:
Code:
// Creating a structure
traceMem = new MemStruct(name, sets, ways,
"PhysAddr", "0x%08x", 0xDEADBEEF,
"Dirty", "%1d", false,
"Valid", "%1d", false,
"Data", "0x%08x", 0x0);
// Later using it
traceMem->update(set, way, "PhysAddr", newAddr,
"Valid", true);
The code that enables this looks something like:
Code:
// CRTP helper class that does the actual work
template<typename Derived>
class Fields
{
protected:
template<typename DataType, typename ...Params>
void addField(const std::string& name, const std::string& format,
const DataType& default_value, const Params& ...params)
{
static_assert(sizeof ...(params) % 3 == 0,
"Wrong number of parameters to addField()");
... omitted code ...
addField(params...);
}
void addField(void) { }
private:
... omitted code ...
};
// Real class
class MemStruct : public Fields<MemStruct>
{
... omitted code ...
}
The 2 options I currently see using "standard" c++ are...
1) Give up on variable number of parameters and go to code like this (with the last parameter being a template parameter
Code:
// Creating a structure
traceMem = new MemStruct(name, sets, ways);
traceMem->addField("PhysAddr", "0x%08x", 0xDEADBEEF);
traceMem->addField("Dirty", "%1d", false);
traceMem->addField("Valid", "%1d", false);
traceMem->addField("Data", "0x%08x", 0x0);
// Later using it
traceMem->update(set, way, "PhysAddr", newAddr);
traceMem->update(set, way, "Valid", true);
Code:
// One argument
template<typename T1> void addField(const std::string& name, const std::string& format, const T1& val);
// Two arguments
template<typename T1, typename T2> void addField(const std::string& name1, const std::string& format1, const T1& val1,
const std::string& name2, const std::string& format2, const T2& val2);
.... (until I think I have more than anyone would really use)
I probably need to format/word this post better... I can code something that "works", but am just wondering is there a better way?