Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:44:56

0001 //
0002 // It is expected that the code of this program will be extended by the
0003 // users of the "geners" package as they develop their storable classes.
0004 // To add printing capability for archived objects of a new class,
0005 // make sure that "operator<<" is defined between std::ostream and this
0006 // class. Then add the relevant header file below and insert the
0007 // "printable_type" statement for your class together with other similar
0008 // statements already present in this program.
0009 //
0010 
0011 #include "Alignment/Geners/interface/CPP11_auto_ptr.hh"
0012 #include "Alignment/Geners/interface/ClassId.hh"
0013 #include "Alignment/Geners/interface/MultiFileArchive.hh"
0014 #include "Alignment/Geners/interface/Reference.hh"
0015 #include "Alignment/Geners/interface/complexIO.hh"
0016 
0017 #include "CmdLine.hh"
0018 
0019 #include <iostream>
0020 #include <map>
0021 #include <memory>
0022 #include <set>
0023 
0024 using namespace gs;
0025 using namespace std;
0026 
0027 static void print_usage(const char *progname) {
0028   cout << "\nUsage: " << progname << " [-n] [-c] archive_name item_name item_category\n\n"
0029        << "Optional switches \"-n\" and \"-c\" turn on regular expression "
0030           "search for item\n"
0031        << "name and category, respectively. For each matching item, the "
0032           "program prints\n"
0033        << "its class and its text representation, as defined by \"operator "
0034           "<<\".\n"
0035        << endl;
0036 }
0037 
0038 namespace {
0039   typedef unsigned long (*PrinterFunction)(MultiFileArchive &, const SearchSpecifier &, const SearchSpecifier &);
0040 
0041   template <class T>
0042   struct Printer {
0043     static unsigned long print(MultiFileArchive &ar, const SearchSpecifier &name, const SearchSpecifier &category) {
0044       ClassId id(ClassId::makeId<T>());
0045       Reference<T> ref(ar, name, category);
0046       const unsigned long nItems = ref.size();
0047       for (unsigned long i = 0; i < nItems; ++i)
0048         cout << id.name() << "  " << *ref.get(i) << endl;
0049       return nItems;
0050     }
0051   };
0052 }  // namespace
0053 
0054 #define printable_type(sometype)                    \
0055   do {                                              \
0056     ClassId id(ClassId::makeId<sometype>());        \
0057     typemap[id.name()] = &Printer<sometype>::print; \
0058   } while (0);
0059 
0060 int main(int argc, char const *argv[]) {
0061   typedef std::map<std::string, PrinterFunction> Typemap;
0062 
0063   CmdLine cmdline(argc, argv);
0064   if (argc == 1) {
0065     print_usage(cmdline.progname());
0066     return 0;
0067   }
0068 
0069   bool useRegexForName = false, useRegexForCategory = false;
0070   std::string archiveName, nameIn, categoryIn;
0071 
0072   try {
0073     useRegexForName = cmdline.has("-n");
0074     useRegexForCategory = cmdline.has("-c");
0075 
0076     cmdline.optend();
0077 
0078     const unsigned cmdargc = cmdline.argc();
0079     if (cmdargc != 3)
0080       throw CmdLineError("wrong number of command line arguments");
0081     cmdline >> archiveName >> nameIn >> categoryIn;
0082   } catch (CmdLineError &e) {
0083     cerr << "Error in " << cmdline.progname() << ": " << e.str() << endl;
0084     print_usage(cmdline.progname());
0085     return 1;
0086   }
0087 
0088   MultiFileArchive mar(archiveName.c_str(), "r");
0089   if (!mar.isOpen()) {
0090     cerr << mar.error() << endl;
0091     return 1;
0092   }
0093 
0094   SearchSpecifier name(nameIn, useRegexForName);
0095   SearchSpecifier category(categoryIn, useRegexForCategory);
0096   std::vector<unsigned long long> found;
0097   mar.itemSearch(name, category, &found);
0098   const unsigned long nfound = found.size();
0099   if (!nfound) {
0100     cout << "No items found" << endl;
0101     return 0;
0102   }
0103 
0104   Typemap typemap;
0105 
0106   // You can add more printable types to the following collection
0107   // as long as the type supports "<<" operator with ostream
0108   printable_type(bool);
0109   printable_type(char);
0110   printable_type(unsigned char);
0111   printable_type(signed char);
0112   printable_type(short);
0113   printable_type(unsigned short);
0114   printable_type(int);
0115   printable_type(long);
0116   printable_type(long long);
0117   printable_type(unsigned);
0118   printable_type(unsigned long);
0119   printable_type(unsigned long long);
0120   printable_type(float);
0121   printable_type(double);
0122   printable_type(long double);
0123   printable_type(std::complex<float>);
0124   printable_type(std::complex<double>);
0125   printable_type(std::complex<long double>);
0126   printable_type(std::string);
0127 
0128   // We need to call the generic printer exactly once for each distinct
0129   // type of the items satisfying the search criterion
0130   std::set<std::string> typenames;
0131   unsigned long nprinted = 0;
0132   for (unsigned long i = 0; i < nfound; ++i) {
0133     std::shared_ptr<const CatalogEntry> entry = mar.catalogEntry(found[i]);
0134     const ClassId &type = entry->type();
0135     const std::string &tname = type.name();
0136     if (typenames.insert(tname).second) {
0137       Typemap::iterator it = typemap.find(tname);
0138       if (it != typemap.end())
0139         nprinted += (*it->second)(mar, name, category);
0140     }
0141   }
0142   if (nprinted != nfound) {
0143     const unsigned long notprinted = nfound - nprinted;
0144     cout << "Found " << notprinted << " non-printable item" << (notprinted == 1UL ? "" : "s") << endl;
0145   }
0146 
0147   return 0;
0148 }