]> git.buserror.net Git - polintos/scott/priv.git/blob - idlcomp/tests/cpp-server.cc
Even simpler license.
[polintos/scott/priv.git] / idlcomp / tests / cpp-server.cc
1 #include <stdio.h>
2 #include <System.h>
3
4 using System::IO::File;
5 using namespace System::IO::FileStream_ns;
6 using System::RunTime::Array;
7 using System::RunTime::MutableArray;
8
9
10 namespace Stuff {
11         using System::Notifiers::Notifier;
12
13 class FileStream {
14         File f;
15         uint64_t pos;
16
17 public:
18 #include "server/c++/Stuff/FileStream.h"
19 //#include "sample-cpp-server-decl.h"
20
21         FileStream()
22         {
23                 init_iface();
24         }
25
26         void set_file(File F)
27         {
28                 using System::IO;
29                 printf("this is %p\n", this);
30                 IO::FileStream fs = *this;
31                 IO::IOStream ostr = fs;
32                 IO::FileStream fs2 = IO::FileStream::downcast(ostr);
33                 printf("classptr is %p/%p\n", classptr(fs), classptr(fs2));
34         
35                 if (!F)
36                         printf("File is NULL\n");
37                 else
38                         printf("File is not NULL\n");
39                         
40                 f = F;
41         }
42         
43         void get_file(File *F)
44         {
45                 *F = f;
46         }
47         
48         void seek(int64_t offset, SeekType type)
49         {
50                 if (!f) {
51                         // FIXME: Add a constructor to structs
52                         System::Exceptions::Std::InvalidState err;
53                         throw err;
54                 }
55         
56                 switch (type) {
57                         case SeekType::FromBeginning:
58                                 pos = offset;
59                                 break;
60                         
61                         case SeekType::FromCurrent:
62                                 pos += offset;
63                                 break;
64                         
65                         case SeekType::FromEnd:
66                                 f.size(&pos);
67                                 pos += offset;
68                                 break;
69                 }
70         }
71         
72         void get_pos(uint64_t *POS)
73         {
74                 *POS = pos;
75         }
76
77         void size(uint64_t *size)
78         {
79                 f.size(size);
80         }
81         
82         void read(Array<uint8_t> *buf, uint64_t *len)
83         {
84                 if (!f) {
85                         // throw exc
86                         return;
87                 }
88         
89                 f.read(pos, buf, len);
90                 pos += *len;
91         }
92
93         void read_foo(MutableArray<uint8_t> buf, uint64_t len, Notifier n)
94         {
95                 if (!f) {
96                         // throw exc
97                         return;
98                 }
99         
100                 f.read_async(pos, buf, len, n);
101                 pos += len;
102         }
103
104         void write(Array<uint8_t> buf, uint64_t *len)
105         {
106                 if (!f) {
107                         // throw exc
108                         return;
109                 }
110         
111                 f.write(pos, buf, len);
112                 pos += *len;
113         }
114
115         void write_foo(Array<uint8_t> buf, uint64_t len, Notifier n)
116         {
117                 printf("write_foo, this %p\n", this);
118         
119                 if (!f) {
120                         // throw exc
121                         return;
122                 }
123         
124                 f.write_async(pos, buf, len, n);
125                 pos += len;
126         }
127         
128         void new_timer(::System::Time::Timer *timer)
129         {
130         }
131 };
132 }
133
134 //#include "sample-cpp-server-footer.cc"
135 #include "server/c++/footer.cc"
136
137 #if 0
138 const unsigned long System::IO::_i_IOStream::guid[4] = { 1 };
139 const unsigned long System::IO::_i_IStream::guid[4] = { 2 };
140 const unsigned long System::IO::_i_OStream::guid[4] = { 3 };
141 const unsigned long System::IO::_i_FileStream::guid[4] = { 4 };
142 const unsigned long System::_i_Object::guid[4] = { 5 };
143 #endif
144
145 int main()
146 {
147         Stuff::FileStream *fsimp = new Stuff::FileStream();
148         System::IO::FileStream fs = *fsimp;
149
150         if (!fs)
151                 printf("fs is NULL\n");
152         else {
153                 printf("fs is not NULL\n");
154                 fs.set_file(NULL);
155         }
156         
157         printf("fs is %p\n", (void *)fs);
158         fs.write_async(Array<uint8_t>(NULL, 0), 0, NULL);
159         printf("fs is %p\n", (void *)fs);
160         System::IO::OStream ostr = fs;
161         printf("ostr is %p\n", (void *)ostr);
162         ostr.write_async(Array<uint8_t>(NULL, 0), 0, NULL);
163         System::Object obj = fs;
164         System::IO::FileStream fs2 = System::IO::FileStream::downcast(obj);
165         System::IO::OStream os2 = System::IO::OStream::downcast(obj);
166         printf("obj %p fs2 %p os2 %p\n", (void *)obj, (void *)fs2, (void *)os2);
167         
168         return 0;
169 }