BioDynaMo  v1.05.119-a4ff3934
io.h
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------
2 //
3 // Copyright (C) 2021 CERN & University of Surrey for the benefit of the
4 // BioDynaMo collaboration. All Rights Reserved.
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 //
9 // See the LICENSE file distributed with this work for details.
10 // See the NOTICE file distributed with this work for additional information
11 // regarding copyright ownership.
12 //
13 // -----------------------------------------------------------------------------
14 
15 #ifndef CORE_UTIL_IO_H_
16 #define CORE_UTIL_IO_H_
17 
18 #include <string>
19 #include <vector>
20 
21 #include <TFile.h>
22 #include <TSystem.h>
23 #include "core/util/root.h"
24 
25 namespace bdm {
26 
30  public:
32  // Constructor for ROOT I/O
33  explicit RuntimeVariables(TRootIOCtor* io_ctor);
34 
35  SysInfo_t GetSystemInfo() const;
36  void SetSystemInfo(const SysInfo_t& other);
37 
38  void PrintSystemInfo() const;
39 
40  bool operator==(const RuntimeVariables& other) const;
41 
42  bool operator!=(const RuntimeVariables& other) const;
43 
44  private:
45  SysInfo_t sysinfo_;
46  BDM_CLASS_DEF_NV(RuntimeVariables, 1); // NOLINT
47 };
48 
50 class TFileRaii {
51  public:
52  TFileRaii(const std::string& filename, const char* mode);
53  explicit TFileRaii(TFile* file);
54  ~TFileRaii();
55  TFile* Get();
56 
57  private:
58  TFile* file_;
59 };
60 
63 template <typename T>
65  public:
66  explicit IntegralTypeWrapper(const T& data) : data_(data) {}
67  explicit IntegralTypeWrapper(TRootIOCtor* io_ctor) {}
68  const T& Get() const { return data_; }
69 
70  private:
71  T data_;
73 };
74 
77 template <typename T>
79  public:
80  VectorTypeWrapper() = default;
81 
82  std::vector<T> vector_;
84 };
85 
86 bool FileExists(const std::string& file_name);
87 
88 void WriteToFile(const std::string& filename, const std::string& content);
89 
100 template <typename T>
101 bool GetPersistentObject(const char* root_file, const char* obj_name,
102  T*& empty_obj) { // NOLINT
103  if (FileExists(root_file)) {
104  TFileRaii file(TFile::Open(root_file));
105  file.Get()->GetObject(obj_name, empty_obj);
106  return true;
107  }
108  return false;
109 }
110 
111 // clang-format off
128 // clang-format on
129 template <typename T>
130 void WritePersistentObject(const char* root_file, const char* obj_name,
131  const T& pst_object, const char* mode = "new") {
132  TFileRaii file(root_file, mode);
133  file.Get()->WriteObject(&pst_object, obj_name);
134 }
135 
136 } // namespace bdm
137 
138 #endif // CORE_UTIL_IO_H_
bdm::RuntimeVariables::BDM_CLASS_DEF_NV
BDM_CLASS_DEF_NV(RuntimeVariables, 1)
bdm::RuntimeVariables
Definition: io.h:29
bdm::TFileRaii::Get
TFile * Get()
Definition: io.cc:76
bdm
Definition: agent.cc:39
bdm::TFileRaii::file_
TFile * file_
Definition: io.h:58
bdm::IntegralTypeWrapper
Definition: io.h:64
bdm::RuntimeVariables::operator==
bool operator==(const RuntimeVariables &other) const
Definition: io.cc:43
bdm::TFileRaii
Automatically close a TFile object using RAII pattern.
Definition: io.h:50
bdm::VectorTypeWrapper::vector_
std::vector< T > vector_
Definition: io.h:82
bdm::GetPersistentObject
bool GetPersistentObject(const char *root_file, const char *obj_name, T *&empty_obj)
Gets the persistent object from the specified ROOT file.
Definition: io.h:101
bdm::VectorTypeWrapper::ClassDefNV
ClassDefNV(VectorTypeWrapper, 1)
bdm::RuntimeVariables::RuntimeVariables
RuntimeVariables()
Definition: io.cc:23
bdm::IntegralTypeWrapper::Get
const T & Get() const
Definition: io.h:68
bdm::RuntimeVariables::operator!=
bool operator!=(const RuntimeVariables &other) const
Definition: io.cc:62
bdm::TFileRaii::~TFileRaii
~TFileRaii()
Definition: io.cc:71
bdm::RuntimeVariables::SetSystemInfo
void SetSystemInfo(const SysInfo_t &other)
Definition: io.cc:29
bdm::RuntimeVariables::sysinfo_
SysInfo_t sysinfo_
Definition: io.h:45
bdm::VectorTypeWrapper::VectorTypeWrapper
VectorTypeWrapper()=default
bdm::WriteToFile
void WriteToFile(const std::string &filename, const std::string &content)
Definition: io.cc:83
root.h
bdm::FileExists
bool FileExists(const std::string &file_name)
Definition: io.cc:78
bdm::WritePersistentObject
void WritePersistentObject(const char *root_file, const char *obj_name, const T &pst_object, const char *mode="new")
Writes a persistent object to the specified ROOT file.
Definition: io.h:130
bdm::IntegralTypeWrapper::data_
T data_
Definition: io.h:71
bdm::IntegralTypeWrapper::IntegralTypeWrapper
IntegralTypeWrapper(TRootIOCtor *io_ctor)
Definition: io.h:67
bdm::IntegralTypeWrapper::IntegralTypeWrapper
IntegralTypeWrapper(const T &data)
Definition: io.h:66
bdm::TFileRaii::TFileRaii
TFileRaii(const std::string &filename, const char *mode)
Definition: io.cc:66
bdm::RuntimeVariables::GetSystemInfo
SysInfo_t GetSystemInfo() const
Definition: io.cc:27
bdm::RuntimeVariables::PrintSystemInfo
void PrintSystemInfo() const
Definition: io.cc:33
bdm::VectorTypeWrapper
Definition: io.h:78
bdm::IntegralTypeWrapper::BDM_CLASS_DEF_NV
BDM_CLASS_DEF_NV(IntegralTypeWrapper, 1)