1 /*
2  * Copyright (c) Facebook, Inc. and its affiliates.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree.
7  */
8 
9 #pragma once
10 
11 #include <proxygen/lib/sampling/Sampling.h>
12 
13 namespace proxygen {
14 
15 /**
16  * implements the concept of a sampled object, which maintains the side effects
17  * of using a sampling object. Designed to be inherited.
18  */
19 class Sampled {
20 
21  public:
Sampled()22   Sampled() {
23   }
24 
Sampled(const Sampling & sampling)25   explicit Sampled(const Sampling& sampling) {
26     sample(sampling);
27   }
28 
~Sampled()29   virtual ~Sampled() {
30   }
31 
sample(const Sampling & sampling)32   void sample(const Sampling& sampling) {
33     if (sampling.isLucky()) {
34       samplingWeight_ = sampling.getWeight();
35     }
36   }
37 
getSamplingWeight()38   uint32_t getSamplingWeight() const {
39     return samplingWeight_;
40   }
41 
isSampled()42   bool isSampled() const {
43     return samplingWeight_ > 0;
44   }
45 
46  protected:
47   uint32_t samplingWeight_{0};
48 };
49 
50 } // namespace proxygen
51