Debian Codegraph @

src/thirdparty/PoissonRecon/Streams.h

raw
1 /*
2 Copyright (c) 2022, Michael Kazhdan
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7
8 Redistributions of source code must retain the above copyright notice, this list of
9 conditions and the following disclaimer. Redistributions in binary form must reproduce
10 the above copyright notice, this list of conditions and the following disclaimer
11 in the documentation and/or other materials provided with the distribution.
12
13 Neither the name of the Johns Hopkins University nor the names of its contributors
14 may be used to endorse or promote products derived from this software without specific
15 prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
18 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO THE IMPLIED WARRANTIES
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
20 SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
22 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26 DAMAGE.
27 */
28 #ifndef STREAMS_INCLUDED
29 #define STREAMS_INCLUDED
30
31 #include "Array.h"
32
33 namespace PoissonRecon
34 {
35 //////////////////
36 // BinaryStream //
37 //////////////////
38 struct BinaryStream
39 {
40 size_t ioBytes;
41 BinaryStream( void ) : ioBytes(0) {}
42
43 template< typename C > bool read( C &c ){ return __read( ( Pointer( unsigned char ) )GetPointer( c ) , sizeof(C) ); }
44 template< typename C > bool write( const C &c ){ return __write( ( ConstPointer( unsigned char ) )GetPointer( c ) , sizeof(C) ); }
45 template< typename C > bool write( C &c ){ return __write( ( ConstPointer( unsigned char ) )GetPointer( c ) , sizeof(C) ); }
46 template< typename C > bool read( Pointer( C ) c , size_t sz ){ return __read( ( Pointer( unsigned char ) )c , sizeof(C)*sz ); }
47 template< typename C > bool write( ConstPointer( C ) c , size_t sz ){ return __write( ( ConstPointer( unsigned char ) )c , sizeof(C)*sz ); }
48 template< typename C > bool write( Pointer( C ) c , size_t sz ){ return __write( ( ConstPointer( unsigned char ) )c , sizeof(C)*sz ); }
49
50 template< typename C >
51 bool read( std::vector< std::vector< C > > &c )
52 {
53 size_t sz;
54 if( !read( sz ) ) return false;
55 c.resize( sz );
56 bool ret = true;
57 for( size_t i=0 ; i<sz && ret ; i++ ) ret &= read( c[i] );
58 return ret;
59 }
60
61 template< typename C >
62 bool read( std::vector< C > &c )
63 {
64 size_t sz;
65 if( !read( sz ) ) return false;
66 c.resize( sz );
67 if( sz ) return read( GetPointer( c ) , sz );
68 else return true;
69 }
70
71 template< typename C >
72 bool write( const std::vector< std::vector< C > > &c )
73 {
74 size_t sz = c.size();
75 if( !write( sz ) ) return false;
76 bool ret = true;
77 for( size_t i=0 ; i<sz && ret ; i++ ) ret &= write( c[i] );
78 return ret;
79 }
80
81 template< typename C >
82 bool write( const std::vector< C > &c )
83 {
84 size_t sz = c.size();
85 if( !write( sz ) ) return false;
86 if( sz ) return write( GetPointer( c ) , sz );
87 else return true;
88 }
89
90 template< typename C >
91 bool write( std::vector< std::vector< C > > &c )
92 {
93 size_t sz = c.size();
94 if( !write( sz ) ) return false;
95 bool ret = true;
96 for( size_t i=0 ; i<sz && ret ; i++ ) ret &= write( c[i] );
97 return ret;
98 }
99
100 template< typename C >
101 bool write( std::vector< C > &c )
102 {
103 size_t sz = c.size();
104 if( !write( sz ) ) return false;
105 if( sz ) return write( GetPointer( c ) , sz );
106 else return true;
107 }
108
109 bool read( std::string &str )
110 {
111 size_t sz;
112 if( !read( sz ) ) return false;
113 char *_str = new char[sz+1];
114 bool ret = read( GetPointer( _str , sz+1 ) , sz+1 );
115 if( ret ) str = std::string( _str );
116 delete[] _str;
117 return ret;
118 }
119
120 bool write( const std::string &str )
121 {
122 size_t sz = strlen( str.c_str() );
123 if( !write( sz ) ) return false;
124 return write( GetPointer( str.c_str() , sz+1 ) , sz+1 );
125 }
126
127 bool write( std::string &str )
128 {
129 size_t sz = strlen( str.c_str() );
130 if( !write( sz ) ) return false;
131 return write( GetPointer( str.c_str() , sz+1 ) , sz+1 );
132 }
133 protected:
134 virtual bool _read( Pointer( unsigned char ) ptr , size_t sz ) = 0;
135 virtual bool _write( ConstPointer( unsigned char ) ptr , size_t sz ) = 0;
136 bool _write( Pointer( unsigned char ) ptr , size_t sz ){ return _write( ( ConstPointer( unsigned char ) )ptr , sz ); }
137 bool __read( Pointer( unsigned char ) ptr , size_t sz ){ ioBytes += sz ; return _read( ptr , sz ); }
138 bool __write( ConstPointer( unsigned char ) ptr , size_t sz ){ ioBytes += sz ; return _write( ptr , sz ); }
139 bool __write( Pointer( unsigned char ) ptr , size_t sz ){ ioBytes += sz ; return _write( ( ConstPointer( unsigned char ) )ptr , sz ); }
140 };
141
142 struct FileStream : public BinaryStream
143 {
144 FileStream( FILE *fp ) : _fp(fp){}
145 void reset( void ){ std::rewind( _fp ); }
146 protected:
147 FILE *_fp;
148 bool _read( Pointer( unsigned char ) ptr , size_t sz ){ return fread( ptr , sizeof(unsigned char) , sz , _fp )==sz; }
149 bool _write( ConstPointer( unsigned char ) ptr , size_t sz ){ return fwrite( ptr , sizeof(unsigned char) , sz , _fp )==sz; }
150 };
151
152 template< typename C >
153 struct Serializer
154 {
155 virtual size_t size( void ) const = 0;
156 virtual void serialize( const C & , Pointer( char ) buffer ) const = 0;
157 virtual void deserialize( ConstPointer( char ) buffer , C & ) const = 0;
158 };
159 }
160
161 #endif // STREAMS_INCLUDED