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