1 //===-- MICmdFactory.cpp ----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 #include "MICmdFactory.h"
12 #include "MICmdBase.h"
13 #include "MICmdCommands.h"
14 #include "MICmdData.h"
15 #include "MICmnResources.h"
18 //------------------------------------------------------------------------------------
19 // Details: CMICmdFactory constructor.
25 CMICmdFactory::CMICmdFactory() {}
28 //------------------------------------------------------------------------------------
29 // Details: CMICmdFactory destructor.
35 CMICmdFactory::~CMICmdFactory() { Shutdown(); }
38 //------------------------------------------------------------------------------------
39 // Details: Initialize resources for *this Command factory.
42 // Return: MIstatus::success - Functionality succeeded.
43 // MIstatus::failure - Functionality failed.
46 bool CMICmdFactory::Initialize() {
47 m_clientUsageRefCnt++;
50 return MIstatus::success;
52 m_bInitialized = true;
54 MICmnCommands::RegisterAll();
56 return MIstatus::success;
60 //------------------------------------------------------------------------------------
61 // Details: Release resources for *this Command Factory.
64 // Return: MIstatus::success - Functionality succeeded.
65 // MIstatus::failure - Functionality failed.
68 bool CMICmdFactory::Shutdown() {
69 if (--m_clientUsageRefCnt > 0)
70 return MIstatus::success;
73 return MIstatus::success;
75 m_mapMiCmdToCmdCreatorFn.clear();
77 m_bInitialized = false;
79 return MIstatus::success;
83 //------------------------------------------------------------------------------------
84 // Details: Register a command's creator function with the command identifier
86 // command name i.e. 'file-exec-and-symbols'.
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.
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;
101 if (vCmdCreateFn == nullptr) {
102 SetErrorDescription(CMIUtilString::Format(
103 MIRSRC(IDS_CMDFACTORY_ERR_INVALID_CMD_CR8FN), vMiCmd.c_str()));
104 return MIstatus::failure;
107 if (HaveAlready(vMiCmd)) {
108 SetErrorDescription(CMIUtilString::Format(
109 MIRSRC(IDS_CMDFACTORY_ERR_CMD_ALREADY_REGED), vMiCmd.c_str()));
110 return MIstatus::failure;
113 MapPairMiCmdToCmdCreatorFn_t pr(vMiCmd, vCmdCreateFn);
114 m_mapMiCmdToCmdCreatorFn.insert(pr);
116 return MIstatus::success;
120 //------------------------------------------------------------------------------------
121 // Details: Check a command is already registered.
123 // Args: vMiCmd - (R) Command's name, the MI command.
124 // Return: True - registered.
125 // False - not found.
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();
135 //------------------------------------------------------------------------------------
136 // Details: Check a command's name is valid:
137 // - name is not empty
138 // - name does not have spaces
140 // Args: vMiCmd - (R) Command's name, the MI command.
141 // Return: True - valid.
142 // False - not valid.
145 bool CMICmdFactory::IsValid(const CMIUtilString &vMiCmd) const {
148 if (vMiCmd.empty()) {
153 const size_t nPos = vMiCmd.find(' ');
154 if (nPos != std::string::npos)
161 //------------------------------------------------------------------------------------
162 // Details: Check a command is already registered.
164 // Args: vMiCmd - (R) Command's name, the MI command.
165 // Return: True - registered.
166 // False - not found.
169 bool CMICmdFactory::CmdExist(const CMIUtilString &vMiCmd) const {
170 return HaveAlready(vMiCmd);
174 //------------------------------------------------------------------------------------
175 // Details: Create a command given the specified MI command name. The command
177 // contains the options for the command.
179 // Args: vMiCmd - (R) Command's name, the MI command.
180 // vCmdData - (RW) Command's metadata status/information/result
182 // vpNewCmd - (W) New command instance.
183 // Return: MIstatus::success - Functionality succeeded.
184 // MIstatus::failure - Functionality failed.
187 bool CMICmdFactory::CmdCreate(const CMIUtilString &vMiCmd,
188 const SMICmdData &vCmdData,
189 CMICmdBase *&vpNewCmd) {
192 if (!IsValid(vMiCmd)) {
193 SetErrorDescription(CMIUtilString::Format(
194 MIRSRC(IDS_CMDFACTORY_ERR_INVALID_CMD_NAME), vMiCmd.c_str()));
195 return MIstatus::failure;
197 if (!HaveAlready(vMiCmd)) {
198 SetErrorDescription(CMIUtilString::Format(
199 MIRSRC(IDS_CMDFACTORY_ERR_CMD_NOT_REGISTERED), vMiCmd.c_str()));
200 return MIstatus::failure;
203 const MapMiCmdToCmdCreatorFn_t::const_iterator it =
204 m_mapMiCmdToCmdCreatorFn.find(vMiCmd);
205 const CMIUtilString &rMiCmd((*it).first);
207 CmdCreatorFnPtr pFn = (*it).second;
208 CMICmdBase *pCmd = (*pFn)();
210 SMICmdData cmdData(vCmdData);
211 cmdData.id = pCmd->GetGUID();
212 pCmd->SetCmdData(cmdData);
215 return MIstatus::success;