Main Page Class Hierarchy Compound List File List Compound Members File Members

# Pattern Class Reference

`#include <cmt_pattern.h>`

Collaboration diagram for Pattern:

[legend]
List of all members.

## Public Types
typedef cmt_vector<Pattern> | PatternVector
## Public Methods
| Pattern ()
| ~Pattern ()
void | clear ()
void | apply () const
| Applies a pattern to all uses except CMT itself. More...
void | apply (Use* constext_use) const
void | apply (Use* constext_use, const Template::TemplateVector& templates) const
| Applies a pattern to one particular use. More...
void | expand (Use* constext_use, const Template::TemplateVector& templates, cmt_string& line) const
## Public Attributes
cmt_string | name
CmtSystem::cmt_string_vector | words
bool | global
Use* | use
## Static Public Methods
void | action (const CmtSystem::cmt_string_vector& words, Use* use)
Pattern* | find (const cmt_string& name)
| Patterns are stored with two keys : <name> and <use>. More...
Pattern* | find (const cmt_string& name, Use* use)
void | add (const cmt_string& name, const CmtSystem::cmt_string_vector& words, int start_index, bool global, Use* use)
int | pattern_number ()
| Get the number of registered patterns. More...
Pattern& | pattern (int index)
| Get the index'th pattern in the database. More...
void | clear_all ()
PatternVector& | patterns ()
void | apply_all_globals ()
| Applies all global patterns to all uses. More...
void | apply_all_globals (Use* use)
| Applies all global patterns to a given Use. More...
void | show_all_names ()
void | show_all ()
| this is the cmt show patterns command It just shows the pattern declarations. More...
void | show_all_applied_patterns ()
| this is the cmt show applied_patterns command It just shows the pattern applications. More...
void | show (const cmt_string& name)
| This is the cmt show pattern <name> command It shows both the pattern definition(s) and the places where it is explicitly applied. More...

## Member Typedef Documentation

typedef cmt_vector<Pattern> Pattern::PatternVector

| Definition at line 20 of file cmt_pattern.h.

## Constructor & Destructor Documentation

Pattern::Pattern ( | )

| Definition at line 395 of file cmt_pattern.cxx.
    00396 {
    00397 }

Pattern::~Pattern ( | )

| Definition at line 400 of file cmt_pattern.cxx.
    00402 {
    00403 }

## Member Function Documentation

void Pattern::action ( | const CmtSystem::cmt_string_vector & words,
| Use * use )`[static]`

| Definition at line 18 of file cmt_pattern.cxx. Referenced by Cmt::select().
    00019 {
    00020   bool global = false;
    00021   int start_index;
    00022
    00023   if (words.size () < 2) return;
    00024
    00025     //
    00026     // expected syntax is:
    00027     //
    00028     //  pattern [-global] pattern-name any-cmt-statement
    00029     //
    00030     // where any-cmt-statement may contain "templates"
    00031     //
    00032     //      <package>
    00033     //      <PACKAGE>
    00034     //      <version>
    00035     //      <path>
    00036     //
    00037
    00038   cmt_string& option = words[1];
    00039
    00040   if (option == "-global")
    00041     {
    00042       global = true;
    00043       start_index = 2;
    00044     }
    00045   else
    00046     {
    00047       start_index = 1;
    00048     }
    00049
    00050   cmt_string& name = words[start_index];
    00051
    00052   start_index++;
    00053
    00054   add (name, words, start_index, global, use);
    00055
    00056   if (CmtSystem::testenv ("CMTTESTPATTERN"))
    00057     {
    00058       cout << "Pattern::action> add " << name << endl;
    00059     }
    00060 }

void Pattern::add ( | const cmt_string & name,
| const CmtSystem::cmt_string_vector & words,
| int start_index,
| bool global,
| Use * use )`[static]`

| Definition at line 114 of file cmt_pattern.cxx. Referenced by action().
    00119 {
    00120   static PatternVector& Patterns = patterns ();
    00121
    00122   Pattern* pattern;
    00123
    00124   pattern = find (name, use);
    00125
    00126   if (pattern == 0)
    00127     {
    00128         // No pattern for the pair <name, use> exist yet.
    00129         // create one.
    00130       Pattern& p = Patterns.add ();
    00131
    00132       p.clear ();
    00133
    00134       p.name = name;
    00135       p.use  = use;
    00136
    00137       pattern = &p;
    00138     }
    00139   else
    00140     {
    00141       pattern->clear ();
    00142     }
    00143
    00144     //
    00145     // Install the cmt-statement as a vector of words.
    00146     //
    00147   for (int i = start_index; i < words.size (); i++)
    00148     {
    00149       cmt_string& s = pattern->words.add ();
    00150       s = words[i];
    00151     }
    00152
    00153   pattern->global = global;
    00154 }

