Klang C++ 0.7.15
Language Reference (draft)
Loading...
Searching...
No Matches
klang::Memory

#include <klang.h>

Public Types

typedef unsigned char Byte
 
typedef Byte * Pointer
 

Public Member Functions

 Memory ()
 
 Memory (size_t size)
 
 ~Memory ()
 
void attach (unsigned char *data, size_t size, bool own=false)
 
void detach ()
 
bool create (size_t size)
 
void clear ()
 
void rewind ()
 
void rewind (int bytes)
 
void skip (int bytes)
 
void free ()
 
bool finished () const
 
FILE * file ()
 
bool load (const char *path)
 
template<typename TYPE >
bool get (TYPE &value)
 
template<typename TYPE >
 operator TYPE ()
 
template<typename TYPE >
bool add (const TYPE &value)
 
template<typename TYPE >
Memory & operator+= (const TYPE &value)
 
template<typename TYPE >
bool operator== (const TYPE &value) const
 
Memory & operator= (const Memory &memory)
 
Memory & operator= (Memory *memory)
 
Memory & operator+= (const Memory &memory)
 

Public Attributes

Pointer start
 
Pointer ptr
 
Pointer end
 
size_t size
 
bool owned
 

Detailed Description

Definition at line 384 of file klang.h.

Member Typedef Documentation

◆ Byte

unsigned char klang::Memory::Byte

Definition at line 386 of file klang.h.

◆ Pointer

Definition at line 387 of file klang.h.

Constructor & Destructor Documentation

◆ Memory() [1/2]

klang::Memory::Memory ( )
inline

Definition at line 393 of file klang.h.

393 {
394 detach();
395 }
void detach()
Definition klang.h:414

References detach().

◆ Memory() [2/2]

klang::Memory::Memory ( size_t size)
inline

Definition at line 397 of file klang.h.

397 {
398 create(size);
399 }
bool create(size_t size)
Definition klang.h:421
size_t size
Definition klang.h:390

References create(), and size.

◆ ~Memory()

klang::Memory::~Memory ( )
inline

Definition at line 401 of file klang.h.

401 {
402 free();
403 }
void free()
Definition klang.h:454

References free().

Member Function Documentation

◆ add()

template<typename TYPE >
bool klang::Memory::add ( const TYPE & value)
inline

Definition at line 518 of file klang.h.

518 {
519 if (ptr + sizeof(TYPE) > end)
520 return false;
521 *(TYPE*)ptr = value;
522 ptr += sizeof(TYPE);
523 return true;
524 }
Pointer end
Definition klang.h:389
Pointer ptr
Definition klang.h:389

References end, and ptr.

Referenced by operator+=().

◆ attach()

void klang::Memory::attach ( unsigned char * data,
size_t size,
bool own = false )
inline

Definition at line 406 of file klang.h.

406 {
408 start = ptr = data;
409 end = start + size;
410 owned = own;
411 }
Pointer start
Definition klang.h:389
bool owned
Definition klang.h:391

References end, owned, ptr, size, and start.

Referenced by operator=().

◆ clear()

void klang::Memory::clear ( )
inline

Definition at line 434 of file klang.h.

434 {
435 memset(start, 0, size);
436 }

References size, and start.

◆ create()

bool klang::Memory::create ( size_t size)
inline

Definition at line 421 of file klang.h.

421 {
422 free();
423 start = ptr = new Byte[size];
424 if (start) {
426 end = start + size;
427 owned = true;
428 return true;
429 }
430 return false;
431 }
unsigned char Byte
Definition klang.h:386

References end, free(), owned, ptr, size, and start.

Referenced by Memory(), load(), and operator=().

◆ detach()

void klang::Memory::detach ( )
inline

Definition at line 414 of file klang.h.

414 {
415 start = ptr = end = nullptr;
416 size = 0;
417 owned = false;
418 }

References end, owned, ptr, size, and start.

Referenced by Memory().

◆ file()

FILE * klang::Memory::file ( )
inline

Definition at line 469 of file klang.h.

469 {
470 FILE* f = fopen("/dev/null", "wt");
471 if (!f) f = fopen("nul", "wt");
472 setvbuf(f, (char*)start, _IOFBF, size);
473 return f;
474 }

References size, and start.

◆ finished()

bool klang::Memory::finished ( ) const
inline

Definition at line 464 of file klang.h.

464 {
465 return ptr >= end;
466 }

References end, and ptr.

Referenced by klang::File::load().

◆ free()

void klang::Memory::free ( )
inline

Definition at line 454 of file klang.h.

