HIPO4 C++ Library 4.4.1
Columnar I/O library for CLAS12 physics data
Loading...
Searching...
No Matches
node.h
Go to the documentation of this file.
1//******************************************************************************
2//* ██╗ ██╗██╗██████╗ ██████╗ ███████╗ ██████╗
3//* ██║ ██║██║██╔══██╗██╔═══██╗ ██╔════╝ ██╔═████╗
4//* ███████║██║██████╔╝██║ ██║ ███████╗ ██║██╔██║
5//* ██╔══██║██║██╔═══╝ ██║ ██║ ╚════██║ ████╔╝██║
6//* ██║ ██║██║██║ ╚██████╔╝ ███████║██╗╚██████╔╝
7//* ╚═╝ ╚═╝╚═╝╚═╝ ╚═════╝ ╚══════╝╚═╝ ╚═════╝
8//************************ Jefferson National Lab (2023) ***********************
9/*
10 * Copyright (c) 2017. Jefferson Lab (JLab). All rights reserved. Permission
11 * to use, copy, modify, and distribute this software and its documentation
12 * for educational, research, and not-for-profit purposes, without fee and
13 * without a signed licensing agreement.
14 *
15 * IN NO EVENT SHALL JLAB BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL
16 * INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING
17 * OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF JLAB HAS
18 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19 *
20 * JLAB SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE. THE HIPO DATA FORMAT SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF
23 * ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". JLAB HAS NO OBLIGATION TO
24 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
25 *
26 * This software was developed under the United States Government license.
27 * For more information contact author at gavalian@jlab.org
28 * Department of Experimental Nuclear Physics, Jefferson Lab.
29 */
30/*******************************************************************************
31 * File: bank.h
32 * Author: gavalian
33 *
34 * Created on April 12, 2017, 10:14 AM
35 */
36
37#ifndef HIPO_NODE_H
38#define HIPO_NODE_H
39#include <iostream>
40#include <vector>
41#include <cstring>
42#include <cstdint>
43#include <cstdio>
44#include <cstdlib>
45#include <map>
46#include <tuple>
47#include "constants.h"
48
49namespace hipo {
50
51 class node {
52
53 private:
54
55 std::vector<char> nodeBuffer;
56 const char *nodePointer;
57
58 protected:
59
60 void create(int group, int item, int type, int size);
61
62 //void initStructureBySize(int __group, int __item, int __type, int __size);
63 //std::vector<char> &getStructureBuffer(){ return structureBuffer;}
64 //int getStructureBufferSize(){ return 8+getSize();}
65 //int dataOffset = 8;
66 //std::vector<char> structureBuffer;
67 void init(const char *b, int length){ allocate(length); memcpy(const_cast<char *>(nodePointer),b,length);}
68 void initEmpty();
69
70 public:
71
72 node(){ allocate(8); nodePointer = &nodeBuffer[0];}
73 node(std::tuple<int,int,int,int> params){
74 create(std::get<0>(params),std::get<1>(params), std::get<2>(params), std::get<3>(params));
75 }
76 node(int size){ allocate(8+size); nodePointer = &nodeBuffer[0];}
77
78 //structure(int __group, int __item, std::string &str);
79
80 virtual ~node()= default;
81
82 void assign(std::tuple<int,int,int,int> params );
83
84 bool allocate(int size){
85 if(((int) nodeBuffer.size()) <size){
86 nodeBuffer.resize(size+8); nodePointer = &nodeBuffer[0];
87 } return true;
88 }
89
90 virtual void reset(){ setDataLength(0);}
91
92 int size() const noexcept{
93 int length = *reinterpret_cast<const uint32_t *>(nodePointer+4);
94 return length&STRUCT_SIZE_MASK;
95 //return getHeaderSize()+getDataSize();
96 }
97
98 int capacity() const noexcept{
99 return (int) nodeBuffer.size();
100 //return getHeaderSize()+getDataSize();
101 }
102
103 int formatLength() const noexcept {
104 int length = *reinterpret_cast<const uint32_t *>(nodePointer+4);
106 }
107
108 void setFormatLength(int length){
109 if(length<128){
110 char *dest = const_cast<char *>(nodePointer);
111 *reinterpret_cast<uint32_t *>(dest+4) = ((length<<STRUCT_FORMAT_SHIFT)&STRUCT_FORMAT_MASK)|(length&STRUCT_SIZE_MASK);
112 } else { printf("node::setFormatLength: error >>> the format length can not exceed 128, you tried to set it to %d\n",length);}
113 }
114
115 void setDataLength(int length){
116 int word = *reinterpret_cast<const uint32_t *>(nodePointer+4);
117 int flength = (word>>STRUCT_FORMAT_SHIFT)&STRUCT_FORMAT_BYTE;
118 setNodeLength(flength+length);
119 /*int word = *reinterpret_cast<const uint32_t *>(nodePointer+4);
120 int flength = (word>>24)&0x000000FF;
121 int totalLength = length + flength;
122 char *dest = const_cast<char *>(nodePointer);
123 *reinterpret_cast<uint32_t *>(dest+4) = (word&0xFF000000)|(totalLength&0x00FFFFFF);*/
124 }
125
126 int dataLength() const noexcept {
127 int size = (*reinterpret_cast<const uint32_t *>(nodePointer+4))&STRUCT_SIZE_MASK;
128 int fsize = ((*reinterpret_cast<const uint32_t *>(nodePointer+4))>>STRUCT_FORMAT_SHIFT)&STRUCT_FORMAT_BYTE;
129 return size-fsize;
130 }
131
133 int size = (*reinterpret_cast<const uint32_t *>(nodePointer+4))&STRUCT_SIZE_MASK;
134 return size;
135 }
136
138 if(size<16777215){
139 uint32_t word = *reinterpret_cast<const uint32_t *>(nodePointer+4);
140 uint32_t nodeSize = (word&STRUCT_FORMAT_MASK)|(size&STRUCT_SIZE_MASK);
141 char *dest = const_cast<char *>(nodePointer);
142 *reinterpret_cast<uint32_t *>(dest+4) = nodeSize;
143 } else { printf("node::setNodeLength:: error: the total size of the node exceeds 16777215\n");}
144 }
145
146 int dataOffset() const noexcept;
147
148 int group(){return (int) (*reinterpret_cast<const uint16_t *>(nodePointer));}
149 int item(){return (int) (*reinterpret_cast<const uint8_t *>(nodePointer+2));}
150 int type(){return (int) (*reinterpret_cast<const uint8_t *>(nodePointer+3));}
151
152 //void init(const char *buffer, int size);
153 //void initNoCopy(const char *buffer, int size);
154
155 const char *pointer(){ return nodePointer;};
156
157 virtual void show();
158
159 void setSize(int size);
160 //void setHeaderSize(int size);
161 //void setDataSize(int size);
162
163 int getIntAt ( int index) const noexcept {
164 return *reinterpret_cast<const int32_t*>(nodePointer + index + dataOffset());
165 }
166
167 int16_t getShortAt ( int index) const noexcept {
168 return *reinterpret_cast<const int16_t*>(nodePointer + index + dataOffset());
169 }
170 int8_t getByteAt ( int index) const noexcept {
171 return *reinterpret_cast<const int8_t*>(nodePointer + index + dataOffset());
172 }
173 float getFloatAt ( int index) const noexcept {
174 return *reinterpret_cast<const float*>(nodePointer + index + dataOffset());
175 }
176 double getDoubleAt( int index) const noexcept {
177 return *reinterpret_cast<const double*>(nodePointer + index + dataOffset());
178 }
179 long getLongAt ( int index) const noexcept {
180 return *reinterpret_cast<const int64_t*>(nodePointer + index + dataOffset());
181 }
182
183 //std::string getStringAt(int index);
184
185 void putIntAt(int index, int value){
186 char *a = const_cast<char*>(nodePointer);
187 *reinterpret_cast<int32_t*>(a + index + dataOffset()) = value;
188 }
189
190 void putShortAt(int index, int16_t value){
191 char *a = const_cast<char*>(nodePointer);
192 *reinterpret_cast<int16_t*>(a + index + dataOffset()) = value;
193 }
194
195 void putByteAt(int index, int8_t value){
196 char *a = const_cast<char*>(nodePointer);
197 *reinterpret_cast<int8_t*>(a + index + dataOffset()) = value;
198 }
199
200 void putFloatAt(int index, float value){
201 char *a = const_cast<char*>(nodePointer);
202 *reinterpret_cast<float*>(a + index + dataOffset()) = value;
203 }
204
205 void putDoubleAt(int index, double value){
206 char *a = const_cast<char*>(nodePointer);
207 *reinterpret_cast<double*>(a + index + dataOffset()) = value;
208 }
209
210 void putLongAt(int index, int64_t value){
211 char *a = const_cast<char*>(nodePointer);
212 *reinterpret_cast<int64_t*>(a + index + dataOffset()) = value;
213 }
214
215 //void putStringAt(int index, std::string &str);
216
217 virtual void notify(){}
218
219 friend class tuple;
220 friend class event;
221 };
222
223 inline int node::dataOffset() const noexcept {
224 int fsize = ((*reinterpret_cast<const uint32_t *>(nodePointer+4))>>STRUCT_FORMAT_SHIFT)&STRUCT_FORMAT_BYTE;
225 return 8+fsize;
226 }
227}
228#endif /* NODE_H */
Definition event.h:62
Definition node.h:51
void setNodeLength(int size)
Definition node.h:137
int formatLength() const noexcept
Definition node.h:103
virtual void reset()
Definition node.h:90
int group()
Definition node.h:148
int item()
Definition node.h:149
int size() const noexcept
Definition node.h:92
virtual void show()
Definition node.cpp:83
float getFloatAt(int index) const noexcept
Definition node.h:173
void putFloatAt(int index, float value)
Definition node.h:200
void assign(std::tuple< int, int, int, int > params)
Definition node.cpp:61
int capacity() const noexcept
Definition node.h:98
int8_t getByteAt(int index) const noexcept
Definition node.h:170
int dataOffset() const noexcept
Definition node.h:223
node()
Definition node.h:72
int dataLength() const noexcept
Definition node.h:126
void putShortAt(int index, int16_t value)
Definition node.h:190
void initEmpty()
Definition node.cpp:75
const char * pointer()
Definition node.h:155
void create(int group, int item, int type, int size)
Definition node.cpp:47
node(int size)
Definition node.h:76
void putByteAt(int index, int8_t value)
Definition node.h:195
long getLongAt(int index) const noexcept
Definition node.h:179
int type()
Definition node.h:150
int16_t getShortAt(int index) const noexcept
Definition node.h:167
void setSize(int size)
Definition node.cpp:56
double getDoubleAt(int index) const noexcept
Definition node.h:176
void init(const char *b, int length)
Definition node.h:67
void putIntAt(int index, int value)
Definition node.h:185
node(std::tuple< int, int, int, int > params)
Definition node.h:73
virtual ~node()=default
void putDoubleAt(int index, double value)
Definition node.h:205
void putLongAt(int index, int64_t value)
Definition node.h:210
int getIntAt(int index) const noexcept
Definition node.h:163
virtual void notify()
Definition node.h:217
void setDataLength(int length)
Definition node.h:115
void setFormatLength(int length)
Definition node.h:108
int nodeLength()
Definition node.h:132
bool allocate(int size)
Definition node.h:84
Definition tuple.h:61
HIPO namespace is used for the classes that read/write files and records.
Definition bank.cpp:45
constexpr uint32_t STRUCT_FORMAT_BYTE
Definition constants.h:130
constexpr int STRUCT_FORMAT_SHIFT
Definition constants.h:129
constexpr uint32_t STRUCT_FORMAT_MASK
Definition constants.h:128
constexpr uint32_t STRUCT_SIZE_MASK
Definition constants.h:127