Warning, /include/Geant4/toolx/hdf5/pages is written in an unsupported language. File is not indexed.
0001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
0002 // See the file tools.license for terms.
0003
0004 #ifndef toolx_hdf5_pages
0005 #define toolx_hdf5_pages
0006
0007 #include "T_tools"
0008 #include "tools"
0009
0010 #include <tools/S_STRING>
0011
0012 #include <ostream>
0013
0014 namespace toolx {
0015 namespace hdf5 {
0016
0017 TOOLS_GLOBAL_STRING(pages)
0018 TOOLS_GLOBAL_STRING(entries)
0019 TOOLS_GLOBAL_STRING(columns)
0020 TOOLS_GLOBAL_STRING(names)
0021 TOOLS_GLOBAL_STRING(forms)
0022
0023 class pages {
0024 TOOLS_SCLASS(toolx::hdf5::pages)
0025 public:
0026 pages(std::ostream& a_out,
0027 hid_t a_group,const std::string& a_name,const std::string& a_form,
0028 bool a_write,unsigned int a_compress)
0029 :m_out(a_out),m_name(a_name),m_form(a_form)
0030 //,m_type(0),m_size(0)
0031 ,m_group(-1),m_dataset(-1),m_write(a_write),m_compress(a_compress),m_entries(0),m_pos(0){
0032 if(m_write) {
0033 m_group = toolx_H5Gcreate(a_group,m_name.c_str(),0);
0034 if(m_group<0) {
0035 m_out << "pages::pages : can't create group for column " << m_name << "." << std::endl;
0036 m_group = -1;
0037 return;
0038 }
0039 if(!write_atb(m_group,"class",s_class())) {
0040 m_out << "pages::pages : write_atb(class) failed." << std::endl;
0041 ::H5Gclose(m_group);
0042 m_group = -1;
0043 return;
0044 }
0045 int v = 1;
0046 if (!write_scalar_atb<int>(m_group,"version",v)) {
0047 m_out << "pages::pages : write_scalar_atb(version) failed." << std::endl;
0048 ::H5Gclose(m_group);
0049 m_group = -1;
0050 return;
0051 }
0052 } else {
0053 m_group = toolx_H5Gopen(a_group,m_name.c_str());
0054 if(m_group<0) {
0055 m_out << "pages::pages : can't open group for column " << m_name << "." << std::endl;
0056 m_group = -1;
0057 return;
0058 }
0059 if(!read_scalar<tools::uint64>(m_group,s_entries(),m_entries)) {
0060 m_out << "pages::pages : read_scalar(entries) failed." << std::endl;
0061 ::H5Gclose(m_group);
0062 m_group = -1;
0063 return;
0064 }
0065 }
0066 }
0067 virtual ~pages(){
0068 if(m_write) {
0069 if(!write_scalar<tools::uint64>(m_group,s_entries(),m_entries)) {
0070 m_out << "pages::~pages : write_scalar(entries) failed." << std::endl;
0071 }
0072 if(m_dataset>=0) ::H5Dclose(m_dataset);
0073 }
0074 ::H5Gclose(m_group);
0075 }
0076 protected:
0077 pages(const pages& a_from):m_out(a_from.m_out){
0078 }
0079 pages& operator=(const pages&){return *this;}
0080 public:
0081 bool is_valid() const {return m_group<0?false:true;}
0082
0083 const std::string& name() const {return m_name;}
0084 const std::string& form() const {return m_form;}
0085 tools::uint64 entries() const {return m_entries;}
0086 tools::uint64 pos() const {return m_pos;}
0087 void reset_pos() {m_pos = 0;}
0088
0089 template <class TYPE>
0090 bool write_page(size_t a_size,const TYPE* a_array) {
0091 if(!m_pos) {
0092 if(!write_array<TYPE>(m_group,s_pages(),a_size,a_array,a_size?a_size:32,m_compress)) {
0093 m_out << "pages::write_page : write_array<TYPE>() failed. Pos " << m_pos << std::endl;
0094 return false;
0095 }
0096 m_dataset = toolx_H5Dopen(m_group,s_pages().c_str());
0097 if(m_dataset<0) {
0098 m_out << "pages::write_page : H5Dopen failed. Pos " << m_pos << std::endl;
0099 return false;
0100 }
0101 } else {
0102 if(!write_append_array_dataset<TYPE>(m_dataset,a_size,a_array)) {
0103 m_out << "pages::write_page : write_append_array_dataset<TYPE>() failed. Pos " << m_pos << std::endl;
0104 return false;
0105 }
0106 }
0107 m_pos += a_size;
0108 m_entries = m_pos;
0109 return true;
0110 }
0111
0112 template <class TYPE>
0113 bool read_page(size_t a_size,TYPE* a_array) {
0114 //it is assumed that a_array can contain a_size*sizeof(TYPE) bytes.
0115 unsigned int _size = a_size;
0116 unsigned int n = 0;
0117 TYPE* array = 0;
0118 if(!read_sub_array<TYPE>(m_group,s_pages(),(unsigned int)m_pos,(unsigned int)_size,n,array)) {
0119 m_out << "pages::read_page : read_sub_array<TYPE>() failed." << std::endl;
0120 return false;
0121 }
0122 if(n!=_size) {
0123 m_out << "pages::read_page : size mismatch. Requested " << _size << ", got "<< n << "." << std::endl;
0124 delete [] array;
0125 return false;
0126 }
0127
0128 {TYPE* rpos = (TYPE*)array;
0129 TYPE* wpos = (TYPE*)a_array;
0130 for(size_t i=0;i<n;i++,rpos++,wpos++) *wpos = *rpos;
0131 for(size_t i=n;i<_size;i++,rpos++,wpos++) *wpos = TYPE();}
0132
0133 delete [] array;
0134
0135 m_pos += n;
0136 return true;
0137 }
0138
0139 template <class TYPE>
0140 bool write_vlen(size_t a_size,const TYPE* a_array) {
0141 if(!m_pos) {
0142 if(!hdf5::write_vlen<TYPE>(m_group,s_pages(),a_size,a_array,a_size?a_size:32,m_compress)) {
0143 m_out << "pages::write_vlen : write_vlen<TYPE>() failed. Pos " << m_pos << std::endl;
0144 return false;
0145 }
0146 m_dataset = toolx_H5Dopen(m_group,s_pages().c_str());
0147 if(m_dataset<0) {
0148 m_out << "pages::write_vlen : H5Dopen failed. Pos " << m_pos << std::endl;
0149 return false;
0150 }
0151 } else {
0152 if(!write_append_vlen_dataset<TYPE>(m_dataset,a_size,a_array)) {
0153 m_out << "pages::write_vlen : write_append_vlen_dataset<TYPE>() failed. Pos " << m_pos << std::endl;
0154 return false;
0155 }
0156 }
0157 m_pos++;
0158 m_entries++;
0159 return true;
0160 }
0161
0162 template <class TYPE>
0163 bool read_vlen(size_t& a_size,TYPE*& a_array) {
0164 //it is assumed that a_array can contain a_size*sizeof(TYPE) bytes.
0165 unsigned int sz;
0166 if(!read_sub_vlen<TYPE>(m_group,s_pages(),(unsigned int)m_pos,sz,a_array)) {
0167 m_out << "pages::read_vlen : read_sub_vlen<TYPE>() failed." << std::endl;
0168 a_size = 0;
0169 a_array = 0;
0170 return false;
0171 }
0172 a_size = sz;
0173 m_pos++;
0174 m_entries++;
0175 return true;
0176 }
0177
0178 bool write_string(const std::string& a_string) {
0179 if(!m_pos) {
0180 if(!hdf5::write_string_dataset(m_group,s_pages(),a_string,128,m_compress)) { //32=>enforce extendable.
0181 m_out << "pages::write_string : hdf5::write_string() failed. Pos " << m_pos << std::endl;
0182 return false;
0183 }
0184 m_dataset = toolx_H5Dopen(m_group,s_pages().c_str());
0185 if(m_dataset<0) {
0186 m_out << "pages::write_string : H5Dopen failed. Pos " << m_pos << std::endl;
0187 return false;
0188 }
0189 } else {
0190 if(!write_append_string_dataset(m_dataset,a_string)) {
0191 m_out << "pages::write_string : write_append_string_dataset() failed. Pos " << m_pos << std::endl;
0192 return false;
0193 }
0194 }
0195 m_pos++;
0196 m_entries++;
0197 return true;
0198 }
0199
0200 bool read_string(std::string& a_string) {
0201 if(!read_sub_string(m_group,s_pages(),(unsigned int)m_pos,a_string)) {
0202 m_out << "pages::read_string : read_sub_string() failed." << std::endl;
0203 a_string.clear();
0204 return false;
0205 }
0206 m_pos++;
0207 m_entries++;
0208 return true;
0209 }
0210
0211 protected:
0212 std::ostream& m_out;
0213 std::string m_name;
0214 std::string m_form;
0215 hid_t m_group;
0216 hid_t m_dataset;
0217 bool m_write;
0218 unsigned int m_compress; //if write.
0219 tools::uint64 m_entries;
0220 tools::uint64 m_pos;
0221 };
0222
0223 }}
0224
0225 #endif