代码拉取完成,页面将自动刷新
同步操作将从 xyz347/xpack 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
/*
* Copyright (C) 2021 Duowan Inc. All rights reserved.
*
* 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.
*/
#ifndef __X_PACK_YAML_ENCODER_H
#define __X_PACK_YAML_ENCODER_H
#include <string>
#include "yaml-cpp/yaml.h"
#include "xencoder.h"
namespace xpack {
class YamlWriter:private noncopyable {
friend class XEncoder<YamlWriter>;
friend class YamlEncoder;
const static bool support_null = true;
public:
YamlWriter(size_t indent = 2, int maxDecimalPlaces = -1) {
e.SetIndent(indent);
if (maxDecimalPlaces > 0) {
e.SetFloatPrecision((size_t)maxDecimalPlaces);
e.SetDoublePrecision((size_t)maxDecimalPlaces);
}
}
~YamlWriter() {
}
private:
inline static const char *Name() {
return "yaml";
}
inline const char *IndexKey(size_t index) {
(void)index;
return NULL;
}
std::string String() {
return std::string(e.c_str(), e.size());
}
void ArrayBegin(const char *key, const Extend *ext) {
(void)ext;
xpack_set_key(key);
e << YAML::Value << YAML::BeginSeq;
}
void ArrayEnd(const char *key, const Extend *ext) {
(void)key;
(void)ext;
e << YAML::EndSeq;
}
void ObjectBegin(const char *key, const Extend *ext) {
(void)ext;
xpack_set_key(key);
e << YAML::Value << YAML::BeginMap;
}
void ObjectEnd(const char *key, const Extend *ext) {
(void)key;
(void)ext;
e << YAML::EndMap;
}
bool WriteNull(const char*key, const Extend *ext) {
(void)ext;
xpack_set_key(key);
e << YAML::Value << YAML::Null;
return true;
}
bool encode_bool(const char*key, const bool&val, const Extend *ext) {
(void)ext;
xpack_set_key(key);
e << YAML::Value << val;
return true;
}
bool encode_string(const char*key, const std::string&val, const Extend *ext) {
(void)ext;
xpack_set_key(key);
if (!val.empty()) {
e << YAML::Value << val;
} else {
e << YAML::Value << YAML::Null;
}
return true;
}
template <typename T>
typename x_enable_if<numeric<T>::value, bool>::type encode_number(const char*key, const T&val, const Extend *ext) {
(void)ext;
xpack_set_key(key);
e << YAML::Value << val;
return true;
}
void xpack_set_key(const char*key) { // openssl defined set_key macro, so we named it xpack_set_key
if (NULL!=key && key[0]!='\0') {
e << YAML::Key << key;
}
}
YAML::Emitter e;
};
class YamlEncoder {
public:
YamlEncoder() {
indentCount = 2;
maxDecimalPlaces = -1;
}
YamlEncoder& SetIndent(size_t indent) {
indentCount = indent;
return *this;
}
YamlEncoder& SetMaxDecimalPlaces(int _maxDecimalPlaces) {
maxDecimalPlaces = _maxDecimalPlaces;
return *this;
}
template <class T>
std::string encode(const T&val) {
YamlWriter wr(indentCount, maxDecimalPlaces);
XEncoder<YamlWriter> en(wr);
en.encode(NULL, val, NULL);
return wr.String();
}
private:
size_t indentCount;
int maxDecimalPlaces;
};
}
#endif
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。