libcommonc++  0.7
MemoryBlock.h++
Go to the documentation of this file.
1 /* ---------------------------------------------------------------------------
2  commonc++ - A C++ Common Class Library
3  Copyright (C) 2005-2014 Mark A Lindner
4 
5  This file is part of commonc++.
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public
18  License along with this library; if not, write to the Free
19  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  ---------------------------------------------------------------------------
21 */
22 
23 #ifndef __ccxx_MemoryBlock_hxx
24 #define __ccxx_MemoryBlock_hxx
25 
26 #include <commonc++/Common.h++>
28 
29 namespace ccxx {
30 
38 {
39  public:
40 
47  MemoryBlock(byte_t* base = NULL, size_t size = 0)
48  : _base(base),
49  _size(size)
50  { }
51 
53  virtual ~MemoryBlock()
54  { }
55 
57  inline void zero()
58  { fill(0); }
59 
61  void fill(byte_t value);
62 
64  inline byte_t* getBase()
65  { return(_base); }
66 
68  inline const byte_t* getBase() const
69  { return(_base); }
70 
72  virtual inline void setBase(byte_t* base)
73  { _base = base; }
74 
76  inline size_t getSize() const
77  { return(_size); }
78 
80  inline void setSize(size_t size)
81  { _size = size; }
82 
84  bool operator==(const MemoryBlock& other) const;
85 
87  inline bool operator!=(const MemoryBlock& other) const
88  { return(! operator==(other)); }
89 
95  byte_t operator[](int index) const;
96 
102  byte_t& operator[](int index);
103 
113  bool copyFrom(const MemoryBlock& other);
114 
124  void moveFrom(MemoryBlock& other);
125 
134  int indexOf(byte_t val, uint_t startIndex = 0) const;
135 
137  inline bool operator!() const
138  { return(_base == NULL); }
139 
140  protected:
141 
145  size_t _size;
146 
147  private:
148 
150 };
151 
152 } // namespace ccxx
153 
154 #endif // __ccxx_MemoryBlock_hxx
virtual void setBase(byte_t *base)
Set the pointer to the beginning of the memory block.
Definition: MemoryBlock.h++:72
byte_t * getBase()
Get a pointer to the beginning of the memory block.
Definition: MemoryBlock.h++:64
An encapsulation of a block of memory.
Definition: MemoryBlock.h++:37
virtual ~MemoryBlock()
Destructor.
Definition: MemoryBlock.h++:53
size_t getSize() const
Get the size of the memory block.
Definition: MemoryBlock.h++:76
void setSize(size_t size)
Set the size of the memory block.
Definition: MemoryBlock.h++:80
bool operator==(const Blob &b1, const Blob &b2)
Definition: Blob.h++:344
void zero()
Zero the memory block.
Definition: MemoryBlock.h++:57
unsigned int uint_t
An alias for unsigned int.
Definition: Integers.h++:74
bool operator!() const
Test if the base of the block is NULL.
Definition: MemoryBlock.h++:137
size_t _size
The size of the memory block, in bytes.
Definition: MemoryBlock.h++:145
#define COMMONCPP_API
Definition: Common.h++:126
MemoryBlock(byte_t *base=NULL, size_t size=0)
Construct a new MemoryBlock.
Definition: MemoryBlock.h++:47
#define CCXX_COPY_DECLS(CLASS)
Inlines declarations of a copy constructor and assignment operator for the class CLASS.
Definition: Common.h++:295
byte_t * _base
A pointer to the beginning of the memory block.
Definition: MemoryBlock.h++:143
bool operator!=(const MemoryBlock &other) const
Inequality operator.
Definition: MemoryBlock.h++:87
Definition: AllocationMap.c++:25
const byte_t * getBase() const
Get a pointer to the beginning of the memory block.
Definition: MemoryBlock.h++:68
unsigned char byte_t
An unsigned 8-bit value.
Definition: Integers.h++:68