void Pattern::apply ( | Use * context_use,
| const Template::TemplateVector & templates ) const

| Applies a pattern to one particular use. Definition at line 453 of file cmt_pattern.cxx.
    00455 {
    00456   cmt_string line;
    00457
    00458   expand (context_use, templates, line);
    00459
    00460   if (line != "")
    00461     {
    00462       Cmt::parse_requirements_text (line, "", context_use);
    00463     }
    00464 }

void Pattern::apply ( | Use * context_use ) const

| Definition at line 443 of file cmt_pattern.cxx.
    00444 {
    00445   static Template::TemplateVector dummy_templates;
    00446
    00447   apply (context_use, dummy_templates);
    00448 }

void Pattern::apply ( | ) const

| Applies a pattern to all uses except CMT itself. Definition at line 417 of file cmt_pattern.cxx. Referenced by ApplyPattern::apply(), apply(), apply_all_globals(), and Use::apply_global_patterns().
    00418 {
    00419   Use::UsePtrVector& uses = Use::uses ();
    00420
    00421   Use* current = &(Use::current());
    00422
    00423   Use::UsePtrVector list;
    00424
    00425   if (current != use)
    00426     {
    00427       if (!current->get_paths (use, list)) return;
    00428     }
    00429   else
    00430     {
    00431       list.push_back (current);
    00432     }
    00433
    00434   for (int i = 0; i < list.size (); i++)
    00435     {
    00436       Use* u = list[i];
    00437       if ((u->package != "CMT") &&
    00438           (IgnorePattern::find (name, u) == 0)) apply (u);
    00439     }
    00440 }

void Pattern::apply_all_globals ( | Use * use )`[static]`

| Applies all global patterns to a given Use. Definition at line 219 of file cmt_pattern.cxx.
    00220 {
    00221   if (use->package == "CMT") return;
    00222
    00223   static PatternVector& Patterns = patterns ();
    00224
    00225   int i;
    00226
    00227   for (i = 0; i < Patterns.size (); i++)
    00228     {
    00229       Pattern& p = Patterns[i];
    00230
    00231       if (p.global)
    00232         {
    00233           if (IgnorePattern::find (p.name, use) == 0) p.apply (use);
    00234         }
    00235     }
    00236 }

void Pattern::apply_all_globals ( | )`[static]`

| Applies all global patterns to all uses. Definition at line 202 of file cmt_pattern.cxx. Referenced by Cmt::load(), and Cmt::reach_current_package().
    00203 {
    00204   static PatternVector& Patterns = patterns ();
    00205
    00206   int i;
    00207
    00208   for (i = 0; i < Patterns.size (); i++)
    00209     {
    00210       Pattern& p = Patterns[i];
    00211
    00212       if (p.global) p.apply ();
    00213     }
    00214 }

void Pattern::clear ( | )

| Definition at line 406 of file cmt_pattern.cxx. Referenced by add(), and clear_all().
    00407 {
    00408   global = false;
    00409   name = "";
    00410   use = 0;
    00411   words.clear ();
    00412 }

void Pattern::clear_all ( | )`[static]`

| Definition at line 177 of file cmt_pattern.cxx. Referenced by Database::clear().
    00178 {
    00179   static PatternVector& Patterns = patterns ();
    00180
    00181   for (int i = 0; i < Patterns.size (); i++)
    00182     {
    00183       Pattern& p = Patterns[i];
    00184       p.clear ();
    00185     }
    00186
    00187   Patterns.clear ();
    00188 }

void Pattern::expand ( | Use * context_use,
| const Template::TemplateVector & templates,
| cmt_string & line ) const

