I/O abstract class suggestions

J

jakash3

I've made In and Out abstract base classes which I plan to have File,
Socket, and Console classes base some of their methods on. These
interfaces will have method names like those from the C library as
well as formatted insertion and extraction streams. Please give
feedback and suggestions on how I can add to or improve this
interface.

class In {
public:
virtual int getc() = 0;
virtual char* gets(char* s, int size) = 0;
virtual int scanf(const char* format, ...) = 0;
virtual size_t read(void* ptr, size_t size, size_t nmemb) = 0;
virtual In& operator>> (bool& val) = 0;
virtual In& operator>> (short& val) = 0;
virtual In& operator>> (unsigned short& val) = 0;
virtual In& operator>> (int& val) = 0;
virtual In& operator>> (unsigned int& val) = 0;
virtual In& operator>> (long& val) = 0;
virtual In& operator>> (unsigned long& val) = 0;
virtual In& operator>> (float& val) = 0;
virtual In& operator>> (double& val) = 0;
virtual In& operator>> (long double& val) = 0;
virtual In& operator>> (const void*& val) = 0;
virtual In& operator>> (char& c) = 0;
virtual In& operator>> (signed char& c) = 0;
virtual In& operator>> (unsigned char& c) = 0;
virtual In& operator>> (char*& s) = 0;
virtual In& operator>> (String& s) = 0;
virtual In& operator>> (Charp& s) = 0;
};

class Out {
public:
virtual int putc(int c) = 0;
virtual int puts(const char* s) = 0;
virtual int printf(const char* format, ...) = 0;
virtual size_t write(const void* ptr, size_t size, size_t nmemb) = 0;
virtual Out& operator<< (bool val) = 0;
virtual Out& operator<< (short val) = 0;
virtual Out& operator<< (unsigned short val) = 0;
virtual Out& operator<< (int val) = 0;
virtual Out& operator<< (unsigned int val) = 0;
virtual Out& operator<< (long val) = 0;
virtual Out& operator<< (unsigned long val) = 0;
virtual Out& operator<< (float val) = 0;
virtual Out& operator<< (double val) = 0;
virtual Out& operator<< (long double val) = 0;
virtual Out& operator<< (const void* val) = 0;
virtual Out& operator<< (char c) = 0;
virtual Out& operator<< (signed char c) = 0;
virtual Out& operator<< (unsigned char c) = 0;
virtual Out& operator<< (const char* s) = 0;
virtual Out& operator<< (String s) = 0;
virtual Out& operator<< (Charp s) = 0;
};
 
I

Ian Collins

I've made In and Out abstract base classes which I plan to have File,
Socket, and Console classes base some of their methods on. These
interfaces will have method names like those from the C library as
well as formatted insertion and extraction streams. Please give
feedback and suggestions on how I can add to or improve this
interface.

Why don't you keen the standard iostream objects and create specialised
streambuf objects? The streambuf is where the low level, device
specific IO operations are located.
 
J

jakash3

Why don't you keen the standard iostream objects and create specialised
streambuf objects?  The streambuf is where the low level, device
specific IO operations are located.

You may have a good point, but I'm not very familiar with the C++
library. Well, I guess you could say that I like to reinvent the
wheel.
 
I

Ian Collins

You may have a good point, but I'm not very familiar with the C++
library. Well, I guess you could say that I like to reinvent the
wheel.

The streambuf is designed to be derived from, it has appropriate virtual
methods (underflow, overflow and sync give you most of what's needed) to
support specialisation.

Implementing a streambuf to read from a file descriptor is a great
learning exercise.
 
J

jakash3

@Paavo Helde I'm actually a student still learning and you brought up
very good points. I never thought about specific output formatting
options and locales. Perhaps I should include methods to set the
options for string and number output for streams or maybe even create
variations on the class in different namespaces for different locales.
Btw I forgot to mention that String and Charp types referenced in this
class are a string class and character pointer class respectively.
Charp can be treated like a regular character pointer except there
would be an option to automatically call free to the character pointer
upon destruction. My String class will return it's internal character
pointer when casted as a char ptr or passed to a function that expects
one.
 
J

Jorgen Grahn

....

In my experience, most files are not textual, so all these bells and
whistles wasted on the on-the-fly textual formatting and conversions are
not really worth it IMO.

That varies with your environment, I think. I'm on Unix, and most file
formats there are text. If you design your own, you normally choose
text. Many popular networking protocol are too (since he mentioned
sockets).

/Jorgen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top