Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Commit 68da304

Browse files
authored
Make WithContext conditional. (#207)
While here, add an example to the docs.
1 parent a4b19bc commit 68da304

3 files changed

Lines changed: 41 additions & 18 deletions

File tree

opencensus/context/internal/with_context.cc

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,26 @@
2222
namespace opencensus {
2323
namespace context {
2424

25-
WithContext::WithContext(const Context& ctx)
26-
: swapped_context_(ctx)
25+
WithContext::WithContext(const Context& ctx, bool cond)
26+
: swapped_context_(cond ? ctx : Context())
2727
#ifndef NDEBUG
2828
,
2929
original_context_(Context::InternalMutableCurrent())
3030
#endif
31-
{
32-
using std::swap;
33-
swap(*Context::InternalMutableCurrent(), swapped_context_);
31+
,
32+
cond_(cond) {
33+
ConditionalSwap();
3434
}
3535

36-
WithContext::WithContext(Context&& ctx)
37-
: swapped_context_(std::move(ctx))
36+
WithContext::WithContext(Context&& ctx, bool cond)
37+
: swapped_context_(cond ? std::move(ctx) : Context())
3838
#ifndef NDEBUG
3939
,
4040
original_context_(Context::InternalMutableCurrent())
4141
#endif
42-
{
43-
using std::swap;
44-
swap(*Context::InternalMutableCurrent(), swapped_context_);
42+
,
43+
cond_(cond) {
44+
ConditionalSwap();
4545
}
4646

4747
WithContext::~WithContext() {
@@ -50,8 +50,14 @@ WithContext::~WithContext() {
5050
"WithContext must be destructed on the same thread as it was "
5151
"constructed.");
5252
#endif
53-
using std::swap;
54-
swap(*Context::InternalMutableCurrent(), swapped_context_);
53+
ConditionalSwap();
54+
}
55+
56+
void WithContext::ConditionalSwap() {
57+
if (cond_) {
58+
using std::swap;
59+
swap(*Context::InternalMutableCurrent(), swapped_context_);
60+
}
5561
}
5662

5763
} // namespace context

opencensus/context/internal/with_context_test.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ TEST(WithContextTest, WithContextMovable) {
3535
opencensus::context::WithContext wc(std::move(ctx));
3636
}
3737

38+
TEST(WithContextTest, WithContextConditional) {
39+
opencensus::context::Context ctx = opencensus::context::Context::Current();
40+
opencensus::context::WithContext wc1(ctx, true);
41+
opencensus::context::WithContext wc2(ctx, false);
42+
// TODO: Test swaps.
43+
}
44+
3845
#ifndef NDEBUG
3946
TEST(WithContextDeathTest, DestructorOnWrongThread) {
4047
opencensus::context::Context ctx = opencensus::context::Context::Current();

opencensus/context/with_context.h

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,22 @@ namespace opencensus {
2121
namespace context {
2222

2323
// WithContext is a scoped object that sets the current Context to the given
24-
// one, until the WithContext object is destroyed.
24+
// one, until the WithContext object is destroyed. If the condition is false, it
25+
// doesn't do anything.
2526
//
26-
// Because it changes the current (thread local) context, NEVER allocate a
27-
// WithContext in one thread and deallocate in another. A simple way to ensure
28-
// this is to only ever stack-allocate it.
27+
// Because WithContext changes the current (thread local) context, NEVER
28+
// allocate a WithContext in one thread and deallocate in another. A simple way
29+
// to ensure this is to only ever stack-allocate it.
30+
//
31+
// Example usage:
32+
// {
33+
// WithContext wc(op.ctx_);
34+
// // Do work.
35+
// }
2936
class WithContext {
3037
public:
31-
explicit WithContext(const Context& ctx);
32-
explicit WithContext(Context&& ctx);
38+
explicit WithContext(const Context& ctx, bool cond = true);
39+
explicit WithContext(Context&& ctx, bool cond = true);
3340
~WithContext();
3441

3542
private:
@@ -39,10 +46,13 @@ class WithContext {
3946
WithContext& operator=(const WithContext&) = delete;
4047
WithContext& operator=(WithContext&&) = delete;
4148

49+
void ConditionalSwap();
50+
4251
Context swapped_context_;
4352
#ifndef NDEBUG
4453
const Context* original_context_;
4554
#endif
55+
const bool cond_;
4656
};
4757

4858
} // namespace context

0 commit comments

Comments
 (0)