• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  *
4  * HDF is dual licensed: you can use it either under the terms of
5  * the GPL, or the BSD license, at your option.
6  * See the LICENSE file in the root of this repository for complete details.
7  */
8 
9 #include "ast/ast_namespace.h"
10 #include "ast/ast_interface_type.h"
11 #include "ast/ast_sequenceable_type.h"
12 
13 namespace OHOS {
14 namespace HDI {
ASTNamespace(const String & nspaceStr)15 ASTNamespace::ASTNamespace(const String& nspaceStr)
16     : name_(nspaceStr),
17       outerNamespace_(nullptr)
18 {}
19 
AddNamespace(const AutoPtr<ASTNamespace> & innerNspace)20 void ASTNamespace::AddNamespace(const AutoPtr<ASTNamespace>& innerNspace)
21 {
22     if (innerNspace == nullptr) {
23         return;
24     }
25 
26     innerNamespaces_.push_back(innerNspace);
27     innerNspace->outerNamespace_ = this;
28 }
29 
FindNamespace(const String & nspaceStr)30 AutoPtr<ASTNamespace> ASTNamespace::FindNamespace(const String& nspaceStr)
31 {
32     if (nspaceStr.IsEmpty()) {
33         return nullptr;
34     }
35 
36     for (auto nspace : innerNamespaces_) {
37         if (nspace->name_.Equals(nspaceStr)) {
38             return nspace;
39         }
40     }
41     return nullptr;
42 }
43 
GetNamespace(size_t index)44 AutoPtr<ASTNamespace> ASTNamespace::GetNamespace(size_t index)
45 {
46     if (index >= innerNamespaces_.size()) {
47         return nullptr;
48     }
49 
50     return innerNamespaces_[index];
51 }
52 
AddInterface(const AutoPtr<ASTInterfaceType> & interface)53 void ASTNamespace::AddInterface(const AutoPtr<ASTInterfaceType>& interface)
54 {
55     if (interface == nullptr) {
56         return;
57     }
58 
59     interfaces_.push_back(interface);
60 }
61 
GetInterface(size_t index)62 AutoPtr<ASTInterfaceType> ASTNamespace::GetInterface(size_t index)
63 {
64     if (index >= interfaces_.size()) {
65         return nullptr;
66     }
67 
68     return interfaces_[index];
69 }
70 
AddSequenceable(const AutoPtr<ASTSequenceableType> & sequenceable)71 void ASTNamespace::AddSequenceable(const AutoPtr<ASTSequenceableType>& sequenceable)
72 {
73     if (sequenceable == nullptr) {
74         return;
75     }
76 
77     sequenceables_.push_back(sequenceable);
78 }
79 
GetSequenceable(size_t index)80 AutoPtr<ASTSequenceableType> ASTNamespace::GetSequenceable(size_t index)
81 {
82     if (index >= sequenceables_.size()) {
83         return nullptr;
84     }
85 
86     return sequenceables_[index];
87 }
88 
ToString()89 String ASTNamespace::ToString()
90 {
91     String nspaceStr;
92     ASTNamespace* nspace = this;
93     while (nspace != nullptr) {
94         nspaceStr = nspace->name_ + "." + nspaceStr;
95         nspace = nspace->outerNamespace_;
96     }
97     return nspaceStr;
98 }
99 } // namespace HDI
100 } // namespace OHOS