I have a class consisting solely of static attributes acting as a sort of poor man’s singleton. The purpose of which is to collect statistics from various points in the application. For our unit tests I created a shadow copy that can be used to get the difference between the one in production code.
Example:
struct Production {
static ComplexClass value1;
static ComplexClass value2;
};
struct ProductionShadow {
static ComplexClass::ValueType value1;
static ComplexClass::ValueType value2;
};
Since all members are static the shadow class cannot inherit from the production class and the attributes in the production class are complex objects with side effects and I only want the shadow to hold simple values for comparison.
In order to make the shadow class somewhat easier to maintain I wanted to add a static assert to check that whenever someone adds an attribute to the production class the shadow class is also updated.
Had the attributes not been static this could have easily been accomplished by doing the following:
enum { NumberOfProductionAttributes = sizeof(Production) / sizeof(ComplexClass),
NumberOfShadowAttributes = sizeof(ProductionShadow) / sizeof(ComplexClass::ValueType) };
STATIC_ASSERT(NumberOfProductionAttributes == NumberOfShadowAttributes);
Apart from making the classes non-static, which I’d rather not do as I would have to turn the production class into a proper singleton, is there any way I can accomplish this check with the current implementation?
In runtime I have a method that verifies all the values in the shadow with the production class. Since the method has to be updated as well when a new attribute is added I would like to be able to count the number of attributes that have been verified and assert if the amount is different from the total number of attributes.
Example
bool ProductionShadow::verify() {
std::size_t numberOfVerified = 0;
#define VERIFY(x) do { \
++numberOfVerified; \
// Verification code goes here \
} while (0)
VERIFY(value1);
VERIFY(value2);
ASSERT(numberOfVerified == NumberOfShadowAttributes);
// Return result
}
If there is a GCC extension I can use that might be an acceptable solution even though I would prefer something portable.
Just in case, here is the solution I suggested in the comments to the question. The idea is to create two classes that contain the data members in question in the form of non-static members:
And then redefine the original classes to contain the classes defined above as static members. They would continue to function as before, and size checks could be performed against the types defined above.
Admittedly, this means a change in how the static members are accessed (because they are now members of another member); therefore many changes to existing code might be required.