HIPO  4.3.0
High Performance Output data format for experimental physics
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 
19 
20 #ifndef DICTIONARY_H
21 #define DICTIONARY_H
22 
23 #include <cstdlib>
24 #include <map>
25 #include <string>
26 #include <vector>
27 
28 #include "hipoexceptions.h"
29 
30 namespace hipo {
31 
33 
40  typedef struct schemaEntry_t {
41  std::string name;
42  std::string type;
43  int typeId{};
44  int typeSize{};
45  int offset{};
47 
51  enum Type {
52  kByte = 1,
53  kShort = 2,
54  kInt = 3,
55  kFloat = 4,
56  kDouble = 5,
57  kLong = 8
58  };
59 
60 
73 class schema {
74  private:
75 
76  std::map<std::string, int> schemaEntriesMap;
77  std::vector<schemaEntry_t> schemaEntries;
78 
79  int groupid{};
80  int itemid{};
81  int rowLength{};
82  std::string schemaName;
83 
84 
85  int getTypeSize(int id);
86  int getTypeByString(std::string &typeName);
87 
88 
89  public:
90 
92  schema(){ groupid = 0; itemid = 0; rowLength = 0;}
99  schema(const char *name, int __group,int __item){
100  schemaName = name; groupid = __group; itemid = __item;
101  }
104  schema(const schema &s) {
105  schemaName = s.schemaName;
106  schemaEntries = s.schemaEntries;
107  schemaEntriesMap = s.schemaEntriesMap;
108  groupid = s.groupid;
109  itemid = s.itemid;
110  }
111 
113  virtual ~schema()= default;
114 
122  void parse(const std::string& schString);
125  std::string getName() const { return schemaName;}
128  int getGroup(){ return groupid;}
131  int getItem(){ return itemid;}
137  int getSizeForRows(int rows);
138 
141  int getRowLength() const noexcept{
142  const auto nentries = schemaEntries.size()-1;
143  const auto &sch=schemaEntries[nentries];
144  return sch.offset + sch.typeSize;
145  }
146 
152  int getEntryOrder(const char *name) const;
153 
159  bool exists(const char *name) const{
160  if(schemaEntriesMap.count(name)) return true;
161  return false;
162  }
163 
171  int getOffset(int item, int order, int rows) const {
172  const auto &sch=schemaEntries[item];
173  return rows*sch.offset + order*sch.typeSize;
174  }
175 
183  int getOffset(const char *name, int order, int rows) const {
184  int item = schemaEntriesMap.at(name);
185  return getOffset(item,order,rows);
186  }
187 
193  int getEntryType(int item) const noexcept {
194  return schemaEntries[item].typeId;
195  }
201  int getEntryType(const char *name) const noexcept {
202  auto item = getEntryOrder(name);
203  if(item >= 0)
204  return schemaEntries[item].typeId;
205  else
206  return -1;
207  }
208 
214  std::string getEntryName(int item) const noexcept { return schemaEntries[item].name;}
217  int getEntries() const noexcept { return schemaEntries.size();}
219  void show();
220 
223  std::string getSchemaString();
226  std::string getSchemaStringJson();
227 
230  void operator = (const schema &D ) {
231  schemaName = D.schemaName;
232  groupid = D.groupid;
233  itemid = D.itemid;
234  schemaEntries = D.schemaEntries;
235  schemaEntriesMap = D.schemaEntriesMap;
236  }
237 };
238 
239  inline int schema::getEntryOrder(const char *name) const {
240  if(exists(name))
241  return schemaEntriesMap.at(name);
242  return -1;
243  }
244 
245 
248  class dictionary {
249  private:
250  std::map<std::string,schema> factory;
251  public:
252  dictionary()= default;;
253  virtual ~dictionary()= default;;
254 
257  std::vector<std::string> getSchemaList();
260  void addSchema(schema sc){ factory[sc.getName()] = sc;}
264  bool hasSchema(const char *name) { return (factory.count(name)!=0);}
271  schema &getSchema(const char *name){
272  if(factory.count(name)==0){
273  throw hipo::schema_error(std::string("hipo::dictionary: schema '") + name + "' does not exist");
274  }
275  return factory[name];
276  }
282  bool parse(const char *schemaString);
284  void show();
285  };
286 
287 }
288 
289 #endif /* NODE_H */
Collection of schema definitions, typically read from a HIPO file header.
Definition: dictionary.h:248
bool parse(const char *schemaString)
Parse a full schema string and add it to the dictionary.
Definition: dictionary.cpp:130
dictionary()=default
void addSchema(schema sc)
Add a schema to the dictionary.
Definition: dictionary.h:260
std::vector< std::string > getSchemaList()
Get a list of all schema names in the dictionary.
Definition: dictionary.cpp:121
virtual ~dictionary()=default
bool hasSchema(const char *name)
Check whether a schema with the given name exists.
Definition: dictionary.h:264
void show()
Print all schemas in the dictionary to standard output.
Definition: dictionary.cpp:143
schema & getSchema(const char *name)
Retrieve a schema by name.
Definition: dictionary.h:271
Exception thrown for schema-related errors (e.g., missing schema, parse failure).
Schema definition for a HIPO bank.
Definition: dictionary.h:73
void parse(const std::string &schString)
Parse a schema definition string and populate entries.
Definition: dictionary.cpp:17
void operator=(const schema &D)
Copy-assignment operator.
Definition: dictionary.h:230
schema()
Default constructor. Initializes an empty schema with zero group/item IDs.
Definition: dictionary.h:92
std::string getName() const
Get the schema name.
Definition: dictionary.h:125
std::string getSchemaString()
Serialize the schema to a definition string (e.g. "pid/I,px/F").
Definition: dictionary.cpp:86
virtual ~schema()=default
Virtual destructor (defaulted).
bool exists(const char *name) const
Check whether a column with the given name exists.
Definition: dictionary.h:159
int getEntryOrder(const char *name) const
Get the column index for a given column name.
Definition: dictionary.h:239
std::string getEntryName(int item) const noexcept
Get the name of a column by index.
Definition: dictionary.h:214
schema(const char *name, int __group, int __item)
Construct a schema with a name, group ID, and item ID.
Definition: dictionary.h:99
int getSizeForRows(int rows)
Compute the total buffer size needed for a given number of rows.
Definition: dictionary.cpp:80
int getOffset(int item, int order, int rows) const
Compute the byte offset for a column value by column index.
Definition: dictionary.h:171
void show()
Print the schema to standard output.
Definition: dictionary.cpp:67
int getEntryType(const char *name) const noexcept
Get the type identifier of a column by name.
Definition: dictionary.h:201
int getRowLength() const noexcept
Get the size of a single row in bytes.
Definition: dictionary.h:141
int getItem()
Get the item identifier.
Definition: dictionary.h:131
int getEntries() const noexcept
Get the number of columns in the schema.
Definition: dictionary.h:217
int getGroup()
Get the group identifier.
Definition: dictionary.h:128
std::string getSchemaStringJson()
Serialize the schema to a JSON-formatted string.
Definition: dictionary.cpp:100
int getOffset(const char *name, int order, int rows) const
Compute the byte offset for a column value by column name.
Definition: dictionary.h:183
int getEntryType(int item) const noexcept
Get the type identifier of a column by index.
Definition: dictionary.h:193
schema(const schema &s)
Copy constructor.
Definition: dictionary.h:104
struct hipo::schemaEntry_t schemaEntry_t
Describes a single column (entry) within a schema.
Exception classes for HIPO library error handling.
Definition: bank.cpp:47
Type
Supported column data types for HIPO banks.
Definition: dictionary.h:51
@ kLong
64-bit signed integer.
Definition: dictionary.h:57
@ kInt
32-bit signed integer.
Definition: dictionary.h:54
@ kFloat
32-bit IEEE 754 floating point.
Definition: dictionary.h:55
@ kShort
16-bit signed integer.
Definition: dictionary.h:53
@ kByte
8-bit signed integer.
Definition: dictionary.h:52
@ kDouble
64-bit IEEE 754 floating point.
Definition: dictionary.h:56
Describes a single column (entry) within a schema.
Definition: dictionary.h:40
int typeId
Numeric type identifier (see Type).
Definition: dictionary.h:43
int offset
Byte offset of this column within a row.
Definition: dictionary.h:45
std::string type
Type string (e.g. "B", "S", "I", "F", "D", "L").
Definition: dictionary.h:42
std::string name
Column name.
Definition: dictionary.h:41
int typeSize
Size of the type in bytes.
Definition: dictionary.h:44