1 //
2 // LOBStream.h
3 //
4 // Library: Data
5 // Package: DataCore
6 // Module:  LOBStream
7 //
8 // Definition of the LOBStream class.
9 //
10 // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
11 // and Contributors.
12 //
13 // SPDX-License-Identifier:	BSL-1.0
14 //
15 
16 
17 #ifndef Data_LOBStream_INCLUDED
18 #define Data_LOBStream_INCLUDED
19 
20 
21 #include "Poco/Foundation.h"
22 #include "Poco/UnbufferedStreamBuf.h"
23 #include "Poco/Data/LOB.h"
24 #include <istream>
25 #include <ostream>
26 
27 
28 namespace Poco {
29 namespace Data {
30 
31 
32 template <typename T>
33 class LOBStreamBuf: public BasicUnbufferedStreamBuf<T, std::char_traits<T>>
34 	/// This is the streambuf class used for reading from and writing to a LOB.
35 {
36 public:
LOBStreamBuf(LOB<T> & lob)37 	LOBStreamBuf(LOB<T>& lob): _lob(lob), _it(_lob.begin())
38 		/// Creates LOBStreamBuf.
39 	{
40 	}
41 
42 
~LOBStreamBuf()43 	~LOBStreamBuf()
44 		/// Destroys LOBStreamBuf.
45 	{
46 	}
47 
48 protected:
49 	using TraitsType = std::char_traits<T>;
50 	using BaseType = BasicUnbufferedStreamBuf<T, TraitsType>;
51 
readFromDevice()52 	typename BaseType::int_type readFromDevice()
53 	{
54 		if (_it != _lob.end())
55 			return BaseType::charToInt(*_it++);
56 		else
57 			return -1;
58 	}
59 
writeToDevice(T c)60 	typename BaseType::int_type writeToDevice(T c)
61 	{
62 		_lob.appendRaw(&c, 1);
63 		return 1;
64 	}
65 
66 private:
67 	LOB<T>& _lob;
68 	typename LOB<T>::Iterator _it;
69 };
70 
71 
72 template <typename T>
73 class LOBIOS: public virtual std::ios
74 	/// The base class for LOBInputStream and
75 	/// LOBOutputStream.
76 	///
77 	/// This class is needed to ensure the correct initialization
78 	/// order of the stream buffer and base classes.
79 {
80 public:
LOBIOS(LOB<T> & lob,openmode mode)81 	LOBIOS(LOB<T>& lob, openmode mode): _buf(lob)
82 		/// Creates the LOBIOS with the given LOB.
83 	{
84 		poco_ios_init(&_buf);
85 	}
86 
~LOBIOS()87 	~LOBIOS()
88 		/// Destroys the LOBIOS.
89 	{
90 	}
91 
rdbuf()92 	LOBStreamBuf<T>* rdbuf()
93 		/// Returns a pointer to the internal LOBStreamBuf.
94 	{
95 		return &_buf;
96 	}
97 
98 protected:
99 	LOBStreamBuf<T> _buf;
100 };
101 
102 
103 template <typename T>
104 class LOBOutputStream: public LOBIOS<T>, public std::basic_ostream<T, std::char_traits<T>>
105 	/// An output stream for writing to a LOB.
106 {
107 public:
LOBOutputStream(LOB<T> & lob)108 	LOBOutputStream(LOB<T>& lob):
109 		LOBIOS<T>(lob, std::ios::out),
110 		std::ostream(LOBIOS<T>::rdbuf())
111 		/// Creates the LOBOutputStream with the given LOB.
112 	{
113 	}
114 
~LOBOutputStream()115 	~LOBOutputStream()
116 		/// Destroys the LOBOutputStream.
117 	{
118 	}
119 };
120 
121 
122 template <typename T>
123 class LOBInputStream: public LOBIOS<T>, public std::basic_istream<T, std::char_traits<T>>
124 	/// An input stream for reading from a LOB.
125 {
126 public:
LOBInputStream(LOB<T> & lob)127 	LOBInputStream(LOB<T>& lob):
128 		LOBIOS<T>(lob, std::ios::in),
129 		std::istream(LOBIOS<T>::rdbuf())
130 		/// Creates the LOBInputStream with the given LOB.
131 	{
132 	}
133 
~LOBInputStream()134 	~LOBInputStream()
135 		/// Destroys the LOBInputStream.
136 	{
137 	}
138 };
139 
140 
141 using BLOBOutputStream = LOBOutputStream<unsigned char>;
142 using CLOBOutputStream = LOBOutputStream<char>;
143 
144 using BLOBInputStream = LOBInputStream<unsigned char>;
145 using CLOBInputStream = LOBInputStream<char>;
146 
147 } } // namespace Poco::Data
148 
149 
150 #endif // Data_LOBStream_INCLUDED
151