1 /* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include "debug_output.h" 17 18 #include <string> 19 20 META_BEGIN_NAMESPACE() 21 namespace Serialization { 22 Process(const ISerNode::Ptr & tree)23BASE_NS::string DebugOutput::Process(const ISerNode::Ptr& tree) 24 { 25 if (tree) { 26 tree->Apply(*this); 27 } 28 return result_; 29 } 30 Visit(const IRootNode & n)31void DebugOutput::Visit(const IRootNode& n) 32 { 33 if (auto o = n.GetObject()) { 34 o->Apply(*this); 35 } 36 } Visit(const INilNode & n)37void DebugOutput::Visit(const INilNode& n) 38 { 39 Output("<null>"); 40 } Visit(const IObjectNode & n)41void DebugOutput::Visit(const IObjectNode& n) 42 { 43 IncOutputIndent(); 44 Output("Object [name=" + n.GetObjectName() + "]\n"); 45 IncOutputIndent(); 46 if (auto members = n.GetMembers()) { 47 members->Apply(*this); 48 } 49 DecIndentEndLine(); 50 DecIndentEndLine(); 51 } Visit(const IArrayNode & n)52void DebugOutput::Visit(const IArrayNode& n) 53 { 54 bool first = true; 55 for (auto&& v : n.GetMembers()) { 56 if (!first) { 57 NewLine(); 58 first = false; 59 } 60 if (v) { 61 v->Apply(*this); 62 } 63 } 64 } Visit(const IMapNode & n)65void DebugOutput::Visit(const IMapNode& n) 66 { 67 bool first = true; 68 for (auto&& v : n.GetMembers()) { 69 if (!first) { 70 NewLine(); 71 first = false; 72 } 73 Output(v.name + ": "); 74 if (v.node) { 75 v.node->Apply(*this); 76 } 77 } 78 } Visit(const IBuiltinValueNode<bool> & n)79void DebugOutput::Visit(const IBuiltinValueNode<bool>& n) 80 { 81 Output(std::to_string(n.GetValue()).c_str()); 82 } Visit(const IBuiltinValueNode<double> & n)83void DebugOutput::Visit(const IBuiltinValueNode<double>& n) 84 { 85 Output(std::to_string(n.GetValue()).c_str()); 86 } Visit(const IBuiltinValueNode<int64_t> & n)87void DebugOutput::Visit(const IBuiltinValueNode<int64_t>& n) 88 { 89 Output(std::to_string(n.GetValue()).c_str()); 90 } Visit(const IBuiltinValueNode<uint64_t> & n)91void DebugOutput::Visit(const IBuiltinValueNode<uint64_t>& n) 92 { 93 Output(std::to_string(n.GetValue()).c_str()); 94 } Visit(const IBuiltinValueNode<BASE_NS::string> & n)95void DebugOutput::Visit(const IBuiltinValueNode<BASE_NS::string>& n) 96 { 97 Output(n.GetValue()); 98 } Visit(const IBuiltinValueNode<RefUri> & n)99void DebugOutput::Visit(const IBuiltinValueNode<RefUri>& n) 100 { 101 Output(n.GetValue().ToString()); 102 } Visit(const ISerNode & n)103void DebugOutput::Visit(const ISerNode& n) 104 { 105 result_ += "<Unknown node type>\n"; 106 } Output(const BASE_NS::string & v)107void DebugOutput::Output(const BASE_NS::string& v) 108 { 109 result_ += v; 110 } NewLine()111void DebugOutput::NewLine() 112 { 113 result_ += "\n" + BASE_NS::string(indent_, '\t'); 114 } IncOutputIndent()115void DebugOutput::IncOutputIndent() 116 { 117 ++indent_; 118 result_ += BASE_NS::string(indent_, '\t'); 119 } DecIndentEndLine()120void DebugOutput::DecIndentEndLine() 121 { 122 --indent_; 123 result_ += "\n"; 124 } 125 126 } // namespace Serialization 127 META_END_NAMESPACE()