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 17 #include "debug_output.h" 18 19 #include <string> 20 21 META_BEGIN_NAMESPACE() 22 namespace Serialization { 23 Process(const ISerNode::Ptr & tree)24BASE_NS::string DebugOutput::Process(const ISerNode::Ptr& tree) 25 { 26 if (tree) { 27 tree->Apply(*this); 28 } 29 return result_; 30 } 31 Visit(const IRootNode & n)32void DebugOutput::Visit(const IRootNode& n) 33 { 34 if (auto o = n.GetObject()) { 35 o->Apply(*this); 36 } 37 } Visit(const INilNode & n)38void DebugOutput::Visit(const INilNode& n) 39 { 40 Output("<null>"); 41 } Visit(const IObjectNode & n)42void DebugOutput::Visit(const IObjectNode& n) 43 { 44 IncOutputIndent(); 45 Output("Object [name=" + n.GetObjectName() + "]\n"); 46 IncOutputIndent(); 47 if (auto members = n.GetMembers()) { 48 members->Apply(*this); 49 } 50 DecIndentEndLine(); 51 DecIndentEndLine(); 52 } Visit(const IArrayNode & n)53void DebugOutput::Visit(const IArrayNode& n) 54 { 55 bool first = true; 56 for (auto&& v : n.GetMembers()) { 57 if (!first) { 58 NewLine(); 59 first = false; 60 } 61 if (v) { 62 v->Apply(*this); 63 } 64 } 65 } Visit(const IMapNode & n)66void DebugOutput::Visit(const IMapNode& n) 67 { 68 bool first = true; 69 for (auto&& v : n.GetMembers()) { 70 if (!first) { 71 NewLine(); 72 first = false; 73 } 74 Output(v.name + ": "); 75 if (v.node) { 76 v.node->Apply(*this); 77 } 78 } 79 } Visit(const IBuiltinValueNode<bool> & n)80void DebugOutput::Visit(const IBuiltinValueNode<bool>& n) 81 { 82 Output(std::to_string(n.GetValue()).c_str()); 83 } Visit(const IBuiltinValueNode<double> & n)84void DebugOutput::Visit(const IBuiltinValueNode<double>& n) 85 { 86 Output(std::to_string(n.GetValue()).c_str()); 87 } Visit(const IBuiltinValueNode<int64_t> & n)88void DebugOutput::Visit(const IBuiltinValueNode<int64_t>& n) 89 { 90 Output(std::to_string(n.GetValue()).c_str()); 91 } Visit(const IBuiltinValueNode<uint64_t> & n)92void DebugOutput::Visit(const IBuiltinValueNode<uint64_t>& n) 93 { 94 Output(std::to_string(n.GetValue()).c_str()); 95 } Visit(const IBuiltinValueNode<BASE_NS::string> & n)96void DebugOutput::Visit(const IBuiltinValueNode<BASE_NS::string>& n) 97 { 98 Output(n.GetValue()); 99 } Visit(const IBuiltinValueNode<RefUri> & n)100void DebugOutput::Visit(const IBuiltinValueNode<RefUri>& n) 101 { 102 Output(n.GetValue().ToString()); 103 } Visit(const ISerNode & n)104void DebugOutput::Visit(const ISerNode& n) 105 { 106 result_ += "<Unknown node type>\n"; 107 } Output(const BASE_NS::string & v)108void DebugOutput::Output(const BASE_NS::string& v) 109 { 110 result_ += v; 111 } NewLine()112void DebugOutput::NewLine() 113 { 114 result_ += "\n" + BASE_NS::string(indent_, '\t'); 115 } IncOutputIndent()116void DebugOutput::IncOutputIndent() 117 { 118 ++indent_; 119 result_ += BASE_NS::string(indent_, '\t'); 120 } DecIndentEndLine()121void DebugOutput::DecIndentEndLine() 122 { 123 --indent_; 124 result_ += "\n"; 125 } 126 127 } // namespace Serialization 128 META_END_NAMESPACE()