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 "ast/ast_namespace.h" 17 18 #include <algorithm> 19 20 #include "ast/ast_interface_type.h" 21 #include "ast/ast_rawdata_type.h" 22 #include "ast/ast_sequenceable_type.h" 23 24 namespace OHOS { 25 namespace Idl { ASTNamespace(const std::string & nspaceStr)26ASTNamespace::ASTNamespace(const std::string &nspaceStr) : name_(nspaceStr), outerNamespace_(nullptr) {} 27 AddNamespace(const AutoPtr<ASTNamespace> & innerNspace)28void ASTNamespace::AddNamespace(const AutoPtr<ASTNamespace> &innerNspace) 29 { 30 if (innerNspace == nullptr) { 31 return; 32 } 33 34 innerNamespaces_.push_back(innerNspace); 35 innerNspace->outerNamespace_ = this; 36 } 37 FindNamespace(const std::string & nspaceStr)38AutoPtr<ASTNamespace> ASTNamespace::FindNamespace(const std::string &nspaceStr) 39 { 40 if (nspaceStr.empty()) { 41 return nullptr; 42 } 43 44 auto resIter = std::find_if( 45 innerNamespaces_.begin(), innerNamespaces_.end(), [nspaceStr](const AutoPtr<ASTNamespace> &element) { 46 return element->name_ == nspaceStr; 47 }); 48 return resIter != innerNamespaces_.end() ? *resIter : nullptr; 49 } 50 GetNamespace(size_t index)51AutoPtr<ASTNamespace> ASTNamespace::GetNamespace(size_t index) 52 { 53 if (index >= innerNamespaces_.size()) { 54 return nullptr; 55 } 56 57 return innerNamespaces_[index]; 58 } 59 AddInterface(const AutoPtr<ASTInterfaceType> & interface)60void ASTNamespace::AddInterface(const AutoPtr<ASTInterfaceType> &interface) 61 { 62 if (interface == nullptr) { 63 return; 64 } 65 66 interfaces_.push_back(interface); 67 } 68 GetInterface(size_t index)69AutoPtr<ASTInterfaceType> ASTNamespace::GetInterface(size_t index) 70 { 71 if (index >= interfaces_.size()) { 72 return nullptr; 73 } 74 75 return interfaces_[index]; 76 } 77 AddSequenceable(const AutoPtr<ASTSequenceableType> & sequenceable)78void ASTNamespace::AddSequenceable(const AutoPtr<ASTSequenceableType> &sequenceable) 79 { 80 if (sequenceable == nullptr) { 81 return; 82 } 83 84 sequenceables_.push_back(sequenceable); 85 } 86 GetSequenceable(size_t index)87AutoPtr<ASTSequenceableType> ASTNamespace::GetSequenceable(size_t index) 88 { 89 if (index >= sequenceables_.size()) { 90 return nullptr; 91 } 92 93 return sequenceables_[index]; 94 } 95 AddRawData(const AutoPtr<ASTRawDataType> & rawdata)96void ASTNamespace::AddRawData(const AutoPtr<ASTRawDataType> &rawdata) 97 { 98 if (rawdata == nullptr) { 99 return; 100 } 101 102 rawdatas_.push_back(rawdata); 103 } 104 GetRawData(size_t index)105AutoPtr<ASTRawDataType> ASTNamespace::GetRawData(size_t index) 106 { 107 if (index >= rawdatas_.size()) { 108 return nullptr; 109 } 110 111 return rawdatas_[index]; 112 } 113 ToString() const114std::string ASTNamespace::ToString() const 115 { 116 std::string nspaceStr; 117 const ASTNamespace *nspace = this; 118 while (nspace != nullptr) { 119 nspaceStr = nspace->name_ + "." + nspaceStr; 120 nspace = nspace->outerNamespace_; 121 } 122 return nspaceStr; 123 } 124 } // namespace Idl 125 } // namespace OHOS