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