| Definition at line 467 of file cmt_pattern.cxx. Referenced by apply(), and ApplyPattern::show().
    00470 {
    00471   line = "";
    00472
    00473     // First re-create the cmt statement as one line.
    00474   for (int i = 0; i < words.size (); i++)
    00475     {
    00476       const cmt_string& w = words[i];
    00477
    00478       if (i > 0) line += " ";
    00479
    00480       if ((w == "\n") | (w == ";"))
    00481         {
    00482           line += "\n";
    00483         }
    00484       else
    00485         {
    00486           line += "\"";
    00487           line += w;
    00488           line += "\"";
    00489         }
    00490     }
    00491
    00492   if (context_use == 0) context_use = &(Use::current ());
    00493
    00494   if (line != "")
    00495     {
    00496         // Substitute templates from the cmt statement
    00497       line.replace_all ("<package>", context_use->package.c_str ());
    00498       line.replace_all ("<PACKAGE>", context_use->prefix.c_str ());
    00499       line.replace_all ("<version>", context_use->version.c_str ());
    00500       line.replace_all ("<path>",    context_use->real_path.c_str ());
    00501
    00502       for (int j = 0; j < templates.size (); j++)
    00503         {
    00504           Template& t = templates[j];
    00505           cmt_string s;
    00506           s = "<";
    00507           s += t.name;
    00508           s += ">";
    00509           line.replace_all (s, t.value);
    00510         }
    00511
    00512       for (;;)
    00513         {
    00514           int begin = line.find ("<");
    00515           if (begin == cmt_string::npos) break;
    00516           int end = line.find (begin, ">");
    00517           if (end == cmt_string::npos) break;
    00518           // Do not erase XML constructs
    00519           if (line[end-1] == '/') break;
    00520           line.erase (begin, end - begin + 1);
    00521         }
    00522     }
    00523 }

Pattern * Pattern::find ( | const cmt_string & name,
| Use * use )`[static]`

| Definition at line 91 of file cmt_pattern.cxx.
    00092 {
    00093   static PatternVector& Patterns = patterns ();
    00094
    00095   int pattern_index;
    00096   Pattern* pattern;
    00097
    00098   if (Patterns.size () == 0) return (0);
    00099
    00100   for (pattern_index = 0; pattern_index < Patterns.size (); pattern_index++)
    00101     {
    00102       pattern = &(Patterns[pattern_index]);
    00103
    00104       if ((pattern->name == name) && (pattern->use) == use)
    00105         {
    00106           return (pattern);
    00107         }
    00108     }
    00109
    00110   return (0);
    00111 }

Pattern * Pattern::find ( | const cmt_string & name )`[static]`

| Patterns are stored with two keys : <name> and <use>. thus search must done against these two keys. Definition at line 69 of file cmt_pattern.cxx. Referenced by add(), ApplyPattern::apply(), and ApplyPattern::show().
    00070 {
    00071   Use::UsePtrVector& uses = Use::uses ();
    00072
    00073   Use* use = &(Use::current ());
    00074
    00075   Pattern* p;
    00076
    00077   p = find (name, use);
    00078   if (p != 0) return (p);
    00079
    00080   for (int i = 0; i < uses.size (); i++)
    00081     {
    00082       use = uses[i];
    00083       p = find (name, use);
    00084       if (p != 0) return (p);
    00085     }
    00086
    00087   return (0);
    00088 }

Pattern & Pattern::pattern ( | int index )`[static]`

| Get the index'th pattern in the database. Definition at line 169 of file cmt_pattern.cxx.
    00170 {
    00171   static PatternVector& Patterns = patterns ();
    00172
    00173   return (Patterns[index]);
    00174 }

int Pattern::pattern_number ( | )`[static]`

| Get the number of registered patterns. Definition at line 159 of file cmt_pattern.cxx.
    00160 {
    00161   static PatternVector& Patterns = patterns ();
    00162
    00163   return (Patterns.size ());
    00164 }

Pattern::PatternVector & Pattern::patterns ( | )`[static]`

| Definition at line 191 of file cmt_pattern.cxx. Referenced by add(), apply_all_globals(), Use::apply_global_patterns(), clear_all(), find(), pattern(), pattern_number(), show(), show_all(), and show_all_names().
    00192 {
    00193   static Database& db = Database::instance ();
    00194   static PatternVector& Patterns = db.patterns ();
    00195
    00196   return (Patterns);
    00197 }

void Pattern::show ( | const cmt_string & name )`[static]`

