1//===-- MICmdArgValThreadGrp.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// In-house headers:
11#include "MICmdArgValThreadGrp.h"
12#include "MICmdArgContext.h"
13
14//++ ------------------------------------------------------------------------------------
15// Details: CMICmdArgValThreadGrp constructor.
16// Type:    Method.
17// Args:    None.
18// Return:  None.
19// Throws:  None.
20//--
21CMICmdArgValThreadGrp::CMICmdArgValThreadGrp()
22    : m_nThreadGrp(0)
23{
24}
25
26//++ ------------------------------------------------------------------------------------
27// Details: CMICmdArgValThreadGrp constructor.
28// Type:    Method.
29// Args:    vrArgName       - (R) Argument's name to search by.
30//          vbMandatory     - (R) True = Yes must be present, false = optional argument.
31//          vbHandleByCmd   - (R) True = Command processes *this option, false = not handled.
32// Return:  None.
33// Throws:  None.
34//--
35CMICmdArgValThreadGrp::CMICmdArgValThreadGrp(const CMIUtilString &vrArgName, const bool vbMandatory, const bool vbHandleByCmd)
36    : CMICmdArgValBaseTemplate(vrArgName, vbMandatory, vbHandleByCmd)
37    , m_nThreadGrp(0)
38{
39}
40
41//++ ------------------------------------------------------------------------------------
42// Details: CMICmdArgValThreadGrp destructor.
43// Type:    Overridden.
44// Args:    None.
45// Return:  None.
46// Throws:  None.
47//--
48CMICmdArgValThreadGrp::~CMICmdArgValThreadGrp()
49{
50}
51
52//++ ------------------------------------------------------------------------------------
53// Details: Parse the command's argument options string and try to extract the value *this
54//          argument is looking for.
55// Type:    Overridden.
56// Args:    vwArgContext    - (RW) The command's argument options string.
57// Return:  MIstatus::success - Functional succeeded.
58//          MIstatus::failure - Functional failed.
59// Throws:  None.
60//--
61bool
62CMICmdArgValThreadGrp::Validate(CMICmdArgContext &vwArgContext)
63{
64    if (vwArgContext.IsEmpty())
65        return m_bMandatory ? MIstatus::failure : MIstatus::success;
66
67    if (vwArgContext.GetNumberArgsPresent() == 1)
68    {
69        const CMIUtilString &rArg(vwArgContext.GetArgsLeftToParse());
70        if (IsArgThreadGrp(rArg) && ExtractNumber(rArg))
71        {
72            m_bFound = true;
73            m_bValid = true;
74            m_argValue = GetNumber();
75            vwArgContext.RemoveArg(rArg);
76            return MIstatus::success;
77        }
78        else
79            return MIstatus::failure;
80    }
81
82    // More than one option...
83    const CMIUtilString::VecString_t vecOptions(vwArgContext.GetArgs());
84    CMIUtilString::VecString_t::const_iterator it = vecOptions.begin();
85    while (it != vecOptions.end())
86    {
87        const CMIUtilString &rArg(*it);
88        if (IsArgThreadGrp(rArg) && ExtractNumber(rArg))
89        {
90            m_bFound = true;
91
92            if (vwArgContext.RemoveArg(rArg))
93            {
94                m_bValid = true;
95                m_argValue = GetNumber();
96                return MIstatus::success;
97            }
98            else
99                return MIstatus::failure;
100        }
101
102        // Next
103        ++it;
104    }
105
106    return MIstatus::failure;
107}
108
109//++ ------------------------------------------------------------------------------------
110// Details: Examine the string and determine if it is a valid string type argument.
111// Type:    Method.
112// Args:    vrTxt   - (R) Some text.
113// Return:  bool    - True = yes valid arg, false = no.
114// Throws:  None.
115//--
116bool
117CMICmdArgValThreadGrp::IsArgThreadGrp(const CMIUtilString &vrTxt) const
118{
119    // Look for i1 i2 i3....
120    const MIint nPos = vrTxt.find('i');
121    if (nPos != 0)
122        return false;
123
124    const CMIUtilString strNum = vrTxt.substr(1);
125    if (!strNum.IsNumber())
126        return false;
127
128    return true;
129}
130
131//++ ------------------------------------------------------------------------------------
132// Details: Extract the thread group number from the thread group argument.
133// Type:    Method.
134// Args:    vrTxt   - (R) Some text.
135// Return:  MIstatus::success - Functional succeeded.
136//          MIstatus::failure - Functional failed.
137// Throws:  None.
138//--
139bool
140CMICmdArgValThreadGrp::ExtractNumber(const CMIUtilString &vrTxt)
141{
142    const CMIUtilString strNum = vrTxt.substr(1);
143    MIint64 nNumber = 0;
144    bool bOk = strNum.ExtractNumber(nNumber);
145    if (bOk)
146    {
147        m_nThreadGrp = static_cast<MIuint>(nNumber);
148    }
149
150    return bOk;
151}
152
153//++ ------------------------------------------------------------------------------------
154// Details: Retrieve the thread group ID found in the argument.
155// Type:    Method.
156// Args:    None.
157// Return:  MIuint - Thread group ID.
158// Throws:  None.
159//--
160MIuint
161CMICmdArgValThreadGrp::GetNumber() const
162{
163    return m_nThreadGrp;
164}
165