HIPO4 C++ Library 4.4.1
Columnar I/O library for CLAS12 physics data
Loading...
Searching...
No Matches
dictionary.h
Go to the documentation of this file.
1/*
2 * To change this license header, choose License Headers in Project Properties.
3 * To change this template file, choose Tools | Templates
4 * and open the template in the editor.
5 */
6
7/*
8 * File: dictionary.h
9 * Author: gavalian
10 *
11 * Created on April 27, 2017, 10:01 AM
12 */
13
14#ifndef DICTIONARY_H
15#define DICTIONARY_H
16
17#include <cstdlib>
18#include <iostream>
19#include <map>
20#include <string>
21#include <vector>
22
23#include "hipoexceptions.h"
24
25namespace hipo {
26
27 typedef struct schemaEntry_t {
28 std::string name;
29 std::string type;
30 int typeId{};
31 int typeSize{};
32 int offset{};
33 } schemaEntry_t;
34
35 enum Type {
36 kByte = 1,
37 kShort = 2,
38 kInt = 3,
39 kFloat = 4,
41 kLong = 8
42 };
43
44
56class schema {
57 private:
58
59 std::map<std::string, int> schemaEntriesMap;
60 std::vector<schemaEntry_t> schemaEntries;
61
62 int groupid{};
63 int itemid{};
64 int rowLength{};
65 mutable int warningCount{10};
66
67 std::string schemaName;
68
69
70 int getTypeSize(int id);
71 int getTypeByString(std::string &typeName);
72
73
74 public:
75
76 schema(){ groupid = 0; itemid = 0; rowLength = 0;}
77 schema(const char *name, int __group,int __item){
78 schemaName = name; groupid = __group; itemid = __item;
79 }
80 schema(const schema &s) {
81 schemaName = s.schemaName;
82 schemaEntries = s.schemaEntries;
83 schemaEntriesMap = s.schemaEntriesMap;
84 groupid = s.groupid;
85 itemid = s.itemid;
86 }
87
88 virtual ~schema()= default;
89
90 void parse(const std::string& schString);
91 std::string getName() const { return schemaName;}
92 int getGroup(){ return groupid;}
93 int getItem(){ return itemid;}
94 int getSizeForRows(int rows);
95
96 int getRowLength() const noexcept{
97 const auto nentries = schemaEntries.size()-1;
98 const auto &sch=schemaEntries[nentries];
99 return sch.offset + sch.typeSize;
100 }
101
102 int getEntryOrder(const char *name) const;
103
104 bool exists(const char *name) const{
105 if(schemaEntriesMap.count(name)) return true;
106 return false;
107 }
108
109 int getOffset(int item, int order, int rows) const {
110 const auto &sch=schemaEntries[item];
111 return rows*sch.offset + order*sch.typeSize;
112 }
113
114 int getOffset(const char *name, int order, int rows) const {
115 int item = schemaEntriesMap.at(name);
116 return getOffset(item,order,rows);
117 }
118
119 int getEntryType(int item) const noexcept {
120 return schemaEntries[item].typeId;
121 }
122 int getEntryType(const char *name) const noexcept {
123 auto item = getEntryOrder(name);
124 if(item >= 0)
125 return schemaEntries[item].typeId;
126 else
127 return -1;
128 }
129
130 std::string getEntryName(int item) const noexcept { return schemaEntries[item].name;}
131 int getEntries() const noexcept { return schemaEntries.size();}
132 void show();
133
134 std::string getSchemaString();
135 std::string getSchemaStringJson();
136
137 void operator = (const schema &D ) {
138 schemaName = D.schemaName;
139 groupid = D.groupid;
140 itemid = D.itemid;
141 schemaEntries = D.schemaEntries;
142 schemaEntriesMap = D.schemaEntriesMap;
143 }
144};
145
146 inline int schema::getEntryOrder(const char *name) const {
147 if(exists(name))
148 return schemaEntriesMap.at(name);//at needed for const function
149
150 if(warningCount>0 ) { warningCount--; std::cout<<"Warning , hipo::schema getEntryOrder(const char *name) item :" <<name<<" not found, for bank "<<schemaName<<" data for this item is not valid "<<std::endl;
151 }
152 return -1;
153 }
154
155
158 private:
159 std::map<std::string,schema> factory;
160 public:
161 dictionary()= default;;
162 virtual ~dictionary()= default;;
163
164 std::vector<std::string> getSchemaList();
165 void addSchema(schema sc){ factory[sc.getName()] = sc;}
166 bool hasSchema(const char *name) { return (factory.count(name)!=0);}
167 schema &getSchema(const char *name){
168 if(factory.count(name)==0){
169 throw hipo::schema_error(std::string("hipo::dictionary: schema '") + name + "' does not exist");
170 }
171 return factory[name];
172 }
173 bool parse(const char *schemaString);
174 void show();
175 };
176
177}
178
179#endif /* NODE_H */
Collection of schema definitions, typically read from a HIPO file header.
Definition dictionary.h:157
bool parse(const char *schemaString)
Definition dictionary.cpp:126
dictionary()=default
void addSchema(schema sc)
Definition dictionary.h:165
schema & getSchema(const char *name)
Definition dictionary.h:167
std::vector< std::string > getSchemaList()
Definition dictionary.cpp:117
virtual ~dictionary()=default
bool hasSchema(const char *name)
Definition dictionary.h:166
void show()
Definition dictionary.cpp:139
Definition hipoexceptions.h:26
Schema definition for a HIPO bank.
Definition dictionary.h:56
void parse(const std::string &schString)
Definition dictionary.cpp:13
void operator=(const schema &D)
Definition dictionary.h:137
schema()
Definition dictionary.h:76
std::string getName() const
Definition dictionary.h:91
std::string getSchemaString()
Definition dictionary.cpp:82
virtual ~schema()=default
bool exists(const char *name) const
Definition dictionary.h:104
int getEntryOrder(const char *name) const
Definition dictionary.h:146
std::string getEntryName(int item) const noexcept
Definition dictionary.h:130
schema(const char *name, int __group, int __item)
Definition dictionary.h:77
int getSizeForRows(int rows)
Definition dictionary.cpp:76
int getOffset(int item, int order, int rows) const
Definition dictionary.h:109
void show()
Definition dictionary.cpp:63
int getEntryType(const char *name) const noexcept
Definition dictionary.h:122
int getRowLength() const noexcept
Definition dictionary.h:96
int getItem()
Definition dictionary.h:93
int getEntries() const noexcept
Definition dictionary.h:131
int getGroup()
Definition dictionary.h:92
std::string getSchemaStringJson()
Definition dictionary.cpp:96
int getOffset(const char *name, int order, int rows) const
Definition dictionary.h:114
int getEntryType(int item) const noexcept
Definition dictionary.h:119
schema(const schema &s)
Definition dictionary.h:80
HIPO namespace is used for the classes that read/write files and records.
Definition bank.cpp:45
Type
Definition dictionary.h:35
@ kLong
Definition dictionary.h:41
@ kInt
Definition dictionary.h:38
@ kFloat
Definition dictionary.h:39
@ kShort
Definition dictionary.h:37
@ kByte
Definition dictionary.h:36
@ kDouble
Definition dictionary.h:40
Definition dictionary.h:27
int typeId
Definition dictionary.h:30
int offset
Definition dictionary.h:32
std::string type
Definition dictionary.h:29
std::string name
Definition dictionary.h:28
int typeSize
Definition dictionary.h:31