MFEM  v4.2.0
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
binaryio.hpp
Go to the documentation of this file.
1 // Copyright (c) 2010-2020, Lawrence Livermore National Security, LLC. Produced
2 // at the Lawrence Livermore National Laboratory. All Rights reserved. See files
3 // LICENSE and NOTICE for details. LLNL-CODE-806117.
4 //
5 // This file is part of the MFEM library. For more information and source code
6 // availability visit https://mfem.org.
7 //
8 // MFEM is free software; you can redistribute it and/or modify it under the
9 // terms of the BSD-3 license. We welcome feedback and contributions, see file
10 // CONTRIBUTING.md for details.
11 
12 #ifndef MFEM_BINARYIO
13 #define MFEM_BINARYIO
14 
15 #include "../config/config.hpp"
16 
17 #include <iostream>
18 #include <vector>
19 
20 namespace mfem
21 {
22 
23 // binary I/O helpers
24 
25 namespace bin_io
26 {
27 
28 /// Write 'value' to stream.
29 template<typename T>
30 inline void write(std::ostream& os, T value)
31 {
32  os.write((char*) &value, sizeof(T));
33 }
34 
35 /// Read a value from the stream and return it.
36 template<typename T>
37 inline T read(std::istream& is)
38 {
39  T value;
40  is.read((char*) &value, sizeof(T));
41  return value;
42 }
43 
44 template <typename T>
45 void AppendBytes(std::vector<char> &vec, const T &val)
46 {
47  const char *ptr = reinterpret_cast<const char*>(&val);
48  vec.insert(vec.end(), ptr, ptr + sizeof(T));
49 }
50 
51 void WriteBase64(std::ostream &out, const void *bytes, size_t length);
52 
53 } // namespace mfem::bin_io
54 
55 } // namespace mfem
56 
57 #endif
void write(std::ostream &os, T value)
Write &#39;value&#39; to stream.
Definition: binaryio.hpp:30
void WriteBase64(std::ostream &out, const void *bytes, size_t nbytes)
Definition: binaryio.cpp:25
T read(std::istream &is)
Read a value from the stream and return it.
Definition: binaryio.hpp:37
void AppendBytes(std::vector< char > &vec, const T &val)
Definition: binaryio.hpp:45
OutStream out(std::cout)
Global stream used by the library for standard output. Initially it uses the same std::streambuf as s...
Definition: globals.hpp:66