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