libpniio
nxfile.hpp
Go to the documentation of this file.
1 //
2 // (c) Copyright 2011 DESY, Eugen Wintersberger <eugen.wintersberger@desy.de>
3 //
4 // This file is part of libpniio.
5 //
6 // libpniio is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // libpniio is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with libpniio. If not, see <http://www.gnu.org/licenses/>.
18 // ===========================================================================
19 //
20 // Created on: Jun 30, 2011
21 // Author: Eugen Wintersberger <eugen.wintersberger@desy.de>
22 //
23 
24 #pragma once
25 
26 #include <pni/core/types.hpp>
27 #include <pni/core/utilities.hpp>
28 
29 #include "nximp_map.hpp"
30 #include "nxobject_traits.hpp"
31 #include "nxgroup.hpp"
32 #include "nxdate_time.hpp"
33 
34 
35 namespace pni{
36 namespace io{
37 namespace nx{
38 
39  using namespace pni::core;
40  //need this here to avoid name collisions with tango headers.
41  using pni::core::string;
42  using pni::core::exception;
43 
55  template<nximp_code IMPID> class nxfile
56  {
57  public:
62  private:
64  imp_type _imp;
65  public:
66  //===============constructors and destructor========================
70  explicit nxfile():_imp() { }
71 
72  //-----------------------------------------------------------------
76  nxfile(const file_type &file):_imp(file._imp) { }
77 
78  //-----------------------------------------------------------------
82  nxfile(file_type &&f):_imp(std::move(f._imp)) { }
83 
84  //-----------------------------------------------------------------
88  explicit nxfile(imp_type &&imp):_imp(std::move(imp)){ }
89 
90  //-----------------------------------------------------------------
95  {
96  if((_imp.is_valid())&&(!_imp.is_readonly()))
97  {
98  auto r = root();
99  r.attributes.template create<string>("file_update_time",true)
101  r.close();
102  }
103  }
104 
105  //================assigment operator===============================
109  file_type &operator=(const file_type &f)
110  {
111  if(this == &f) return *this;
112 
113  _imp = f._imp;
114 
115  return *this;
116  }
117 
118  //----------------------------------------------------------------
122  file_type &operator=(file_type &&f)
123  {
124  if(this == &f) return *this;
125 
126  _imp = std::move(f._imp);
127  return *this;
128  }
129 
130  //================factory methods==================================
145  static file_type open_file(const string &n,bool ro=true)
146  {
147  return file_type(imp_type::open(n,ro));
148  }
149 
150  //-----------------------------------------------------------------
168  static file_type
169  create_files(const string &n,ssize_t ssize,bool ow=false)
170  {
171  file_type file = file_type(imp_type::create(n,ow,ssize));
172 
173  auto root_group = file.root();
174 
175  //set file specific attributes
176  root_group.attributes.template
177  create<string>("NX_class",true).write(string("NXroot"));
178  root_group.attributes.template
179  create<string>("file_time",true).write(nxdate_time::get_date_time_str());
180  root_group.attributes.template
181  create<string>("file_update_time",true).write(nxdate_time::get_date_time_str());
182  root_group.attributes.template create<string>("file_name",true).write(n);
183 
184  //this should be taken from a configuration
185  root_group.attributes.template
186  create<string>("NeXus_version",true).write(string("4.3.0"));
187 
188  //flush the files content
189  file.flush();
190 
191  return file;
192  }
193 
194  //-----------------------------------------------------------------
208  static file_type
209  create_file(const string &n,bool ow=false)
210  {
211  return create_files(n,0,ow);
212  }
213 
214  //-----------------------------------------------------------------
221  void flush() const{ _imp.flush(); }
222 
223  //-----------------------------------------------------------------
233  bool is_readonly() const { return _imp.is_readonly(); }
234 
235 
236  //-----------------------------------------------------------------
244  void close()
245  {
246  if((this->is_valid())&&(!this->is_readonly()))
247  {
248  auto r = root();
249  r.attributes.template create<string>("file_update_time",true)
251  r.close();
252  }
253 
254  _imp.close();
255  }
256 
257 
258  //------------------------------------------------------------------
271  {
272  typedef typename nximp_map<IMPID>::group_imp group_imp_type;
273  typedef typename nxobject_trait<IMPID>::group_type group_type;
274 
275  return group_type(group_imp_type(_imp.root()));
276  }
277 
278  //----------------------------------------------------------------
286  bool is_valid() const noexcept
287  {
288  return _imp.is_valid();
289  }
290 
291  };
292 
293 
294 //end of namespace
295 }
296 }
297 }
static file_type create_file(const string &n, bool ow=false)
create single file
Definition: nxfile.hpp:209
static pni::core::string get_date_time_str()
date-time now
Definition: nxdate_time.cpp:65
static file_type create_files(const string &n, ssize_t ssize, bool ow=false)
create file family
Definition: nxfile.hpp:169
implementation map
Definition: nximp_map.hpp:50
nexus object traits
Definition: nxobject_traits.hpp:44
STL namespace.
void flush() const
flush the file
Definition: nxfile.hpp:221
nxfile(file_type &&f)
move constructor
Definition: nxfile.hpp:82
imp_type _imp
implementation instance
Definition: nxfile.hpp:64
bool is_readonly() const
check read only
Definition: nxfile.hpp:233
file_type & operator=(const file_type &f)
copy assignment operator
Definition: nxfile.hpp:109
static file_type open_file(const string &n, bool ro=true)
open file
Definition: nxfile.hpp:145
Definition: cbf_reader.hpp:41
nximp_map< IMPID >::file_imp imp_type
implementation type
Definition: nxfile.hpp:59
nxfile< IMPID > file_type
Nexus file type.
Definition: nxfile.hpp:61
bool is_valid(const OTYPE &o) noexcept
visitor checking object validity
Definition: is_valid.hpp:53
File object.
Definition: nxfile.hpp:55
void close()
close the file
Definition: nxfile.hpp:244
nxfile()
default constructor
Definition: nxfile.hpp:70
nxobject_trait< IMPID >::group_type root() const
get root group
Definition: nxfile.hpp:270
~nxfile()
destructor
Definition: nxfile.hpp:94
bool is_valid() const noexcept
check validity
Definition: nxfile.hpp:286
nxfile(imp_type &&imp)
implemenetation move constructor
Definition: nxfile.hpp:88
void write(const OTYPE< IMPID > &o, const ATYPE &a)
write data to an attribute or field
Definition: write.hpp:61
file_type & operator=(file_type &&f)
move assignment operator
Definition: nxfile.hpp:122
nxfile(const file_type &file)
copy constrcutor
Definition: nxfile.hpp:76