IODA Bundle
ToolFactory.cc
Go to the documentation of this file.
1 /*
2  * (C) Copyright 1996-2012 ECMWF.
3  *
4  * This software is licensed under the terms of the Apache Licence Version 2.0
5  * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6  * In applying this licence, ECMWF does not waive the privileges and immunities
7  * granted to it by virtue of its status as an intergovernmental organisation nor
8  * does it submit to any jurisdiction.
9  */
10 /// \file ToolFactory.cc
11 ///
12 /// @author Piotr Kuchta, ECMWF, Feb 2009
13 ///
14 
15 //#include <iostream>
16 //#include <string>
17 //#include <map>
18 //#include <vector>
19 
20 //#include "eckit/utils/Regex.h"
21 #include "eckit/exception/Exceptions.h"
22 
23 #include "Tool.h"
24 #include "TestCase.h"
25 #include "ToolFactory.h"
26 
27 using namespace std;
28 using namespace eckit;
29 
30 namespace odc {
31 namespace tool {
32 
33 std::map<std::string, AbstractToolFactory *> *AbstractToolFactory::toolFactories = 0;
34 
35 class MatchAll : public std::vector<std::string> {
36 public:
37  MatchAll() { push_back(".*"); }
38 };
39 
40 const std::vector<std::string> AbstractToolFactory::matchAll = MatchAll();
41 
42 std::vector<test::TestCase*>* AbstractToolFactory::testCases(const std::vector<std::string> &patterns)
43 {
44  ASSERT(toolFactories != 0);
45 
46  std::vector<test::TestCase*> *v = new std::vector<test::TestCase*>();
47 
48  for (std::map<std::string,AbstractToolFactory*>::iterator it = toolFactories->begin();
49  it != toolFactories->end();
50  it++)
51  {
52  std::string testName = it->first;
53  if (testName.find("Test") == std::string::npos
54  || !Tool::matchAny(patterns, testName))
55  continue;
56 
57  AbstractToolFactory *factory = it->second;
58  char *argv[] = {const_cast<char*>("test"), 0};
59  Tool *tool = factory->create(1, argv);
60  ASSERT(tool);
61 
62  test::TestCase *testCase = dynamic_cast<test::TestCase *>(tool);
63  if (testCase != 0)
64  {
65  testCase->name(testName);
66  v->push_back(testCase);
67  }
68  else
69  {
70  Log::warning() << "AbstractToolFactory::testCases: " << testName
71  << " is not a TestCase. Skipping" << std::endl;
72  delete tool;
73  }
74  }
75  return v;
76 }
77 
78 AbstractToolFactory& AbstractToolFactory::findTool(const std::string &name)
79 {
80  ASSERT(toolFactories);
81 
82  std::map<std::string,AbstractToolFactory*>::const_iterator it = toolFactories->find(name);
83 
84  ASSERT("Unknown tool" && it != toolFactories->end());
85 
86  return *it->second;
87 }
88 
89 void AbstractToolFactory::printToolHelp(const std::string& name, std::ostream &s)
90 {
91  findTool(name).help(s);
92  s << std::endl;
93 }
94 
95 void AbstractToolFactory::printToolUsage(const std::string& name, std::ostream &s)
96 {
97  findTool(name).usage(name, s);
98  s << std::endl;
99 }
100 
101 void AbstractToolFactory::printToolsHelp(std::ostream &s)
102 {
103  ASSERT(toolFactories);
104 
105  for (std::map<std::string,AbstractToolFactory*>::iterator it = toolFactories->begin();
106  it != toolFactories->end();
107  it++)
108  {
109  std::string toolName = it->first;
110  AbstractToolFactory *toolFactory = it->second;
111 
112  if (toolName.find("Test") == std::string::npos && !toolFactory->experimental())
113  {
114  s << toolName << ":\t";
115  toolFactory->help(s);
116  s << std::endl << "Usage:" << std::endl << "\t";
117  toolFactory->usage(toolName, s);
118  s << std::endl << std::endl;
119  }
120  }
121 }
122 
123 void AbstractToolFactory::listTools(std::ostream& s)
124 {
125  ASSERT(toolFactories);
126 
127  for (std::map<std::string,AbstractToolFactory*>::iterator it = toolFactories->begin();
128  it != toolFactories->end();
129  it++)
130  {
131  std::string toolName = it->first;
132  AbstractToolFactory *toolFactory = it->second;
133 
134  if (toolName.find("Test") == std::string::npos && !toolFactory->experimental())
135  {
136  s << " " << toolName << " ";
137  toolFactory->help(s);
138  s << std::endl;
139  }
140  }
141 }
142 
143 Tool* AbstractToolFactory::createTool(const std::string& name, int argc, char **argv)
144 {
145  AbstractToolFactory *factory = (*toolFactories)[name];
146  if (factory == 0)
147  return 0;
148 
149  return factory->create(argc, argv);
150 };
151 
152 AbstractToolFactory::AbstractToolFactory(const std::string& name)
153 {
154  if (toolFactories == 0)
155  toolFactories = new std::map<std::string, AbstractToolFactory *>();
156 
157  (*toolFactories)[name] = this;
158 }
159 
160 AbstractToolFactory::~AbstractToolFactory()
161 {
162  if (toolFactories)
163  {
164  delete toolFactories;
165  toolFactories = 0;
166  }
167 }
168 
169 } // namespace tool
170 } // namespace odc
171 
virtual Tool * create(int argc, char **argv)=0
virtual void help(std::ostream &)=0
virtual void usage(const std::string &, std::ostream &)=0
std::string name()
Definition: Tool.h:34
Definition: ColumnInfo.h:23
Definition: encode.cc:30