1 //===-- MICmdMgr.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 //===----------------------------------------------------------------------===//
13 // Overview: CMICmdMgr implementation.
15 // Environment: Compilers: Visual C++ 12.
16 // gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1
17 // Libraries: See MIReadmetxt.
24 #include "MICmnResources.h"
26 #include "MICmdInterpreter.h"
27 #include "MICmdFactory.h"
28 #include "MICmdInvoker.h"
29 #include "MICmdBase.h"
30 #include "MIUtilSingletonBase.h"
31 #include "MIUtilSingletonHelper.h"
33 //++ ------------------------------------------------------------------------------------
34 // Details: CMICmdMgr constructor.
40 CMICmdMgr::CMICmdMgr( void )
41 : m_interpretor( CMICmdInterpreter::Instance() )
42 , m_factory( CMICmdFactory::Instance() )
43 , m_invoker( CMICmdInvoker::Instance() )
47 //++ ------------------------------------------------------------------------------------
48 // Details: CMICmdMgr destructor.
54 CMICmdMgr::~CMICmdMgr( void )
59 //++ ------------------------------------------------------------------------------------
60 // Details: Initialize resources for *this Command Manager.
63 // Return: MIstatus::success - Functionality succeeded.
64 // MIstatus::failure - Functionality failed.
67 bool CMICmdMgr::Initialize( void )
69 m_clientUsageRefCnt++;
72 return MIstatus::success;
74 bool bOk = MIstatus::success;
77 // Note initialization order is important here as some resources depend on previous
78 MI::ModuleInit< CMICmnLog > ( IDS_MI_INIT_ERR_LOG , bOk, errMsg );
79 MI::ModuleInit< CMICmnResources >( IDS_MI_INIT_ERR_RESOURCES, bOk, errMsg );
80 if( bOk && !m_interpretor.Initialize() )
83 errMsg = CMIUtilString::Format( MIRSRC( IDS_MI_INIT_ERR_CMDINTERPRETER ), m_interpretor.GetErrorDescription().c_str() );
85 if( bOk && !m_factory.Initialize() )
88 errMsg = CMIUtilString::Format( MIRSRC( IDS_MI_INIT_ERR_CMDFACTORY ), m_factory.GetErrorDescription().c_str() );
90 if( bOk && !m_invoker.Initialize() )
93 errMsg = CMIUtilString::Format( MIRSRC( IDS_MI_INIT_ERR_CMDINVOKER ), m_invoker.GetErrorDescription().c_str() );
99 CMIUtilString strInitError( CMIUtilString::Format( MIRSRC( IDS_MI_INIT_ERR_CMDMGR ), errMsg.c_str() ) );
100 SetErrorDescription( strInitError );
101 return MIstatus::failure;
104 return MIstatus::success;
107 //++ ------------------------------------------------------------------------------------
108 // Details: Release resources for *this Command Manager.
111 // Return: MIstatus::success - Functionality succeeded.
112 // MIstatus::failure - Functionality failed.
115 bool CMICmdMgr::Shutdown( void )
117 if( --m_clientUsageRefCnt > 0 )
118 return MIstatus::success;
120 if( !m_bInitialized )
121 return MIstatus::success;
123 m_bInitialized = false;
125 ClrErrorDescription();
127 bool bOk = MIstatus::success;
128 CMIUtilString errMsg;
131 m_setCmdDeleteCallback.clear();
133 // Note shutdown order is important here
134 if( !m_invoker.Shutdown() )
137 errMsg += CMIUtilString::Format( MIRSRC( IDS_MI_SHTDWN_ERR_CMDINVOKER ), m_invoker.GetErrorDescription().c_str() );
139 if( !m_factory.Shutdown() )
142 if( !errMsg.empty() ) errMsg += ", ";
143 errMsg += CMIUtilString::Format( MIRSRC( IDS_MI_SHTDWN_ERR_CMDFACTORY ), m_factory.GetErrorDescription().c_str() );
145 if( !m_interpretor.Shutdown() )
148 if( !errMsg.empty() ) errMsg += ", ";
149 errMsg += CMIUtilString::Format( MIRSRC( IDS_MI_SHTDWN_ERR_CMDINTERPRETER ), m_interpretor.GetErrorDescription().c_str() );
151 MI::ModuleShutdown< CMICmnResources >( IDS_MI_INIT_ERR_RESOURCES, bOk, errMsg );
152 MI::ModuleShutdown< CMICmnLog >( IDS_MI_INIT_ERR_LOG , bOk, errMsg );
156 SetErrorDescriptionn( MIRSRC( IDS_MI_SHUTDOWN_ERR ), errMsg.c_str() );
159 return MIstatus::success;
162 //++ ------------------------------------------------------------------------------------
163 // Details: Establish whether the text data is an MI format type command.
165 // Args: vTextLine - (R) Text data to interpret.
166 // vwbYesValid - (W) True = MI type command, false = not recognised.
167 // vwbCmdNotInCmdFactor - (W) True = MI command not found in the command factor, false = recognised.
168 // Return: MIstatus::success - Functionality succeeded.
169 // MIstatus::failure - Functionality failed.
172 bool CMICmdMgr::CmdInterpret( const CMIUtilString & vTextLine, bool & vwbYesValid, bool & vwbCmdNotInCmdFactor, SMICmdData & rwCmdData )
174 return m_interpretor.ValidateIsMi( vTextLine, vwbYesValid, vwbCmdNotInCmdFactor, rwCmdData );
177 //++ ------------------------------------------------------------------------------------
178 // Details: Having previously had the potential command validated and found valid now
179 // get the command executed.
180 // If the Functionalityity returns MIstatus::failure call GetErrorDescription().
181 // This function is used by the application's main thread.
183 // Args: vCmdData - (RW) Command meta data.
184 // Return: MIstatus::success - Functionality succeeded.
185 // MIstatus::failure - Functionality failed.
188 bool CMICmdMgr::CmdExecute( const SMICmdData & vCmdData )
190 bool bOk = MIstatus::success;
192 // Pass the command's meta data structure to the command
193 // so it can update it if required. (Need to copy it out of the
194 // command before the command is deleted)
195 CMICmdBase * pCmd = nullptr;
196 bOk = m_factory.CmdCreate( vCmdData.strMiCmd, vCmdData, pCmd );
199 const CMIUtilString errMsg( CMIUtilString::Format( MIRSRC( IDS_CMDMGR_ERR_CMD_FAILED_CREATE ), m_factory.GetErrorDescription().c_str() ) );
200 SetErrorDescription( errMsg );
201 return MIstatus::failure;
204 bOk = m_invoker.CmdExecute( *pCmd );
207 const CMIUtilString errMsg( CMIUtilString::Format( MIRSRC( IDS_CMDMGR_ERR_CMD_INVOKER ), m_invoker.GetErrorDescription().c_str() ) );
208 SetErrorDescription( errMsg );
209 return MIstatus::failure;
215 //++ ------------------------------------------------------------------------------------
216 // Details: Iterate all interested clients and tell them a command is being deleted.
218 // Args: vCmdData - (RW) The command to be deleted.
219 // Return: MIstatus::success - Functional succeeded.
220 // MIstatus::failure - Functional failed.
223 bool CMICmdMgr::CmdDelete( SMICmdData vCmdData )
225 // Note vCmdData is a copy! The command holding its copy will be deleted soon
226 // we still need to iterate callback clients after a command object is deleted
228 m_setCmdDeleteCallback.Delete( vCmdData );
230 return MIstatus::success;
233 //++ ------------------------------------------------------------------------------------
234 // Details: Register an object to be called when a command object is deleted.
236 // Args: vObject - (R) A new interested client.
237 // Return: MIstatus::success - Functional succeeded.
238 // MIstatus::failure - Functional failed.
241 bool CMICmdMgr::CmdRegisterForDeleteNotification( CMICmdMgrSetCmdDeleteCallback::ICallback & vObject )
243 return m_setCmdDeleteCallback.Register( vObject );
246 //++ ------------------------------------------------------------------------------------
247 // Details: Unregister an object from being called when a command object is deleted.
249 // Args: vObject - (R) The was interested client.
250 // Return: MIstatus::success - Functional succeeded.
251 // MIstatus::failure - Functional failed.
254 bool CMICmdMgr::CmdUnregisterForDeleteNotification( CMICmdMgrSetCmdDeleteCallback::ICallback & vObject )
256 return m_setCmdDeleteCallback.Unregister( vObject );