1 //===-- MICmdMgr.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 //===----------------------------------------------------------------------===//
11 #include "MICmdBase.h"
12 #include "MICmdFactory.h"
13 #include "MICmdInterpreter.h"
14 #include "MICmdInvoker.h"
16 #include "MICmnResources.h"
17 #include "MIUtilSingletonBase.h"
18 #include "MIUtilSingletonHelper.h"
21 // Details: CMICmdMgr constructor.
27 CMICmdMgr::CMICmdMgr()
28 : m_interpretor(CMICmdInterpreter::Instance()),
29 m_factory(CMICmdFactory::Instance()),
30 m_invoker(CMICmdInvoker::Instance()) {}
33 // Details: CMICmdMgr destructor.
39 CMICmdMgr::~CMICmdMgr() { Shutdown(); }
42 // Details: Initialize resources for *this Command Manager.
45 // Return: MIstatus::success - Functionality succeeded.
46 // MIstatus::failure - Functionality failed.
49 bool CMICmdMgr::Initialize() {
50 m_clientUsageRefCnt++;
53 return MIstatus::success;
55 bool bOk = MIstatus::success;
58 // Note initialization order is important here as some resources depend on
60 MI::ModuleInit<CMICmnLog>(IDS_MI_INIT_ERR_LOG, bOk, errMsg);
61 MI::ModuleInit<CMICmnResources>(IDS_MI_INIT_ERR_RESOURCES, bOk, errMsg);
62 if (bOk && !m_interpretor.Initialize()) {
64 errMsg = CMIUtilString::Format(MIRSRC(IDS_MI_INIT_ERR_CMDINTERPRETER),
65 m_interpretor.GetErrorDescription().c_str());
67 if (bOk && !m_factory.Initialize()) {
69 errMsg = CMIUtilString::Format(MIRSRC(IDS_MI_INIT_ERR_CMDFACTORY),
70 m_factory.GetErrorDescription().c_str());
72 if (bOk && !m_invoker.Initialize()) {
74 errMsg = CMIUtilString::Format(MIRSRC(IDS_MI_INIT_ERR_CMDINVOKER),
75 m_invoker.GetErrorDescription().c_str());
80 CMIUtilString strInitError(
81 CMIUtilString::Format(MIRSRC(IDS_MI_INIT_ERR_CMDMGR), errMsg.c_str()));
82 SetErrorDescription(strInitError);
83 return MIstatus::failure;
86 return MIstatus::success;
90 // Details: Release resources for *this Command Manager.
93 // Return: MIstatus::success - Functionality succeeded.
94 // MIstatus::failure - Functionality failed.
97 bool CMICmdMgr::Shutdown() {
98 if (--m_clientUsageRefCnt > 0)
99 return MIstatus::success;
102 return MIstatus::success;
104 m_bInitialized = false;
106 ClrErrorDescription();
108 bool bOk = MIstatus::success;
109 CMIUtilString errMsg;
112 m_setCmdDeleteCallback.clear();
114 // Note shutdown order is important here
115 if (!m_invoker.Shutdown()) {
117 errMsg += CMIUtilString::Format(MIRSRC(IDS_MI_SHTDWN_ERR_CMDINVOKER),
118 m_invoker.GetErrorDescription().c_str());
120 if (!m_factory.Shutdown()) {
124 errMsg += CMIUtilString::Format(MIRSRC(IDS_MI_SHTDWN_ERR_CMDFACTORY),
125 m_factory.GetErrorDescription().c_str());
127 if (!m_interpretor.Shutdown()) {
132 CMIUtilString::Format(MIRSRC(IDS_MI_SHTDWN_ERR_CMDINTERPRETER),
133 m_interpretor.GetErrorDescription().c_str());
135 MI::ModuleShutdown<CMICmnResources>(IDS_MI_INIT_ERR_RESOURCES, bOk, errMsg);
136 MI::ModuleShutdown<CMICmnLog>(IDS_MI_INIT_ERR_LOG, bOk, errMsg);
139 SetErrorDescriptionn(MIRSRC(IDS_MI_SHUTDOWN_ERR), errMsg.c_str());
142 return MIstatus::success;
146 // Details: Establish whether the text data is an MI format type command.
148 // Args: vTextLine - (R) Text data to interpret.
149 // vwbYesValid - (W) True = MI type command, false = not
151 // vwbCmdNotInCmdFactor - (W) True = MI command not found in the
152 // command factor, false = recognised.
153 // Return: MIstatus::success - Functionality succeeded.
154 // MIstatus::failure - Functionality failed.
157 bool CMICmdMgr::CmdInterpret(const CMIUtilString &vTextLine, bool &vwbYesValid,
158 bool &vwbCmdNotInCmdFactor,
159 SMICmdData &rwCmdData) {
160 return m_interpretor.ValidateIsMi(vTextLine, vwbYesValid,
161 vwbCmdNotInCmdFactor, rwCmdData);
165 // Details: Having previously had the potential command validated and found
167 // get the command executed.
168 // If the Functionality returns MIstatus::failure call
169 // GetErrorDescription().
170 // This function is used by the application's main thread.
172 // Args: vCmdData - (RW) Command meta data.
173 // Return: MIstatus::success - Functionality succeeded.
174 // MIstatus::failure - Functionality failed.
177 bool CMICmdMgr::CmdExecute(const SMICmdData &vCmdData) {
178 bool bOk = MIstatus::success;
180 // Pass the command's meta data structure to the command
181 // so it can update it if required. (Need to copy it out of the
182 // command before the command is deleted)
183 CMICmdBase *pCmd = nullptr;
184 bOk = m_factory.CmdCreate(vCmdData.strMiCmd, vCmdData, pCmd);
186 const CMIUtilString errMsg(
187 CMIUtilString::Format(MIRSRC(IDS_CMDMGR_ERR_CMD_FAILED_CREATE),
188 m_factory.GetErrorDescription().c_str()));
189 SetErrorDescription(errMsg);
190 return MIstatus::failure;
193 bOk = m_invoker.CmdExecute(*pCmd);
195 const CMIUtilString errMsg(
196 CMIUtilString::Format(MIRSRC(IDS_CMDMGR_ERR_CMD_INVOKER),
197 m_invoker.GetErrorDescription().c_str()));
198 SetErrorDescription(errMsg);
199 return MIstatus::failure;
206 // Details: Iterate all interested clients and tell them a command is being
209 // Args: vCmdData - (RW) The command to be deleted.
210 // Return: MIstatus::success - Functional succeeded.
211 // MIstatus::failure - Functional failed.
214 bool CMICmdMgr::CmdDelete(SMICmdData vCmdData) {
215 // Note vCmdData is a copy! The command holding its copy will be deleted soon
216 // we still need to iterate callback clients after a command object is deleted
218 m_setCmdDeleteCallback.Delete(vCmdData);
220 return MIstatus::success;
224 // Details: Register an object to be called when a command object is deleted.
226 // Args: vObject - (R) A new interested client.
227 // Return: MIstatus::success - Functional succeeded.
228 // MIstatus::failure - Functional failed.
231 bool CMICmdMgr::CmdRegisterForDeleteNotification(
232 CMICmdMgrSetCmdDeleteCallback::ICallback &vObject) {
233 return m_setCmdDeleteCallback.Register(vObject);
237 // Details: Unregister an object from being called when a command object is
240 // Args: vObject - (R) The was interested client.
241 // Return: MIstatus::success - Functional succeeded.
242 // MIstatus::failure - Functional failed.
245 bool CMICmdMgr::CmdUnregisterForDeleteNotification(
246 CMICmdMgrSetCmdDeleteCallback::ICallback &vObject) {
247 return m_setCmdDeleteCallback.Unregister(vObject);