![]() |
![]() |
00001 #include "CondCore/ORA/interface/Record.h" 00002 #include "AnyData.h" 00003 #include "RecordDetails.h" 00004 00005 #include<map> 00006 00007 namespace { 00008 ora::AllKnowTypeHandlers allKnowTypeHandlers; 00009 } 00010 00011 00012 namespace ora { 00013 struct RecordSpecImpl { 00014 00015 struct Item { 00016 Item(std::string const & iname, 00017 TypeHandler const * ih) : name(iname), handler(ih){} 00018 std::string name; 00019 TypeHandler const * handler; 00020 }; 00021 00022 00023 std::vector<Item> items; 00024 00025 typedef std::map<std::string, int> Lookup; 00026 00027 Lookup indexes; 00028 00029 00030 }; 00031 00032 00033 RecordSpec::RecordSpec() : specs(new RecordSpecImpl) {} 00034 00035 RecordSpec::~RecordSpec(){} 00036 00037 size_t RecordSpec::add(std::string const & name, std::type_info const & type) { 00038 // check if already exists 00039 TypeHandler const * th = allKnowTypeHandlers(type); 00040 // check if 0... 00041 specs->items.push_back(RecordSpecImpl::Item(name,th)); 00042 specs->indexes.insert(std::make_pair(specs->items.back().name,(int)(specs->items.size())-1)); 00043 return specs->items.size()-1; 00044 } 00045 00046 00047 00048 Record::Record(){} 00049 00050 Record::Record(RecordSpec ispecs) { 00051 init(ispecs); 00052 } 00053 00054 void Record::init(RecordSpec ispecs) { 00055 destroy(); 00056 specs = ispecs.specs; 00057 size_t s = specs->items.size(); 00058 m_field.resize(s); 00059 m_null.resize(s,true); 00060 for (size_t i=0; i<s; ++i) { 00061 RecordSpecImpl::Item const & item = specs->items[i]; 00062 item.handler->create(m_field[i]); 00063 } 00064 } 00065 00066 Record::~Record() { 00067 destroy(); 00068 } 00069 00070 void Record::destroy() { 00071 if (m_field.empty()) return; 00072 for (size_t i=0; i<m_field.size(); ++i) { 00073 RecordSpecImpl::Item const & item = specs->items[i]; 00074 item.handler->destroy(m_field[i]); 00075 } 00076 m_field.clear(); 00077 m_null.clear(); 00078 } 00079 00080 00081 void Record::swap(Record & lh) { 00082 specs.swap(lh.specs); 00083 m_field.swap(lh.m_field); 00084 m_null.swap(lh.m_null); 00085 } 00086 00087 00088 int Record::index(std::string const & iname) const { 00089 if (m_field.empty()) return -1; 00090 RecordSpecImpl::Lookup::const_iterator p = specs->indexes.find(iname); 00091 return (p==specs->indexes.end()) ? -1 : (*p).second; 00092 } 00093 00094 00095 std::type_info const * Record::type(int i) const { 00096 return specs->items[i].handler->type; 00097 } 00098 00099 void const * Record::address(int i) const { 00100 return specs->items[i].handler->address(m_field[i]); 00101 } 00102 00103 void const * Record::get(int i) const { 00104 return specs->items[i].handler->get(m_field[i]); 00105 } 00106 00107 void Record::set(int i, void * p) { 00108 specs->items[i].handler->set(m_field[i],p); 00109 } 00110 00111 00112 std::string const & Record::name(int i) const { 00113 return specs->items[i].name; 00114 } 00115 00116 00117 00118 }