]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/tools/lldb-mi/MICmdFactory.cpp
Reintegrate head revisions r273096-r277147
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / tools / lldb-mi / MICmdFactory.cpp
1 //===-- MICmdFactory.cpp ----------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 //++
11 // File:                MICmdFactory.cpp
12 //
13 // Overview:    CMICmdFactory implementation.
14 //
15 // Environment: Compilers:      Visual C++ 12.
16 //                                                      gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1
17 //                              Libraries:      See MIReadmetxt. 
18 //
19 // Copyright:   None.
20 //--
21
22 // In-house headers:
23 #include "MICmdFactory.h"
24 #include "MICmnResources.h"
25 #include "MICmdData.h"
26 #include "MICmdBase.h"
27 #include "MICmdCommands.h"
28
29 //++ ------------------------------------------------------------------------------------
30 // Details:     CMICmdFactory constructor.
31 // Type:        Method.
32 // Args:        None.
33 // Return:      None.
34 // Throws:      None.
35 //--
36 CMICmdFactory::CMICmdFactory( void )
37 {
38 }
39
40 //++ ------------------------------------------------------------------------------------
41 // Details:     CMICmdFactory destructor.
42 // Type:        Overridable.
43 // Args:        None.
44 // Return:      None.
45 // Throws:      None.
46 //--
47 CMICmdFactory::~CMICmdFactory( void )
48 {
49         Shutdown();
50 }
51
52 //++ ------------------------------------------------------------------------------------
53 // Details:     Initialize resources for *this Command factory.
54 // Type:        Method.
55 // Args:        None.
56 // Return:      MIstatus::success - Functionality succeeded.
57 //                      MIstatus::failure - Functionality failed.
58 // Throws:      None.
59 //--
60 bool CMICmdFactory::Initialize( void )
61 {
62         m_clientUsageRefCnt++;
63
64         if( m_bInitialized )
65                 return MIstatus::success;
66
67         m_bInitialized = true;
68
69         MICmnCommands::RegisterAll();
70
71         return  MIstatus::success;
72 }
73
74 //++ ------------------------------------------------------------------------------------
75 // Details:     Release resources for *this Command Factory.
76 // Type:        Method.
77 // Args:        None.
78 // Return:      MIstatus::success - Functionality succeeded.
79 //                      MIstatus::failure - Functionality failed.
80 // Throws:      None.
81 //--
82 bool CMICmdFactory::Shutdown( void )
83 {
84         if( --m_clientUsageRefCnt > 0 )
85                 return MIstatus::success;
86         
87         if( !m_bInitialized )
88                 return MIstatus::success;
89
90         m_mapMiCmdToCmdCreatorFn.clear();
91
92         m_bInitialized = false;
93
94         return MIstatus::success;
95 }       
96
97 //++ ------------------------------------------------------------------------------------
98 // Details:     Register a command's creator function with the command identitier the MI
99 //                      command name i.e. 'file-exec-and-symbols'.
100 // Type:        Method.
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.
105 // Throws:      None.
106 //--
107 bool CMICmdFactory::CmdRegister( const CMIUtilString & vMiCmd, CmdCreatorFnPtr vCmdCreateFn )
108 {
109         if( !IsValid( vMiCmd ) )
110         {
111                 SetErrorDescription( CMIUtilString::Format( MIRSRC( IDS_CMDFACTORY_ERR_INVALID_CMD_NAME ), vMiCmd.c_str() ) );
112                 return MIstatus::failure;
113         }
114         if( vCmdCreateFn == nullptr )
115         {
116                 SetErrorDescription( CMIUtilString::Format( MIRSRC( IDS_CMDFACTORY_ERR_INVALID_CMD_CR8FN ), vMiCmd.c_str() ) );
117                 return MIstatus::failure;
118         }
119
120         if( HaveAlready( vMiCmd ) )
121         {
122                 SetErrorDescription( CMIUtilString::Format( MIRSRC( IDS_CMDFACTORY_ERR_CMD_ALREADY_REGED ), vMiCmd.c_str() ) );
123                 return MIstatus::failure;
124         }
125
126         MapPairMiCmdToCmdCreatorFn_t pr( vMiCmd, vCmdCreateFn );
127         m_mapMiCmdToCmdCreatorFn.insert( pr );
128
129         return MIstatus::success;
130 }
131
132 //++ ------------------------------------------------------------------------------------
133 // Details:     Check a command is already registered.
134 // Type:        Method.
135 // Args:        vMiCmd  - (R) Command's name, the MI command.
136 // Return:      True - registered.
137 //                      False - not found.
138 // Throws:      None.
139 //--
140 bool CMICmdFactory::HaveAlready( const CMIUtilString & vMiCmd ) const
141 {
142         const MapMiCmdToCmdCreatorFn_t::const_iterator it = m_mapMiCmdToCmdCreatorFn.find( vMiCmd );
143         if( it != m_mapMiCmdToCmdCreatorFn.end() )
144                 return true;
145         
146         return false;
147 }
148
149 //++ ------------------------------------------------------------------------------------
150 // Details:     Check a command's name is valid:
151 //                              - name is not empty
152 //                              - name does not have spaces
153 // Type:        Method.
154 // Args:        vMiCmd  - (R) Command's name, the MI command.
155 // Return:      True - valid.
156 //                      False - not valid.
157 // Throws:      None.
158 //--
159 bool CMICmdFactory::IsValid( const CMIUtilString & vMiCmd ) const
160 {
161         bool bValid = true;
162
163         if( vMiCmd.empty() )
164         {
165                 bValid = false;
166                 return false;
167         }
168
169         const MIint nPos = vMiCmd.find( " " );
170         if( nPos != (MIint) std::string::npos )
171                 bValid = false;
172         
173         return bValid;
174 }
175
176 //++ ------------------------------------------------------------------------------------
177 // Details:     Check a command is already registered.
178 // Type:        Method.
179 // Args:        vMiCmd  - (R) Command's name, the MI command.
180 // Return:      True - registered.
181 //                      False - not found.
182 // Throws:      None.
183 //--
184 bool CMICmdFactory::CmdExist( const CMIUtilString & vMiCmd ) const
185 {
186         return HaveAlready( vMiCmd );
187 }
188
189 //++ ------------------------------------------------------------------------------------
190 // Details:     Create a command given the specified MI command name. The command data object
191 //                      contains the options for the command.
192 // Type:        Method.
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.
198 // Throws:      None.
199 //--
200 bool CMICmdFactory::CmdCreate( const CMIUtilString & vMiCmd, const SMICmdData & vCmdData, CMICmdBase *& vpNewCmd )
201 {
202         bool bOk = MIstatus::success;
203
204         vpNewCmd = nullptr;
205
206         if( !IsValid( vMiCmd ) )
207         {
208                 SetErrorDescription( CMIUtilString::Format( MIRSRC( IDS_CMDFACTORY_ERR_INVALID_CMD_NAME ), vMiCmd.c_str() ) );
209                 return MIstatus::failure;
210         }
211         if( !HaveAlready( vMiCmd ) )
212         {
213                 SetErrorDescription( CMIUtilString::Format( MIRSRC( IDS_CMDFACTORY_ERR_CMD_NOT_REGISTERED ), vMiCmd.c_str() ) );
214                 return MIstatus::failure;
215         }
216
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)();
221
222         SMICmdData cmdData( vCmdData );
223         cmdData.id = pCmd->GetGUID();
224         bOk = pCmd->SetCmdData( cmdData );
225         if( bOk )
226                 vpNewCmd = pCmd;
227
228         return bOk;
229 }