Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:04:10

0001 // -*- C++ -*-
0002 //
0003 // Package:     FWLite
0004 // Class  :     format_type_name
0005 //
0006 // Implementation:
0007 //     [Notes on implementation]
0008 //
0009 // Original Author:
0010 //         Created:  Thu Dec  3 16:52:50 CST 2009
0011 //
0012 
0013 // system include files
0014 #include <string>
0015 
0016 // user include files
0017 #include "DataFormats/FWLite/interface/format_type_name.h"
0018 
0019 //
0020 // constants, enums and typedefs
0021 //
0022 
0023 //
0024 // static data member definitions
0025 //
0026 
0027 static const std::string s_symbolDemangled[] = {"::", "_", ",", " ", "&", "*", "<", ">"};
0028 static const std::string s_symbolMangled[] = {"_1", "_2", "_3", "_4", "_7", "_8", "_9", "_0"};
0029 static const unsigned int s_symbolToMangledSize = sizeof(s_symbolDemangled) / sizeof(std::string);
0030 
0031 namespace fwlite {
0032 
0033   void staticAssert() {
0034     static_assert(sizeof(s_symbolMangled) == sizeof(s_symbolDemangled), "Arrays are not the same size.");
0035   }
0036 
0037   ///given a C++ class name returned a mangled name
0038   std::string format_type_to_mangled(const std::string& iType) {
0039     std::string returnValue;
0040     returnValue.append(static_cast<std::string::size_type>(iType.size() * 2), ' ');
0041     std::string::size_type fromIndex = 0;
0042     std::string::size_type toIndex = 0;
0043     size_t sIndex = 0;
0044     for (; fromIndex < iType.size(); ++fromIndex) {
0045       bool foundMatch = false;
0046       for (sIndex = 0; sIndex < s_symbolToMangledSize;) {
0047         const std::string& symbol = s_symbolDemangled[sIndex];
0048         if (iType.substr(fromIndex, symbol.size()) == symbol) {
0049           foundMatch = true;
0050           break;
0051         }
0052         ++sIndex;
0053       }
0054       if (!foundMatch) {
0055         returnValue[toIndex] = iType[fromIndex];
0056         ++toIndex;
0057       } else {
0058         const std::string& mangled = s_symbolMangled[sIndex];
0059         returnValue.replace(toIndex, mangled.size(), mangled);
0060         toIndex += mangled.size();
0061         fromIndex += s_symbolDemangled[sIndex].size() - 1;
0062       }
0063     }
0064     returnValue.resize(toIndex);
0065     return returnValue;
0066   }
0067 
0068   ///given a mangled name return the C++ class name
0069   std::string unformat_mangled_to_type(const std::string& iMangled) {
0070     std::string returnValue;
0071     returnValue.append(static_cast<std::string::size_type>(iMangled.size() * 2), ' ');
0072     std::string::size_type fromIndex = 0;
0073     std::string::size_type toIndex = 0;
0074     size_t sIndex = 0;
0075     for (; fromIndex < iMangled.size(); ++fromIndex) {
0076       bool foundMatch = false;
0077       for (sIndex = 0; sIndex < s_symbolToMangledSize;) {
0078         const std::string& mangled = s_symbolMangled[sIndex];
0079         if (iMangled.substr(fromIndex, mangled.size()) == mangled) {
0080           foundMatch = true;
0081           break;
0082         }
0083         ++sIndex;
0084       }
0085       if (!foundMatch) {
0086         returnValue[toIndex] = iMangled[fromIndex];
0087         ++toIndex;
0088       } else {
0089         const std::string& symbol = s_symbolDemangled[sIndex];
0090         returnValue.replace(toIndex, symbol.size(), symbol);
0091         toIndex += symbol.size();
0092         fromIndex += s_symbolMangled[sIndex].size() - 1;
0093       }
0094     }
0095     returnValue.resize(toIndex);
0096     return returnValue;
0097   }
0098 
0099 }  // namespace fwlite