File: Buffer.h

package info (click to toggle)
mercator 0.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,008 kB
  • sloc: sh: 10,433; cpp: 4,482; makefile: 115
file content (101 lines) | stat: -rw-r--r-- 3,090 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// This file may be redistributed and modified only under the terms of
// the GNU General Public License (See COPYING for details).
// Copyright (C) 2003 Alistair Riddoch

#ifndef MERCATOR_BUFFER_H
#define MERCATOR_BUFFER_H

namespace Mercator {

class Segment;

/// \brief Template for managing buffers of data for a segment.
template<typename DataType>
class Buffer {
  public:
    /// The terrain height segment this buffer is associated with.
    const Segment & m_segment;
  private:
    /// The number of data values per height point.
    const unsigned int m_channels;
    /// The size of segment, m_res + 1.
    const unsigned int m_size;
    /// Pointer to buffer containing data values.
    DataType * m_data;

  public:
    /// \brief Constructor.
    ///
    /// @param segment terrain height segment this buffer is associated with.
    /// @param channels number of data values per height point.
    explicit Buffer(const Segment & segment, unsigned int channels = 4);
    virtual ~Buffer();

    /// \brief Retrieve the data value at a given point.
    ///
    /// Access the data value associated with given point in the segment
    /// in a given channel in this buffer.
    /// @return a reference to the value at the point requested.
    DataType & operator()(unsigned int x,unsigned int y,unsigned int channel) {
        return m_data[(y * m_size + x) * m_channels + channel];
    }

    /// \brief Retrieve the data value at a given point.
    ///
    /// Return the data value associated with given point in the segment
    /// in a given channel in this buffer.
    /// @return the value at the point requested.
    const DataType & operator()(unsigned int x,
                                unsigned int y,
                                unsigned int channel) const {
        return m_data[(y * m_size + x) * m_channels + channel];
    }
    
    /// Accessor for the terrain height segment this buffer is associated with.
    const Segment & getSegment() const {
        return m_segment;
    }

    /// Accessor for the size of segment, m_res + 1.
    unsigned int getSize() const {
        return m_size;
    }

    /// Accessor for the number of data values per height point.
    unsigned int getChannels() const {
        return m_channels;
    }

    /// Accessor for a pointer to buffer containing data values.
    DataType * getData() {
        return m_data;
    }

    /// \brief Allocate the storage required by the buffer.
    ///
    /// Allocate memory based on the size and number of channels required
    /// by the buffer.
    void allocate() {
        m_data = new DataType[m_size * m_size * m_channels];
    }

    /// \brief Determine if this buffer has valid allocated storage.
    ///
    /// @return true if storage is allocated.
    bool isValid() const {
        return (m_data != 0);
    }

    /// \brief De-allocate the storage for this buffer.
    ///
    /// Free the storage allocate for this buffer.
    void invalidate() {
        delete [] m_data;
        m_data = 0;
    }

};

} // namespace Mercator

#endif // MERCATOR_BUFFER_H