]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/tools/lldb-mi/MICmdFactory.cpp
Merge ^/head r337646 through r338014.
[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   if (it != m_mapMiCmdToCmdCreatorFn.end())
132     return true;
133
134   return false;
135 }
136
137 //++
138 //------------------------------------------------------------------------------------
139 // Details: Check a command's name is valid:
140 //              - name is not empty
141 //              - name does not have spaces
142 // Type:    Method.
143 // Args:    vMiCmd  - (R) Command's name, the MI command.
144 // Return:  True - valid.
145 //          False - not valid.
146 // Throws:  None.
147 //--
148 bool CMICmdFactory::IsValid(const CMIUtilString &vMiCmd) const {
149   bool bValid = true;
150
151   if (vMiCmd.empty()) {
152     bValid = false;
153     return false;
154   }
155
156   const size_t nPos = vMiCmd.find(' ');
157   if (nPos != std::string::npos)
158     bValid = false;
159
160   return bValid;
161 }
162
163 //++
164 //------------------------------------------------------------------------------------
165 // Details: Check a command is already registered.
166 // Type:    Method.
167 // Args:    vMiCmd  - (R) Command's name, the MI command.
168 // Return:  True - registered.
169 //          False - not found.
170 // Throws:  None.
171 //--
172 bool CMICmdFactory::CmdExist(const CMIUtilString &vMiCmd) const {
173   return HaveAlready(vMiCmd);
174 }
175
176 //++
177 //------------------------------------------------------------------------------------
178 // Details: Create a command given the specified MI command name. The command
179 // data object
180 //          contains the options for the command.
181 // Type:    Method.
182 // Args:    vMiCmd      - (R) Command's name, the MI command.
183 //          vCmdData    - (RW) Command's metadata status/information/result
184 //          object.
185 //          vpNewCmd    - (W) New command instance.
186 // Return:  MIstatus::success - Functionality succeeded.
187 //          MIstatus::failure - Functionality failed.
188 // Throws:  None.
189 //--
190 bool CMICmdFactory::CmdCreate(const CMIUtilString &vMiCmd,
191                               const SMICmdData &vCmdData,
192                               CMICmdBase *&vpNewCmd) {
193   vpNewCmd = nullptr;
194
195   if (!IsValid(vMiCmd)) {
196     SetErrorDescription(CMIUtilString::Format(
197         MIRSRC(IDS_CMDFACTORY_ERR_INVALID_CMD_NAME), vMiCmd.c_str()));
198     return MIstatus::failure;
199   }
200   if (!HaveAlready(vMiCmd)) {
201     SetErrorDescription(CMIUtilString::Format(
202         MIRSRC(IDS_CMDFACTORY_ERR_CMD_NOT_REGISTERED), vMiCmd.c_str()));
203     return MIstatus::failure;
204   }
205
206   const MapMiCmdToCmdCreatorFn_t::const_iterator it =
207       m_mapMiCmdToCmdCreatorFn.find(vMiCmd);
208   const CMIUtilString &rMiCmd((*it).first);
209   MIunused(rMiCmd);
210   CmdCreatorFnPtr pFn = (*it).second;
211   CMICmdBase *pCmd = (*pFn)();
212
213   SMICmdData cmdData(vCmdData);
214   cmdData.id = pCmd->GetGUID();
215   pCmd->SetCmdData(cmdData);
216   vpNewCmd = pCmd;
217
218   return MIstatus::success;
219 }