454 {
455 if (owned && start) {
456 ptr = end = nullptr;
457 delete[] start;
458 start = nullptr;
459 size = 0;
460 }
461 }

References end, owned, ptr, size, and start.

Referenced by ~Memory(), create(), and operator=().

◆ get()

template<typename TYPE >
bool klang::Memory::get ( TYPE & value)
inline

Definition at line 498 of file klang.h.

498 {
499 if (ptr + sizeof(TYPE) > end) {
500 ptr = end;
501 return false;
502 }
503 value = *(TYPE*)ptr;
504 ptr += sizeof(TYPE);
505 return true;
506 }

References end, and ptr.

Referenced by operator TYPE().

◆ load()

bool klang::Memory::load ( const char * path)
inline

Definition at line 476 of file klang.h.

476 {
477 FILE* f = fopen(path, "rb");
478 if (!f) return false;
479
480 // find out length of file
481 fseek(f, 0, SEEK_END);
482 size_t length = ftell(f);
483 fseek(f, 0, SEEK_SET);
484
485 // allocate memory and read file
486 if (!create(length)) {
487 fclose(f);
488 return false;
489 }
490 fread(start, 1, length, f);
491 fclose(f);
492
493 return true;
494 }

References create(), and start.

Referenced by klang::File::load().

◆ operator TYPE()

template<typename TYPE >
klang::Memory::operator TYPE ( )
inline

Definition at line 510 of file klang.h.

510 {
511 TYPE value;
512 get(value);
513 return value;
514 }
bool get(TYPE &value)
Definition klang.h:498

References get().

◆ operator+=() [1/2]

Memory & klang::Memory::operator+= ( const Memory & memory)
inline

Definition at line 558 of file klang.h.

558 {
559 if (ptr + memory.size > end)
560 return *this;
561 memcpy(ptr, memory.start, memory.size);
562 ptr += memory.size;
563 return *this;
564 }

References end, ptr, size, and start.

◆ operator+=() [2/2]

template<typename TYPE >
Memory & klang::Memory::operator+= ( const TYPE & value)
inline

Definition at line 528 of file klang.h.

528 {
529 add(value);
530 return *this;
531 }
bool add(const TYPE &value)
Definition klang.h:518

References add().

◆ operator=() [1/2]

Memory & klang::Memory::operator= ( const Memory & memory)
inline

Definition at line 542 of file klang.h.

542 {
543 if (ptr + memory.size > end)
544 return *this;
545 create(memory.size);
546 memcpy(start, memory.start, memory.size);
547 return *this;
548 }

References create(), end, ptr, size, and start.

◆ operator=() [2/2]

Memory & klang::Memory::operator= ( Memory * memory)
inline

Definition at line 551 of file klang.h.

551 {
552 free();
553 attach(memory->start, memory->size);
554 return *this;
555 }
void attach(unsigned char *data, size_t size, bool own=false)
Definition klang.h:406

References attach(), free(), size, and start.

◆ operator==()

template<typename TYPE >
bool klang::Memory::operator== ( const TYPE & value) const
inline

Definition at line 535 of file klang.h.

535 {
536 if (ptr + sizeof(TYPE) > end)
537 return false;
538 return value == *(TYPE*)ptr;
539 }

References end, and ptr.

◆ rewind() [1/2]

void klang::Memory::rewind ( )
inline

Definition at line 439 of file klang.h.

439 {
440 ptr = start;
441 }

References ptr, and start.

Referenced by klang::File::load().

◆ rewind() [2/2]

void klang::Memory::rewind ( int bytes)
inline

Definition at line 444 of file klang.h.

444 {
445 ptr -= bytes;
446 }

References ptr.

◆ skip()

void klang::Memory::skip ( int bytes)
inline

Definition at line 449 of file klang.h.

449 {
450 ptr += bytes;
451 }

References ptr.

Referenced by klang::File::load().

Member Data Documentation

◆ end

Pointer klang::Memory::end

◆ owned

bool klang::Memory::owned

Definition at line 391 of file klang.h.

Referenced by attach(), create(), detach(), and free().

◆ ptr

Pointer klang::Memory::ptr

◆ size

size_t klang::Memory::size

Definition at line 390 of file klang.h.

Referenced by Memory(), attach(), clear(), create(), detach(), file(), free(), operator+=(), operator=(), and operator=().

◆ start

Pointer klang::Memory::start

Definition at line 389 of file klang.h.

Referenced by attach(), clear(), create(), detach(), file(), free(), load(), operator+=(), operator=(), operator=(), and rewind().