aboutsummaryrefslogtreecommitdiff
path: root/tools/lldb-mi/MICmdFactory.cpp
blob: 3fe4062b772191b45965d1b1172ce07af88158d5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
//===-- MICmdFactory.cpp ----------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

// In-house headers:
#include "MICmdFactory.h"
#include "MICmnResources.h"
#include "MICmdData.h"
#include "MICmdBase.h"
#include "MICmdCommands.h"

//++ ------------------------------------------------------------------------------------
// Details: CMICmdFactory constructor.
// Type:    Method.
// Args:    None.
// Return:  None.
// Throws:  None.
//--
CMICmdFactory::CMICmdFactory(void)
{
}

//++ ------------------------------------------------------------------------------------
// Details: CMICmdFactory destructor.
// Type:    Overridable.
// Args:    None.
// Return:  None.
// Throws:  None.
//--
CMICmdFactory::~CMICmdFactory(void)
{
    Shutdown();
}

//++ ------------------------------------------------------------------------------------
// Details: Initialize resources for *this Command factory.
// Type:    Method.
// Args:    None.
// Return:  MIstatus::success - Functionality succeeded.
//          MIstatus::failure - Functionality failed.
// Throws:  None.
//--
bool
CMICmdFactory::Initialize(void)
{
    m_clientUsageRefCnt++;

    if (m_bInitialized)
        return MIstatus::success;

    m_bInitialized = true;

    MICmnCommands::RegisterAll();

    return MIstatus::success;
}

//++ ------------------------------------------------------------------------------------
// Details: Release resources for *this Command Factory.
// Type:    Method.
// Args:    None.
// Return:  MIstatus::success - Functionality succeeded.
//          MIstatus::failure - Functionality failed.
// Throws:  None.
//--
bool
CMICmdFactory::Shutdown(void)
{
    if (--m_clientUsageRefCnt > 0)
        return MIstatus::success;

    if (!m_bInitialized)
        return MIstatus::success;

    m_mapMiCmdToCmdCreatorFn.clear();

    m_bInitialized = false;

    return MIstatus::success;
}

//++ ------------------------------------------------------------------------------------
// Details: Register a command's creator function with the command identitier the MI
//          command name i.e. 'file-exec-and-symbols'.
// Type:    Method.
// Args:    vMiCmd          - (R) Command's name, the MI command.
//          vCmdCreateFn    - (R) Command's creator function pointer.
// Return:  MIstatus::success - Functionality succeeded.
//          MIstatus::failure - Functionality failed.
// Throws:  None.
//--
bool
CMICmdFactory::CmdRegister(const CMIUtilString &vMiCmd, CmdCreatorFnPtr vCmdCreateFn)
{
    if (!IsValid(vMiCmd))
    {
        SetErrorDescription(CMIUtilString::Format(MIRSRC(IDS_CMDFACTORY_ERR_INVALID_CMD_NAME), vMiCmd.c_str()));
        return MIstatus::failure;
    }
    if (vCmdCreateFn == nullptr)
    {
        SetErrorDescription(CMIUtilString::Format(MIRSRC(IDS_CMDFACTORY_ERR_INVALID_CMD_CR8FN), vMiCmd.c_str()));
        return MIstatus::failure;
    }

    if (HaveAlready(vMiCmd))
    {
        SetErrorDescription(CMIUtilString::Format(MIRSRC(IDS_CMDFACTORY_ERR_CMD_ALREADY_REGED), vMiCmd.c_str()));
        return MIstatus::failure;
    }

    MapPairMiCmdToCmdCreatorFn_t pr(vMiCmd, vCmdCreateFn);
    m_mapMiCmdToCmdCreatorFn.insert(pr);

    return MIstatus::success;
}

//++ ------------------------------------------------------------------------------------
// Details: Check a command is already registered.
// Type:    Method.
// Args:    vMiCmd  - (R) Command's name, the MI command.
// Return:  True - registered.
//          False - not found.
// Throws:  None.
//--
bool
CMICmdFactory::HaveAlready(const CMIUtilString &vMiCmd) const
{
    const MapMiCmdToCmdCreatorFn_t::const_iterator it = m_mapMiCmdToCmdCreatorFn.find(vMiCmd);
    if (it != m_mapMiCmdToCmdCreatorFn.end())
        return true;

    return false;
}

//++ ------------------------------------------------------------------------------------
// Details: Check a command's name is valid:
//              - name is not empty
//              - name does not have spaces
// Type:    Method.
// Args:    vMiCmd  - (R) Command's name, the MI command.
// Return:  True - valid.
//          False - not valid.
// Throws:  None.
//--
bool
CMICmdFactory::IsValid(const CMIUtilString &vMiCmd) const
{
    bool bValid = true;

    if (vMiCmd.empty())
    {
        bValid = false;
        return false;
    }

    const size_t nPos = vMiCmd.find(" ");
    if (nPos != std::string::npos)
        bValid = false;

    return bValid;
}

//++ ------------------------------------------------------------------------------------
// Details: Check a command is already registered.
// Type:    Method.
// Args:    vMiCmd  - (R) Command's name, the MI command.
// Return:  True - registered.
//          False - not found.
// Throws:  None.
//--
bool
CMICmdFactory::CmdExist(const CMIUtilString &vMiCmd) const
{
    return HaveAlready(vMiCmd);
}

//++ ------------------------------------------------------------------------------------
// Details: Create a command given the specified MI command name. The command data object
//          contains the options for the command.
// Type:    Method.
// Args:    vMiCmd      - (R) Command's name, the MI command.
//          vCmdData    - (RW) Command's metadata status/information/result object.
//          vpNewCmd    - (W) New command instance.
// Return:  MIstatus::success - Functionality succeeded.
//          MIstatus::failure - Functionality failed.
// Throws:  None.
//--
bool
CMICmdFactory::CmdCreate(const CMIUtilString &vMiCmd, const SMICmdData &vCmdData, CMICmdBase *&vpNewCmd)
{
    bool bOk = MIstatus::success;

    vpNewCmd = nullptr;

    if (!IsValid(vMiCmd))
    {
        SetErrorDescription(CMIUtilString::Format(MIRSRC(IDS_CMDFACTORY_ERR_INVALID_CMD_NAME), vMiCmd.c_str()));
        return MIstatus::failure;
    }
    if (!HaveAlready(vMiCmd))
    {
        SetErrorDescription(CMIUtilString::Format(MIRSRC(IDS_CMDFACTORY_ERR_CMD_NOT_REGISTERED), vMiCmd.c_str()));
        return MIstatus::failure;
    }

    const MapMiCmdToCmdCreatorFn_t::const_iterator it = m_mapMiCmdToCmdCreatorFn.find(vMiCmd);
    const CMIUtilString &rMiCmd((*it).first);
    MIunused(rMiCmd);
    CmdCreatorFnPtr pFn = (*it).second;
    CMICmdBase *pCmd = (*pFn)();

    SMICmdData cmdData(vCmdData);
    cmdData.id = pCmd->GetGUID();
    bOk = pCmd->SetCmdData(cmdData);
    if (bOk)
        vpNewCmd = pCmd;

    return bOk;
}