| Index: third_party/cld/base/varsetter.h
|
| diff --git a/third_party/cld/base/varsetter.h b/third_party/cld/base/varsetter.h
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..c51d5c0dc6815eba245c01795fcc7861b135fa1e
|
| --- /dev/null
|
| +++ b/third_party/cld/base/varsetter.h
|
| @@ -0,0 +1,57 @@
|
| +//
|
| +// Copyright (C) 1999 and onwards Google, Inc.
|
| +//
|
| +// Author: Ray Sidney
|
| +//
|
| +#ifndef BASE_VARSETTER_H_
|
| +#define BASE_VARSETTER_H_
|
| +
|
| +//#include "base/port.h"
|
| +
|
| +//
|
| +// Use a VarSetter object to temporarily set an object of some sort to
|
| +// a particular value. When the VarSetter object is destructed, the
|
| +// underlying object will revert to its former value.
|
| +//
|
| +// Sample code:
|
| +//
|
| +#if 0
|
| +{
|
| + bool b = true;
|
| + {
|
| + VarSetter<bool> bool_setter(&b, false);
|
| + // Now b == false.
|
| + }
|
| + // Now b == true again.
|
| +}
|
| +#endif
|
| +
|
| +template <class C>
|
| +class VarSetter {
|
| +public:
|
| +
|
| + // Constructor that just sets the object to a fixed value
|
| + VarSetter(C* object, const C& value) : object_(object), old_value_(*object) {
|
| + *object = value;
|
| + }
|
| +
|
| + ~VarSetter() { *object_ = old_value_; }
|
| +
|
| +private:
|
| +
|
| + C*const object_;
|
| + C old_value_;
|
| +
|
| + // Disallow
|
| + VarSetter(const VarSetter&);
|
| + VarSetter& operator=(const VarSetter&);
|
| +
|
| + // VarSetters always live on the stack
|
| + static void* operator new (size_t);
|
| + static void* operator new[](size_t); // Redundant, no default ctor
|
| +
|
| + static void operator delete (void*);
|
| + static void operator delete[](void*);
|
| +};
|
| +
|
| +#endif // BASE_VARSETTER_H_
|
|
|