transfer_vector.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2015 The ClanLib Team
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** arising from the use of this software.
8 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries ClanLib may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** Magnus Norddahl
27 */
28 
29 
30 #pragma once
31 
32 #include "transfer_buffer.h"
33 
34 namespace clan
35 {
38 
41 template<typename Type>
43 {
46 public:
49  {
50  }
51 
58  : TransferBuffer(gc, size * sizeof(Type), usage)
59  {
60  }
61 
68  TransferVector(GraphicContext &gc, Type *data, int size, BufferUsage usage = usage_dynamic_copy)
69  : TransferBuffer(gc, data, size * sizeof(Type), usage)
70  {
71  }
72 
73  TransferVector(GraphicContext &gc, const std::vector<Type> &data, BufferUsage usage = usage_dynamic_copy)
74  : TransferBuffer(gc, data.empty() ? (Type*)0 : &data[0], data.size() * sizeof(Type), usage)
75  {
76  }
77 
79  explicit TransferVector(const TransferBuffer &transfer_buffer)
80  : TransferBuffer(transfer_buffer)
81  {
82  }
83 
85 
88 public:
90  Type *get_data() { return reinterpret_cast<Type*>(TransferBuffer::get_data()); }
91 
92  Type &operator[](int index) { return get_data()[index]; }
93  Type &operator[](unsigned int index) { return get_data()[index]; }
95 
98 public:
100  void upload_data(GraphicContext &gc, int offset, const Type *data, int size)
101  {
102  TransferBuffer::upload_data(gc, offset * sizeof(Type), data, size * sizeof(Type));
103  }
104 
106  void upload_data(GraphicContext &gc, int offset, const std::vector<Type> &data)
107  {
108  if (!data.empty())
109  TransferBuffer::upload_data(gc, offset * sizeof(Type), &data[0], data.size() * sizeof(Type));
110  }
112 
115 private:
117 };
118 
119 }
120 
TransferVector(const TransferBuffer &transfer_buffer)
Constructs a TransferVector from an existing buffer.
Definition: transfer_vector.h:79
Type & operator[](int index)
Definition: transfer_vector.h:92
@ usage_dynamic_copy
Definition: buffer_usage.h:50
Type * get_data()
Retrieves a pointer to the mapped buffer.
Definition: transfer_vector.h:90
Transfer Vector.
Definition: transfer_vector.h:43
void upload_data(GraphicContext &gc, int offset, const void *data, int size)
Uploads data to transfer buffer.
void upload_data(GraphicContext &gc, int offset, const Type *data, int size)
Uploads data to transfer buffer.
Definition: transfer_vector.h:100
Transfer Buffer.
Definition: transfer_buffer.h:46
TransferVector(GraphicContext &gc, const std::vector< Type > &data, BufferUsage usage=usage_dynamic_copy)
Definition: transfer_vector.h:73
TransferVector(GraphicContext &gc, Type *data, int size, BufferUsage usage=usage_dynamic_copy)
Constructs a TransferVector.
Definition: transfer_vector.h:68
BufferUsage
Array Buffer usage enum.
Definition: buffer_usage.h:41
Interface to drawing graphics.
Definition: graphic_context.h:258
Definition: clanapp.h:36
TransferVector(GraphicContext &gc, int size, BufferUsage usage=usage_dynamic_copy)
Constructs a TransferVector.
Definition: transfer_vector.h:57
void * get_data()
Retrieves a pointer to the mapped buffer.
Type & operator[](unsigned int index)
Definition: transfer_vector.h:93
TransferVector()
Constructs a null instance.
Definition: transfer_vector.h:48
void upload_data(GraphicContext &gc, int offset, const std::vector< Type > &data)
Uploads data to transfer buffer.
Definition: transfer_vector.h:106