| This is the cmt show pattern <name> command It shows both the pattern definition(s) and the places where it is explicitly applied. Definition at line 317 of file cmt_pattern.cxx. Referenced by Cmt::do_show_pattern().
    00318 {
    00319   static PatternVector& Patterns = patterns ();
    00320
    00321   int i;
    00322   int j;
    00323
    00324   bool found = false;
    00325
    00326     // First show the definitions.
    00327   for (i = 0; i < Patterns.size (); i++)
    00328     {
    00329       Pattern& p = Patterns[i];
    00330
    00331       if (p.name == name)
    00332         {
    00333           found = true;
    00334
    00335           cout << "# " << p.use->package << " " << p.use->version << " defines ";
    00336           if (p.global) cout << "global ";
    00337           cout << "pattern " << p.name << " as [";
    00338
    00339           for (int wi = 0; wi < p.words.size (); wi++)
    00340             {
    00341               const cmt_string& s = p.words[wi];
    00342               if (wi > 0) cout << " ";
    00343               cout << s;
    00344             }
    00345
    00346           cout << "]" << endl;
    00347         }
    00348     }
    00349
    00350   if (!found)
    00351     {
    00352       CmtError::set (CmtError::pattern_not_found, name);
    00353       return;
    00354     }
    00355
    00356     //
    00357     // Then show the packages which explicitly apply the pattern.
    00358     //
    00359   Use* use;
    00360   ApplyPattern* apply_pattern = 0;
    00361
    00362   Use::UsePtrVector& uses = Use::uses ();
    00363
    00364   for (i = 0; i < uses.size (); i++)
    00365     {
    00366       use = uses[i];
    00367       for (j = 0; j < use->apply_patterns.size (); j++)
    00368         {
    00369           apply_pattern = &(use->apply_patterns[j]);
    00370
    00371           if (apply_pattern->name == name)
    00372             {
    00373               cout << "# " << use->package << " " << use->version << " applies pattern " << name;
    00374               cout << " => ";
    00375               apply_pattern->show ();
    00376             }
    00377         }
    00378     }
    00379
    00380   use = &(Use::current ());
    00381   for (j = 0; j < use->apply_patterns.size (); j++)
    00382     {
    00383       apply_pattern = &(use->apply_patterns[j]);
    00384
    00385       if (apply_pattern->name == name)
    00386         {
    00387           cout << "# " << use->package << " " << use->version << " applies pattern " << name;
    00388           cout << " => ";
    00389           apply_pattern->show ();
    00390         }
    00391     }
    00392 }

void Pattern::show_all ( | )`[static]`

| this is the cmt show patterns command It just shows the pattern declarations. Definition at line 242 of file cmt_pattern.cxx. Referenced by Cmt::do_show_patterns().
    00243 {
    00244   static PatternVector& Patterns = patterns ();
    00245
    00246   int i;
    00247
    00248   for (i = 0; i < Patterns.size (); i++)
    00249     {
    00250       Pattern& p = Patterns[i];
    00251
    00252       cout << "# " << p.use->package << " " << p.use->version << " defines ";
    00253       if (p.global) cout << "global ";
    00254       cout << "pattern " << p.name << " as [";
    00255
    00256       for (int wi = 0; wi < p.words.size (); wi++)
    00257         {
    00258           const cmt_string& s = p.words[wi];
    00259           if (wi > 0) cout << " ";
    00260           cout << s;
    00261         }
    00262
    00263       cout << "]" << endl;
    00264     }
    00265
    00266   Use* use = &(Use::current ());
    00267   for (i = 0; i < use->apply_patterns.size (); i++)
    00268     {
    00269       const ApplyPattern& apply_pattern = use->apply_patterns[i];
    00270
    00271       cout << "# " << use->package
    00272            << " applies pattern " << apply_pattern.name << " => ";
    00273       apply_pattern.show ();
    00274     }
    00275 }

void Pattern::show_all_applied_patterns ( | )`[static]`

| this is the cmt show applied_patterns command It just shows the pattern applications. Definition at line 281 of file cmt_pattern.cxx. Referenced by Cmt::do_show_applied_patterns().
    00282 {
    00283   Use* use = &(Use::current ());
    00284   for (int i = 0; i < use->apply_patterns.size (); i++)
    00285     {
    00286       const ApplyPattern& apply_pattern = use->apply_patterns[i];
    00287
    00288       cout << "# " << use->package
    00289            << " applies pattern " << apply_pattern.name << " => ";
    00290       apply_pattern.show ();
    00291     }
    00292 }

void Pattern::show_all_names ( | )`[static]`

| Definition at line 295 of file cmt_pattern.cxx. Referenced by Cmt::do_show_pattern_names().
    00296 {
    00297   static PatternVector& Patterns = patterns ();
    00298
    00299   if (Patterns.size () > 0)
    00300     {
    00301       for (int i = 0; i < Patterns.size (); i++)
    00302         {
    00303           Pattern& p = Patterns[i];
    00304
    00305           cout << p.name << " ";
    00306         }
    00307
    00308       cout << endl;
    00309     }
    00310 }

## Member Data Documentation

bool Pattern::global

| Definition at line 59 of file cmt_pattern.h.

cmt_string Pattern::name

| Definition at line 57 of file cmt_pattern.h.

Use * Pattern::use

| Definition at line 60 of file cmt_pattern.h.

CmtSystem::cmt_string_vector Pattern::words

| Definition at line 58 of file cmt_pattern.h.

The documentation for this class was generated from the following files:

- cmt_pattern.h
- cmt_pattern.cxx