status.h 5.05 KB
Newer Older
gejun's avatar
gejun committed
1
// Copyright (c) 2014 Baidu, Inc.
gejun's avatar
gejun committed
2 3 4 5 6 7 8 9 10 11 12 13 14
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

gejun's avatar
gejun committed
15 16 17 18 19 20 21
// Author Zhangyi Chen (chenzhangyi01@baidu.com)
// Date 2014/09/22 11:57:43

#ifndef  BVAR_STATUS_H
#define  BVAR_STATUS_H

#include <string>                       // std::string
22 23 24 25
#include "butil/atomicops.h"
#include "butil/type_traits.h"
#include "butil/string_printf.h"
#include "butil/synchronization/lock.h"
gejun's avatar
gejun committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
#include "bvar/detail/is_atomical.h"
#include "bvar/variable.h"

namespace bvar {

// Display a rarely or periodically updated value.
// Usage:
//   bvar::Status<int> foo_count1(17);
//   foo_count1.expose("my_value");
//
//   bvar::Status<int> foo_count2;
//   foo_count2.set_value(17);
//   
//   bvar::Status<int> foo_count3("my_value", 17);
template <typename T, typename Enabler = void>
class Status : public Variable {
public:
    Status() {}
    Status(const T& value) : _value(value) {}
45
    Status(const butil::StringPiece& name, const T& value) : _value(value) {
gejun's avatar
gejun committed
46 47
        this->expose(name);
    }
48 49
    Status(const butil::StringPiece& prefix,
           const butil::StringPiece& name, const T& value) : _value(value) {
gejun's avatar
gejun committed
50 51 52 53 54 55 56 57 58 59 60 61
        this->expose_as(prefix, name);
    }
    // Calling hide() manually is a MUST required by Variable.
    ~Status() { hide(); }

    // Implement Variable::describe() and Variable::get_value().
    void describe(std::ostream& os, bool /*quote_string*/) const {
        os << get_value();
    }
    
#ifdef BAIDU_INTERNAL
    void get_value(boost::any* value) const {
62
        butil::AutoLock guard(_lock);
gejun's avatar
gejun committed
63 64 65 66 67
        *value = _value;
    }
#endif

    T get_value() const {
68
        butil::AutoLock guard(_lock);
gejun's avatar
gejun committed
69 70 71 72 73
        const T res = _value;
        return res;
    }

    void set_value(const T& value) {
74
        butil::AutoLock guard(_lock);
gejun's avatar
gejun committed
75 76 77 78 79
        _value = value;
    }

private:
    T _value;
80 81 82
    // We use lock rather than butil::atomic for generic values because
    // butil::atomic requires the type to be memcpy-able (POD basically)
    mutable butil::Lock _lock;
gejun's avatar
gejun committed
83 84 85
};

template <typename T>
86
class Status<T, typename butil::enable_if<detail::is_atomical<T>::value>::type>
gejun's avatar
gejun committed
87 88 89 90
    : public Variable {
public:
    Status() {}
    Status(const T& value) : _value(value) { }
91
    Status(const butil::StringPiece& name, const T& value) : _value(value) {
gejun's avatar
gejun committed
92 93
        this->expose(name);
    }
94 95
    Status(const butil::StringPiece& prefix,
           const butil::StringPiece& name, const T& value) : _value(value) {
gejun's avatar
gejun committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
        this->expose_as(prefix, name);
    }
    ~Status() { hide(); }

    // Implement Variable::describe() and Variable::get_value().
    void describe(std::ostream& os, bool /*quote_string*/) const {
        os << get_value();
    }
    
#ifdef BAIDU_INTERNAL
    void get_value(boost::any* value) const {
        *value = get_value();
    }
#endif
    
    T get_value() const {
112
        return _value.load(butil::memory_order_relaxed);
gejun's avatar
gejun committed
113 114 115
    }
    
    void set_value(const T& value) {
116
        _value.store(value, butil::memory_order_relaxed);
gejun's avatar
gejun committed
117 118 119
    }

private:
120
    butil::atomic<T> _value;
gejun's avatar
gejun committed
121 122 123 124 125 126 127
};

// Specialize for std::string, adding a printf-style set_value().
template <>
class Status<std::string, void> : public Variable {
public:
    Status() {}
128
    Status(const butil::StringPiece& name, const char* fmt, ...) {
gejun's avatar
gejun committed
129 130 131
        if (fmt) {
            va_list ap;
            va_start(ap, fmt);
132
            butil::string_vprintf(&_value, fmt, ap);
gejun's avatar
gejun committed
133 134 135 136
            va_end(ap);
        }
        expose(name);
    }
137 138
    Status(const butil::StringPiece& prefix,
           const butil::StringPiece& name, const char* fmt, ...) {
gejun's avatar
gejun committed
139 140 141
        if (fmt) {
            va_list ap;
            va_start(ap, fmt);
142
            butil::string_vprintf(&_value, fmt, ap);
gejun's avatar
gejun committed
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
            va_end(ap);
        }
        expose_as(prefix, name);
    }

    ~Status() { hide(); }

    void describe(std::ostream& os, bool quote_string) const {
        if (quote_string) {
            os << '"' << get_value() << '"';
        } else {
            os << get_value();
        }
    }

    std::string get_value() const {
159
        butil::AutoLock guard(_lock);
gejun's avatar
gejun committed
160 161 162 163 164 165 166 167 168 169 170 171 172
        return _value;
    }

#ifdef BAIDU_INTERNAL
    void get_value(boost::any* value) const {
        *value = get_value();
    }
#endif
    
    void set_value(const char* fmt, ...) {
        va_list ap;
        va_start(ap, fmt);
        {
173 174
            butil::AutoLock guard(_lock);
            butil::string_vprintf(&_value, fmt, ap);
gejun's avatar
gejun committed
175 176 177 178 179
        }
        va_end(ap);
    }

    void set_value(const std::string& s) {
180
        butil::AutoLock guard(_lock);
gejun's avatar
gejun committed
181 182 183 184 185
        _value = s;
    }

private:
    std::string _value;
186
    mutable butil::Lock _lock;
gejun's avatar
gejun committed
187 188 189 190 191
};

}  // namespace bvar

#endif  //BVAR_STATUS_H