• 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 
11 #include <algorithm>
12 
13 #include "ast/ast_interface_type.h"
14 #include "ast/ast_sequenceable_type.h"
15 
16 namespace OHOS {
17 namespace HDI {
ASTNamespace(const std::string & nspaceStr)18 ASTNamespace::ASTNamespace(const std::string &nspaceStr) : name_(nspaceStr), outerNamespace_(nullptr) {}
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 std::string & nspaceStr)30 AutoPtr<ASTNamespace> ASTNamespace::FindNamespace(const std::string &nspaceStr)
31 {
32     if (nspaceStr.empty()) {
33         return nullptr;
34     }
35 
36     auto resIter = std::find_if(
37         innerNamespaces_.begin(), innerNamespaces_.end(), [nspaceStr](const AutoPtr<ASTNamespace> &element) {
38             return element->name_ == nspaceStr;
39         });
40     return resIter != innerNamespaces_.end() ? *resIter : nullptr;
41 }
42 
GetNamespace(size_t index)43 AutoPtr<ASTNamespace> ASTNamespace::GetNamespace(size_t index)
44 {
45     if (index >= innerNamespaces_.size()) {
46         return nullptr;
47     }
48 
49     return innerNamespaces_[index];
50 }
51 
AddInterface(const AutoPtr<ASTInterfaceType> & interface)52 void ASTNamespace::AddInterface(const AutoPtr<ASTInterfaceType> &interface)
53 {
54     if (interface == nullptr) {
55         return;
56     }
57 
58     interfaces_.push_back(interface);
59 }
60 
GetInterface(size_t index)61 AutoPtr<ASTInterfaceType> ASTNamespace::GetInterface(size_t index)
62 {
63     if (index >= interfaces_.size()) {
64         return nullptr;
65     }
66 
67     return interfaces_[index];
68 }
69 
AddSequenceable(const AutoPtr<ASTSequenceableType> & sequenceable)70 void ASTNamespace::AddSequenceable(const AutoPtr<ASTSequenceableType> &sequenceable)
71 {
72     if (sequenceable == nullptr) {
73         return;
74     }
75 
76     sequenceables_.push_back(sequenceable);
77 }
78 
GetSequenceable(size_t index)79 AutoPtr<ASTSequenceableType> ASTNamespace::GetSequenceable(size_t index)
80 {
81     if (index >= sequenceables_.size()) {
82         return nullptr;
83     }
84 
85     return sequenceables_[index];
86 }
87 
ToString() const88 std::string ASTNamespace::ToString() const
89 {
90     std::string nspaceStr;
91     const ASTNamespace *nspace = this;
92     while (nspace != nullptr) {
93         nspaceStr = nspace->name_ + "." + nspaceStr;
94         nspace = nspace->outerNamespace_;
95     }
96     return nspaceStr;
97 }
98 } // namespace HDI
99 } // namespace OHOS