8159370: Add FlagGuard for easier modification of flags for unit tests

Reviewed-by: kbarrett, jwilhelm
This commit is contained in:
Erik Helin 2016-06-21 15:02:45 +02:00
parent 161976b05f
commit 3895ad9e00
2 changed files with 97 additions and 0 deletions

View file

@ -463,6 +463,29 @@ class SizeTFlagSetting {
~SizeTFlagSetting() { *flag = val; }
};
// Helper class for temporarily saving the value of a flag during a scope.
template <size_t SIZE>
class FlagGuard {
unsigned char _value[SIZE];
void* const _addr;
// Hide operator new, this class should only be allocated on the stack.
// NOTE: Cannot include memory/allocation.hpp here due to circular
// dependencies.
void* operator new(size_t size) throw();
void* operator new [](size_t size) throw();
public:
FlagGuard(void* flag_addr) : _addr(flag_addr) {
memcpy(_value, _addr, SIZE);
}
~FlagGuard() {
memcpy(_addr, _value, SIZE);
}
};
#define FLAG_GUARD(f) FlagGuard<sizeof(f)> f ## _guard(&f)
class CommandLineFlags {
public: