1 //===-- MICmdFactory.cpp ----------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 #include "MICmdFactory.h"
11 #include "MICmdBase.h"
12 #include "MICmdCommands.h"
13 #include "MICmdData.h"
14 #include "MICmnResources.h"
17 // Details: CMICmdFactory constructor.
23 CMICmdFactory::CMICmdFactory() {}
26 // Details: CMICmdFactory destructor.
32 CMICmdFactory::~CMICmdFactory() { Shutdown(); }
35 // Details: Initialize resources for *this Command factory.
38 // Return: MIstatus::success - Functionality succeeded.
39 // MIstatus::failure - Functionality failed.
42 bool CMICmdFactory::Initialize() {
43 m_clientUsageRefCnt++;
46 return MIstatus::success;
48 m_bInitialized = true;
50 MICmnCommands::RegisterAll();
52 return MIstatus::success;
56 // Details: Release resources for *this Command Factory.
59 // Return: MIstatus::success - Functionality succeeded.
60 // MIstatus::failure - Functionality failed.
63 bool CMICmdFactory::Shutdown() {
64 if (--m_clientUsageRefCnt > 0)
65 return MIstatus::success;
68 return MIstatus::success;
70 m_mapMiCmdToCmdCreatorFn.clear();
72 m_bInitialized = false;
74 return MIstatus::success;
78 // Details: Register a command's creator function with the command identifier
80 // command name i.e. 'file-exec-and-symbols'.
82 // Args: vMiCmd - (R) Command's name, the MI command.
83 // vCmdCreateFn - (R) Command's creator function pointer.
84 // Return: MIstatus::success - Functionality succeeded.
85 // MIstatus::failure - Functionality failed.
88 bool CMICmdFactory::CmdRegister(const CMIUtilString &vMiCmd,
89 CmdCreatorFnPtr vCmdCreateFn) {
90 if (!IsValid(vMiCmd)) {
91 SetErrorDescription(CMIUtilString::Format(
92 MIRSRC(IDS_CMDFACTORY_ERR_INVALID_CMD_NAME), vMiCmd.c_str()));
93 return MIstatus::failure;
95 if (vCmdCreateFn == nullptr) {
96 SetErrorDescription(CMIUtilString::Format(
97 MIRSRC(IDS_CMDFACTORY_ERR_INVALID_CMD_CR8FN), vMiCmd.c_str()));
98 return MIstatus::failure;
101 if (HaveAlready(vMiCmd)) {
102 SetErrorDescription(CMIUtilString::Format(
103 MIRSRC(IDS_CMDFACTORY_ERR_CMD_ALREADY_REGED), vMiCmd.c_str()));
104 return MIstatus::failure;
107 MapPairMiCmdToCmdCreatorFn_t pr(vMiCmd, vCmdCreateFn);
108 m_mapMiCmdToCmdCreatorFn.insert(pr);
110 return MIstatus::success;
114 // Details: Check a command is already registered.
116 // Args: vMiCmd - (R) Command's name, the MI command.
117 // Return: True - registered.
118 // False - not found.
121 bool CMICmdFactory::HaveAlready(const CMIUtilString &vMiCmd) const {
122 const MapMiCmdToCmdCreatorFn_t::const_iterator it =
123 m_mapMiCmdToCmdCreatorFn.find(vMiCmd);
124 return it != m_mapMiCmdToCmdCreatorFn.end();
128 // Details: Check a command's name is valid:
129 // - name is not empty
130 // - name does not have spaces
132 // Args: vMiCmd - (R) Command's name, the MI command.
133 // Return: True - valid.
134 // False - not valid.
137 bool CMICmdFactory::IsValid(const CMIUtilString &vMiCmd) const {
140 if (vMiCmd.empty()) {
145 const size_t nPos = vMiCmd.find(' ');
146 if (nPos != std::string::npos)
153 // Details: Check a command is already registered.
155 // Args: vMiCmd - (R) Command's name, the MI command.
156 // Return: True - registered.
157 // False - not found.
160 bool CMICmdFactory::CmdExist(const CMIUtilString &vMiCmd) const {
161 return HaveAlready(vMiCmd);
165 // Details: Create a command given the specified MI command name. The command
167 // contains the options for the command.
169 // Args: vMiCmd - (R) Command's name, the MI command.
170 // vCmdData - (RW) Command's metadata status/information/result
172 // vpNewCmd - (W) New command instance.
173 // Return: MIstatus::success - Functionality succeeded.
174 // MIstatus::failure - Functionality failed.
177 bool CMICmdFactory::CmdCreate(const CMIUtilString &vMiCmd,
178 const SMICmdData &vCmdData,
179 CMICmdBase *&vpNewCmd) {
182 if (!IsValid(vMiCmd)) {
183 SetErrorDescription(CMIUtilString::Format(
184 MIRSRC(IDS_CMDFACTORY_ERR_INVALID_CMD_NAME), vMiCmd.c_str()));
185 return MIstatus::failure;
187 if (!HaveAlready(vMiCmd)) {
188 SetErrorDescription(CMIUtilString::Format(
189 MIRSRC(IDS_CMDFACTORY_ERR_CMD_NOT_REGISTERED), vMiCmd.c_str()));
190 return MIstatus::failure;
193 const MapMiCmdToCmdCreatorFn_t::const_iterator it =
194 m_mapMiCmdToCmdCreatorFn.find(vMiCmd);
195 const CMIUtilString &rMiCmd((*it).first);
197 CmdCreatorFnPtr pFn = (*it).second;
198 CMICmdBase *pCmd = (*pFn)();
200 SMICmdData cmdData(vCmdData);
201 cmdData.id = pCmd->GetGUID();
202 pCmd->SetCmdData(cmdData);
205 return MIstatus::success;