Coverage Report

Created: 2025-10-28 07:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/brpc/src/bvar/mvariable.h
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
// Date: 2021/11/17 14:37:53
19
20
#ifndef BVAR_MVARIABLE_H
21
#define BVAR_MVARIABLE_H
22
23
#include <ostream>                      // std::ostream
24
#include <sstream>                      // std::ostringstream
25
#include <list>                         // std::list
26
#include <string>                       // std::string
27
#include "butil/macros.h"               // DISALLOW_COPY_AND_ASSIGN
28
#include "butil/strings/string_piece.h" // butil::StringPiece
29
30
namespace bvar {
31
32
class Dumper;
33
struct DumpOptions;
34
35
class MVariableBase {
36
public:
37
    MVariableBase() = default;
38
39
    virtual ~MVariableBase();
40
41
    // Implement this method to print the mvariable info into ostream.
42
    virtual void describe(std::ostream&) = 0;
43
44
    // string form of describe().
45
    std::string get_description();
46
   
47
    // Get mvariable name
48
0
    const std::string& name() const { return _name; }
49
50
    // Expose this mvariable globally so that it's counted in following
51
    // functions:
52
    //      list_exposed
53
    //      count_exposed
54
    // Return 0 on success, -1 otherwise.
55
0
    int expose(const butil::StringPiece& name) {
56
0
        return expose_impl(butil::StringPiece(), name);
57
0
    }
58
59
    // Expose this mvariable globally with a prefix
60
    // Return 0 on success, -1 otherwise.
61
    int expose_as(const butil::StringPiece& prefix,
62
0
                  const butil::StringPiece& name) {
63
0
        return expose_impl(prefix, name);
64
0
    }
65
66
    // Dump this mvariable
67
    virtual size_t dump(Dumper* dumper, const DumpOptions* options) = 0;
68
69
    // Hide this variable so that it's not counted in *_exposed functions.
70
    // Returns false if this variable is already hidden.
71
    // CAUTION!! Subclasses must call hide() manually to avoid displaying
72
    // a variable that is just destructing.
73
    bool hide();
74
75
    // Get number of exposed mvariables.
76
    static size_t count_exposed();
77
78
    // Put names of all exposed mvariable into `name'.
79
    static void list_exposed(std::vector<std::string> *names);
80
81
    // Find all exposed mvariables matching `white_wildcards' but
82
    // `black_wildcards' and send them to `dumper'.
83
    // Use default options when `options' is NULL.
84
    // Return number of dumped mvariables, -1 on error.
85
    static size_t dump_exposed(Dumper* dumper, const DumpOptions* options);
86
87
    // Find an exposed mvariable by `name' and put its description into `os'.
88
    // Returns 0 on found, -1 otherwise.
89
    static int describe_exposed(const std::string& name,
90
                                std::ostream& os);
91
92
    // String form. Returns empty string when not found.
93
    static std::string describe_exposed(const std::string& name);
94
95
#ifdef UNIT_TEST
96
    // Hide all mvariables so that all mvariables not counted in following
97
    // functions:
98
    //      list_exposed
99
    //      count_exposed
100
    // CAUTION!!! Just For Debug!!!
101
    static void hide_all();
102
#endif
103
104
protected:
105
    int expose_impl(const butil::StringPiece& prefix,
106
                    const butil::StringPiece& name);
107
108
protected:
109
    std::string _name;
110
111
    // mbvar uses bvar, bvar uses TLS, thus copying/assignment need to copy TLS stuff as well,
112
    // which is heavy. We disable copying/assignment now. 
113
    DISALLOW_COPY_AND_ASSIGN(MVariableBase);
114
};
115
116
template <typename KeyType>
117
class MVariable : public MVariableBase {
118
public:
119
    explicit MVariable(const KeyType& labels) : _labels(labels.cbegin(), labels.cend()) {
120
        static_assert(std::is_same<typename KeyType::value_type, std::string>::value,
121
                      "value_type of KeyType must be std::string");
122
    }
123
124
    // Get mvariable labels
125
    const KeyType& labels() const { return _labels; }
126
127
    // Get number of mvariable labels
128
    size_t count_labels() const { return _labels.size(); }
129
130
protected:
131
    KeyType  _labels;
132
};
133
134
} // namespace bvar
135
136
#endif  // BVAR_MVARIABLE_H