Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cArchive.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cArchive_H
7 #define _INC_cArchive_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "cStream.h"
13 #include "cUnitTestDecl.h"
14 
15 namespace Gray
16 {
18 
20  {
28  private:
29  const bool m_bStoring;
30  cStreamBase* const m_pStream;
31 
32  public:
34  : m_bStoring(true)
35  , m_pStream(&so)
36  {
37  }
38  cArchive(cStreamInput& si) noexcept
39  : m_bStoring(false)
40  , m_pStream(&si)
41  {
42  }
43  cArchive(cStream& s, bool bStoring) noexcept
44  : m_bStoring(bStoring)
45  , m_pStream(bStoring ? static_cast<cStreamBase*>(static_cast<cStreamOutput*>(&s)) : static_cast<cStreamBase*>(static_cast<cStreamInput*>(&s)))
46  {
47  }
48 
49  bool IsStoring() const noexcept
50  {
53  return m_bStoring;
54  }
55  bool IsLoading() const noexcept
56  {
59  return !m_bStoring;
60  }
61 
63  {
64  ASSERT(IsStoring());
65  return *static_cast<cStreamOutput*>(m_pStream);
66  }
68  {
69  ASSERT(IsLoading());
70  return *static_cast<cStreamInput*>(m_pStream);
71  }
72 
74  HRESULT Serialize(void* pData, size_t nSize);
75  HRESULT SerializeSize(size_t& nSize);
76 
77  HRESULT Write(const void* pData, size_t nSize)
78  {
79  // Emulate MFC. Insert into the archive.
80  ASSERT(IsStoring());
81  return Serialize(const_cast<void*>(pData), nSize);
82  }
83  HRESULT Read(void* pData, size_t nSize)
84  {
85  // Emulate MFC. Extract from the archive.
86  ASSERT(IsLoading());
87  return Serialize(pData, nSize);
88  }
89 
91  {
92  // Emulate MFC. return size_t
93  ASSERT(IsLoading());
94  size_t n;
95  SerializeSize(n);
96  return n;
97  }
98  void WriteCount(size_t n)
99  {
100  // Emulate MFC
101  ASSERT(IsStoring());
102  SerializeSize(n);
103  }
104 
105 #define CTYPE_DEF(a,_TYPE,c,d,e,f,g,h) HRESULT Serialize( _TYPE& Val ) { return Serialize(&Val,sizeof(Val)); } \
106  cArchive& operator << (const _TYPE& Val) { Write(&Val,sizeof(Val)); return *this; } \
107  cArchive& operator >> (_TYPE& Val) { Read(&Val,sizeof(Val)); return *this; }
108 
109 #include "cTypes.tbl"
110 #undef CTYPE_DEF
111 
113  };
114 }
115 
116 #ifndef _MFC_VER // emulate MFC.
118 // #define cArchive Gray::cArchive
119 #define DECLARE_SERIAL(class_name) friend CArchive& GRAYCALL operator>>(CArchive& ar, class_name* &pOb); // _DECLARE_DYNCREATE(class_name)
120 #define IMPLEMENT_SERIAL(class_name,base_name,quant) // external to class.
121 #endif
122 
123 #endif
#define GRAYCORE_LINK
Definition: GrayCore.h:47
MIT License free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to and or sell copies of the and to permit persons to whom the Software is furnished to do so
Definition: LICENSE.txt:10
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
Gray::cArchive CArchive
Definition: cArchive.h:117
#define ASSERT(exp)
Definition: cDebugAssert.h:87
#define UNITTEST2_PREDEF(x)
Definition: cUnitTestDecl.h:19
#define UNITTEST_FRIEND(n)
Define this in the class body to be unit tested. Allow the unit test to access private/protected stuf...
Definition: cUnitTestDecl.h:17
Definition: cArchive.h:20
bool IsLoading() const noexcept
Definition: cArchive.h:55
cStreamOutput & ref_Out()
Definition: cArchive.h:62
bool IsStoring() const noexcept
Definition: cArchive.h:49
void WriteCount(size_t n)
Definition: cArchive.h:98
cArchive(cStream &s, bool bStoring) noexcept
Definition: cArchive.h:43
HRESULT Write(const void *pData, size_t nSize)
Definition: cArchive.h:77
COUNT_t ReadCount()
Definition: cArchive.h:90
cStreamInput & ref_Inp()
Definition: cArchive.h:67
cArchive(cStreamInput &si) noexcept
Definition: cArchive.h:38
HRESULT Read(void *pData, size_t nSize)
Definition: cArchive.h:83
cArchive(cStreamOutput &so) noexcept
Definition: cArchive.h:33
Definition: cStream.h:83
Definition: cStream.h:306
Definition: cStream.h:126
Definition: cStream.h:456
< The main namespace for all Core functions.
Definition: GrayCore.cpp:14
size_t COUNT_t
like size_t but a count of things that might NOT be bytes. ASSUME unsigned. _countof(x)
Definition: Index.h:32
class __DECL_IMPORT cArchive
Definition: cString.h:28