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 // File: MICmdFactory.cpp
13 // Overview: CMICmdFactory implementation.
15 // Environment: Compilers: Visual C++ 12.
16 // gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1
17 // Libraries: See MIReadmetxt.
23 #include "MICmdFactory.h"
24 #include "MICmnResources.h"
25 #include "MICmdData.h"
26 #include "MICmdBase.h"
27 #include "MICmdCommands.h"
29 //++ ------------------------------------------------------------------------------------
30 // Details: CMICmdFactory constructor.
36 CMICmdFactory::CMICmdFactory( void )
40 //++ ------------------------------------------------------------------------------------
41 // Details: CMICmdFactory destructor.
47 CMICmdFactory::~CMICmdFactory( void )
52 //++ ------------------------------------------------------------------------------------
53 // Details: Initialize resources for *this Command factory.
56 // Return: MIstatus::success - Functionality succeeded.
57 // MIstatus::failure - Functionality failed.
60 bool CMICmdFactory::Initialize( void )
62 m_clientUsageRefCnt++;
65 return MIstatus::success;
67 m_bInitialized = true;
69 MICmnCommands::RegisterAll();
71 return MIstatus::success;
74 //++ ------------------------------------------------------------------------------------
75 // Details: Release resources for *this Command Factory.
78 // Return: MIstatus::success - Functionality succeeded.
79 // MIstatus::failure - Functionality failed.
82 bool CMICmdFactory::Shutdown( void )
84 if( --m_clientUsageRefCnt > 0 )
85 return MIstatus::success;
88 return MIstatus::success;
90 m_mapMiCmdToCmdCreatorFn.clear();
92 m_bInitialized = false;
94 return MIstatus::success;
97 //++ ------------------------------------------------------------------------------------
98 // Details: Register a command's creator function with the command identitier the MI
99 // command name i.e. 'file-exec-and-symbols'.
101 // Args: vMiCmd - (R) Command's name, the MI command.
102 // vCmdCreateFn - (R) Command's creator function pointer.
103 // Return: MIstatus::success - Functionality succeeded.
104 // MIstatus::failure - Functionality failed.
107 bool CMICmdFactory::CmdRegister( const CMIUtilString & vMiCmd, CmdCreatorFnPtr vCmdCreateFn )
109 if( !IsValid( vMiCmd ) )
111 SetErrorDescription( CMIUtilString::Format( MIRSRC( IDS_CMDFACTORY_ERR_INVALID_CMD_NAME ), vMiCmd.c_str() ) );
112 return MIstatus::failure;
114 if( vCmdCreateFn == nullptr )
116 SetErrorDescription( CMIUtilString::Format( MIRSRC( IDS_CMDFACTORY_ERR_INVALID_CMD_CR8FN ), vMiCmd.c_str() ) );
117 return MIstatus::failure;
120 if( HaveAlready( vMiCmd ) )
122 SetErrorDescription( CMIUtilString::Format( MIRSRC( IDS_CMDFACTORY_ERR_CMD_ALREADY_REGED ), vMiCmd.c_str() ) );
123 return MIstatus::failure;
126 MapPairMiCmdToCmdCreatorFn_t pr( vMiCmd, vCmdCreateFn );
127 m_mapMiCmdToCmdCreatorFn.insert( pr );
129 return MIstatus::success;
132 //++ ------------------------------------------------------------------------------------
133 // Details: Check a command is already registered.
135 // Args: vMiCmd - (R) Command's name, the MI command.
136 // Return: True - registered.
137 // False - not found.
140 bool CMICmdFactory::HaveAlready( const CMIUtilString & vMiCmd ) const
142 const MapMiCmdToCmdCreatorFn_t::const_iterator it = m_mapMiCmdToCmdCreatorFn.find( vMiCmd );
143 if( it != m_mapMiCmdToCmdCreatorFn.end() )
149 //++ ------------------------------------------------------------------------------------
150 // Details: Check a command's name is valid:
151 // - name is not empty
152 // - name does not have spaces
154 // Args: vMiCmd - (R) Command's name, the MI command.
155 // Return: True - valid.
156 // False - not valid.
159 bool CMICmdFactory::IsValid( const CMIUtilString & vMiCmd ) const
169 const MIint nPos = vMiCmd.find( " " );
170 if( nPos != (MIint) std::string::npos )
176 //++ ------------------------------------------------------------------------------------
177 // Details: Check a command is already registered.
179 // Args: vMiCmd - (R) Command's name, the MI command.
180 // Return: True - registered.
181 // False - not found.
184 bool CMICmdFactory::CmdExist( const CMIUtilString & vMiCmd ) const
186 return HaveAlready( vMiCmd );
189 //++ ------------------------------------------------------------------------------------
190 // Details: Create a command given the specified MI command name. The command data object
191 // contains the options for the command.
193 // Args: vMiCmd - (R) Command's name, the MI command.
194 // vCmdData - (RW) Command's metadata status/information/result object.
195 // vpNewCmd - (W) New command instance.
196 // Return: MIstatus::success - Functionality succeeded.
197 // MIstatus::failure - Functionality failed.
200 bool CMICmdFactory::CmdCreate( const CMIUtilString & vMiCmd, const SMICmdData & vCmdData, CMICmdBase *& vpNewCmd )
202 bool bOk = MIstatus::success;
206 if( !IsValid( vMiCmd ) )
208 SetErrorDescription( CMIUtilString::Format( MIRSRC( IDS_CMDFACTORY_ERR_INVALID_CMD_NAME ), vMiCmd.c_str() ) );
209 return MIstatus::failure;
211 if( !HaveAlready( vMiCmd ) )
213 SetErrorDescription( CMIUtilString::Format( MIRSRC( IDS_CMDFACTORY_ERR_CMD_NOT_REGISTERED ), vMiCmd.c_str() ) );
214 return MIstatus::failure;
217 const MapMiCmdToCmdCreatorFn_t::const_iterator it = m_mapMiCmdToCmdCreatorFn.find( vMiCmd );
218 const CMIUtilString & rMiCmd( (*it).first ); MIunused( rMiCmd );
219 CmdCreatorFnPtr pFn = (*it).second;
220 CMICmdBase * pCmd = (*pFn)();
222 SMICmdData cmdData( vCmdData );
223 cmdData.id = pCmd->GetGUID();
224 bOk = pCmd->SetCmdData( cmdData );