fork download
  1. #include <iostream>
  2. #include <kitchen/type.h>
  3.  
  4. namespace kc {
  5.  
  6. class buffer {
  7. private:
  8. buffer(const buffer& other ); // non construction-copyable
  9. buffer& operator=( const buffer& ); // non copyable
  10.  
  11. protected:
  12. uint8 *_buffer;
  13. uint32 _valid;
  14.  
  15. uint32 _size; // Size of the buffer
  16. uint32 _index; // Index into the buffer.
  17.  
  18.  
  19. public:
  20. explicit buffer(); // In concordance with this, and the private directions, a buffer object becomes un-copyable
  21. explicit buffer(uint32 size); // Initialize with a size. But no buffer foo = 1024!
  22. // These are explicit so that someone can't do, say, buffer foo = 1024. We don't want to convert from an int to a buffer! That wouldn't make sense!
  23.  
  24. ~buffer(); // Delete the buffer!
  25.  
  26. uint8* operator*() { return _buffer; } // Returns a pointer to the buffer...just like a pointer! Returns a NULL pointer if the buffer is not valid!!
  27. uint8* operator+(const int offset) { return (_buffer + offset); } // For pointer arithmetic i.e. *(object + 4). This had bounds checking (and returning of NULL) but it got way, WAY TOO SLOW.
  28. operator bool() { return (_valid ? true : false); } // Tests if we are valid or not
  29. operator void*() { return (void *)_buffer; }
  30. operator char*() { return (char *)_buffer; } // Cast this to a uint8*
  31. operator uint8*() { return _buffer; }
  32. operator uint32*() { return (uint32 *)_buffer; }
  33. operator uint64*() { return (uint64 *)_buffer; }
  34.  
  35. uint8 &operator[](int offset) { if (!_valid) { return _buffer[0]; } if (((uint32)offset < _size) && (offset >= 0)) return _buffer[offset]; else { _valid = 0; return _buffer[0]; } } // De-referencing like a pointer
  36. uint32 operator=(uint32 what) { if (!_valid) return 0; if (what > _size) { _valid = 0; return 0; } _index = what; return what; } // Sets the index...has a return to do a = b = c =...
  37. friend std::ostream& operator<<(std::ostream& os, const buffer& me) { if (!me._valid) { os << "INVALID BUFFER"; } else { os << me._buffer; } return os; } // Print us to an ostream...usually cout.
  38.  
  39. uint32 allocate(uint32 size); // Deletes and allocates new buffer
  40. uint32 reallocate(uint32 size); // realloc()'s the buffer
  41. void destroy(); // Destroys the buffer
  42.  
  43. uint32 is_valid() { return _valid; }
  44. uint32 is_full() { if (!_valid) return 0; return ((_index < _size) ? 0 : 1); }
  45. uint32 size() { return _size; }
  46. uint32 bytes_left() { if (!_valid) return 0; return (_size - _index); }
  47. uint8 *start_ptr() { if (!_valid) return NULL; return _buffer; }
  48. uint8 *index_ptr() { if (!_valid) return NULL; return (_buffer + _index); }
  49. uint32 index() { return _index; }
  50. uint32 wrote(uint32 length) { if ((!_valid) || (length == 0)) { return 0; } if ((length + _index) <= _size) { _index += length; return 1; } else { _valid = 0; return 0; } } // Let the class know that we wrote to the buffer
  51. uint32 seek(uint32 whereto) { if (!_valid) { return 0; } if (whereto >= _size) { _valid = 0; return 0; } _index = whereto; return 1; }
  52.  
  53. void force_valid() { _valid = 1; } // Only do this after an unsuccesful copy_into!
  54.  
  55. uint32 copy_into(uint8 *src, const uint32 length); // Copies into the buffer and returns 0 on success
  56. uint32 copy_into(void *src, const uint32 length); // Copies into the buffer and returns 0 on success
  57. uint32 copy_into(buffer& src, const uint32 length); // Copies into another buffer object
  58.  
  59. // Just a few for the easyness
  60. uint32 copy_into(uint32 src);
  61. uint32 copy_into(uint64 src);
  62. uint32 copy_into(std::string& src);
  63.  
  64. uint32 copy_available_into(void *src, const uint32 length);// Copies into the buffer and returns the number of bytes written
  65. uint32 copy_available_into(uint8 *src, const uint32 length);// Copies into the buffer and returns the number of bytes written
  66. uint32 copy_available_into(buffer& src, const uint32 length); // Copies from another buffer object
  67.  
  68. uint32 copy_to(uint8 *dest, const uint32 length); // Copies from the buffer
  69. uint32 copy_to(void *dest, const uint32 length); // Copies
  70.  
  71. uint32 copy_to(uint32 &dest);
  72. uint32 copy_to(uint64 &dest);
  73.  
  74. uint32 copy_available_to(uint8 *dest, const uint32 length);
  75. uint32 copy_available_to(void *dest, const uint32 length);
  76.  
  77. void erase() { _index = 0; } // "erases" the buffer. Note that this is different from zeroing it!
  78. };
  79.  
  80. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:2:26: error: kitchen/type.h: No such file or directory
prog.cpp:12: error: ISO C++ forbids declaration of ‘uint8’ with no type
prog.cpp:12: error: expected ‘;’ before ‘*’ token
prog.cpp:13: error: ‘uint32’ does not name a type
prog.cpp:15: error: ‘uint32’ does not name a type
prog.cpp:16: error: ‘uint32’ does not name a type
prog.cpp:21: error: expected `)' before ‘size’
prog.cpp:26: error: ISO C++ forbids declaration of ‘uint8’ with no type
prog.cpp:26: error: expected ‘;’ before ‘*’ token
prog.cpp:27: error: expected `;' before ‘uint8’
prog.cpp:27: error: ISO C++ forbids declaration of ‘uint8’ with no type
prog.cpp:27: error: expected ‘;’ before ‘*’ token
prog.cpp:28: error: expected `;' before ‘operator’
prog.cpp:31: error: expected type-specifier before ‘uint8’
prog.cpp:32: error: expected type-specifier before ‘uint32’
prog.cpp:33: error: expected type-specifier before ‘uint64’
prog.cpp:35: error: ISO C++ forbids declaration of ‘uint8’ with no type
prog.cpp:35: error: expected ‘;’ before ‘&’ token
prog.cpp:80: error: expected `;' at end of input
prog.cpp:80: error: expected `}' at end of input
prog.cpp: In member function ‘kc::buffer::operator bool()’:
prog.cpp:28: error: ‘_valid’ was not declared in this scope
prog.cpp: In member function ‘kc::buffer::operator void*()’:
prog.cpp:29: error: ‘_buffer’ was not declared in this scope
prog.cpp: In member function ‘kc::buffer::operator char*()’:
prog.cpp:30: error: ‘_buffer’ was not declared in this scope
prog.cpp: At global scope:
prog.cpp:30: error: expected unqualified-id at end of input
prog.cpp:30: error: expected `}' at end of input
stdout
Standard output is empty