]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/tools/lldb-mi/MICmdFactory.cpp
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / tools / lldb-mi / MICmdFactory.cpp
1 //===-- MICmdFactory.cpp ----------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // In-house headers:
11 #include "MICmdFactory.h"
12 #include "MICmdBase.h"
13 #include "MICmdCommands.h"
14 #include "MICmdData.h"
15 #include "MICmnResources.h"
16
17 //++
18 //------------------------------------------------------------------------------------
19 // Details: CMICmdFactory constructor.
20 // Type:    Method.
21 // Args:    None.
22 // Return:  None.
23 // Throws:  None.
24 //--
25 CMICmdFactory::CMICmdFactory() {}
26
27 //++
28 //------------------------------------------------------------------------------------
29 // Details: CMICmdFactory destructor.
30 // Type:    Overridable.
31 // Args:    None.
32 // Return:  None.
33 // Throws:  None.
34 //--
35 CMICmdFactory::~CMICmdFactory() { Shutdown(); }
36
37 //++
38 //------------------------------------------------------------------------------------
39 // Details: Initialize resources for *this Command factory.
40 // Type:    Method.
41 // Args:    None.
42 // Return:  MIstatus::success - Functionality succeeded.
43 //          MIstatus::failure - Functionality failed.
44 // Throws:  None.
45 //--
46 bool CMICmdFactory::Initialize() {
47   m_clientUsageRefCnt++;
48
49   if (m_bInitialized)
50     return MIstatus::success;
51
52   m_bInitialized = true;
53
54   MICmnCommands::RegisterAll();
55
56   return MIstatus::success;
57 }
58
59 //++
60 //------------------------------------------------------------------------------------
61 // Details: Release resources for *this Command Factory.
62 // Type:    Method.
63 // Args:    None.
64 // Return:  MIstatus::success - Functionality succeeded.
65 //          MIstatus::failure - Functionality failed.
66 // Throws:  None.
67 //--
68 bool CMICmdFactory::Shutdown() {
69   if (--m_clientUsageRefCnt > 0)
70     return MIstatus::success;
71
72   if (!m_bInitialized)
73     return MIstatus::success;
74
75   m_mapMiCmdToCmdCreatorFn.clear();
76
77   m_bInitialized = false;
78
79   return MIstatus::success;
80 }
81
82 //++
83 //------------------------------------------------------------------------------------
84 // Details: Register a command's creator function with the command identifier
85 // the MI
86 //          command name i.e. 'file-exec-and-symbols'.
87 // Type:    Method.
88 // Args:    vMiCmd          - (R) Command's name, the MI command.
89 //          vCmdCreateFn    - (R) Command's creator function pointer.
90 // Return:  MIstatus::success - Functionality succeeded.
91 //          MIstatus::failure - Functionality failed.
92 // Throws:  None.
93 //--
94 bool CMICmdFactory::CmdRegister(const CMIUtilString &vMiCmd,
95                                 CmdCreatorFnPtr vCmdCreateFn) {
96   if (!IsValid(vMiCmd)) {
97     SetErrorDescription(CMIUtilString::Format(
98         MIRSRC(IDS_CMDFACTORY_ERR_INVALID_CMD_NAME), vMiCmd.c_str()));
99     return MIstatus::failure;
100   }
101   if (vCmdCreateFn == nullptr) {
102     SetErrorDescription(CMIUtilString::Format(
103         MIRSRC(IDS_CMDFACTORY_ERR_INVALID_CMD_CR8FN), vMiCmd.c_str()));
104     return MIstatus::failure;
105   }
106
107   if (HaveAlready(vMiCmd)) {
108     SetErrorDescription(CMIUtilString::Format(
109         MIRSRC(IDS_CMDFACTORY_ERR_CMD_ALREADY_REGED), vMiCmd.c_str()));
110     return MIstatus::failure;
111   }
112
113   MapPairMiCmdToCmdCreatorFn_t pr(vMiCmd, vCmdCreateFn);
114   m_mapMiCmdToCmdCreatorFn.insert(pr);
115
116   return MIstatus::success;
117 }
118
119 //++
120 //------------------------------------------------------------------------------------
121 // Details: Check a command is already registered.
122 // Type:    Method.
123 // Args:    vMiCmd  - (R) Command's name, the MI command.
124 // Return:  True - registered.
125 //          False - not found.
126 // Throws:  None.
127 //--
128 bool CMICmdFactory::HaveAlready(const CMIUtilString &vMiCmd) const {
129   const MapMiCmdToCmdCreatorFn_t::const_iterator it =
130       m_mapMiCmdToCmdCreatorFn.find(vMiCmd);
131   return it != m_mapMiCmdToCmdCreatorFn.end();
132 }
133
134 //++
135 //------------------------------------------------------------------------------------
136 // Details: Check a command's name is valid:
137 //              - name is not empty
138 //              - name does not have spaces
139 // Type:    Method.
140 // Args:    vMiCmd  - (R) Command's name, the MI command.
141 // Return:  True - valid.
142 //          False - not valid.
143 // Throws:  None.
144 //--
145 bool CMICmdFactory::IsValid(const CMIUtilString &vMiCmd) const {
146   bool bValid = true;
147
148   if (vMiCmd.empty()) {
149     bValid = false;
150     return false;
151   }
152
153   const size_t nPos = vMiCmd.find(' ');
154   if (nPos != std::string::npos)
155     bValid = false;
156
157   return bValid;
158 }
159
160 //++
161 //------------------------------------------------------------------------------------
162 // Details: Check a command is already registered.
163 // Type:    Method.
164 // Args:    vMiCmd  - (R) Command's name, the MI command.
165 // Return:  True - registered.
166 //          False - not found.
167 // Throws:  None.
168 //--
169 bool CMICmdFactory::CmdExist(const CMIUtilString &vMiCmd) const {
170   return HaveAlready(vMiCmd);
171 }
172
173 //++
174 //------------------------------------------------------------------------------------
175 // Details: Create a command given the specified MI command name. The command
176 // data object
177 //          contains the options for the command.
178 // Type:    Method.
179 // Args:    vMiCmd      - (R) Command's name, the MI command.
180 //          vCmdData    - (RW) Command's metadata status/information/result
181 //          object.
182 //          vpNewCmd    - (W) New command instance.
183 // Return:  MIstatus::success - Functionality succeeded.
184 //          MIstatus::failure - Functionality failed.
185 // Throws:  None.
186 //--
187 bool CMICmdFactory::CmdCreate(const CMIUtilString &vMiCmd,
188                               const SMICmdData &vCmdData,
189                               CMICmdBase *&vpNewCmd) {
190   vpNewCmd = nullptr;
191
192   if (!IsValid(vMiCmd)) {
193     SetErrorDescription(CMIUtilString::Format(
194         MIRSRC(IDS_CMDFACTORY_ERR_INVALID_CMD_NAME), vMiCmd.c_str()));
195     return MIstatus::failure;
196   }
197   if (!HaveAlready(vMiCmd)) {
198     SetErrorDescription(CMIUtilString::Format(
199         MIRSRC(IDS_CMDFACTORY_ERR_CMD_NOT_REGISTERED), vMiCmd.c_str()));
200     return MIstatus::failure;
201   }
202
203   const MapMiCmdToCmdCreatorFn_t::const_iterator it =
204       m_mapMiCmdToCmdCreatorFn.find(vMiCmd);
205   const CMIUtilString &rMiCmd((*it).first);
206   MIunused(rMiCmd);
207   CmdCreatorFnPtr pFn = (*it).second;
208   CMICmdBase *pCmd = (*pFn)();
209
210   SMICmdData cmdData(vCmdData);
211   cmdData.id = pCmd->GetGUID();
212   pCmd->SetCmdData(cmdData);
213   vpNewCmd = pCmd;
214
215   return MIstatus::success;
216 }