1 | // Copyright (C) 2012-2015 ChaosForge Ltd
|
---|
2 | // http://chaosforge.org/
|
---|
3 | //
|
---|
4 | // This file is part of Nova libraries.
|
---|
5 | // For conditions of distribution and use, see copying.txt file in root folder.
|
---|
6 |
|
---|
7 | #ifndef NV_INTERFACE_DATA_CHANNEL_HH
|
---|
8 | #define NV_INTERFACE_DATA_CHANNEL_HH
|
---|
9 |
|
---|
10 | #include <nv/common.hh>
|
---|
11 | #include <nv/stl/vector.hh>
|
---|
12 | #include <nv/interface/data_descriptor.hh>
|
---|
13 |
|
---|
14 | namespace nv
|
---|
15 | {
|
---|
16 |
|
---|
17 | struct raw_data_channel
|
---|
18 | {
|
---|
19 | raw_data_channel( raw_data_channel&& other )
|
---|
20 | {
|
---|
21 | m_data = other.m_data;
|
---|
22 | m_size = other.m_size;
|
---|
23 | m_desc = other.m_desc;
|
---|
24 | other.m_data = nullptr;
|
---|
25 | other.m_size = 0;
|
---|
26 | }
|
---|
27 |
|
---|
28 | raw_data_channel& operator=( raw_data_channel&& other )
|
---|
29 | {
|
---|
30 | if ( this != &other )
|
---|
31 | {
|
---|
32 | if ( m_data != nullptr ) delete[] m_data;
|
---|
33 | m_data = other.m_data;
|
---|
34 | m_size = other.m_size;
|
---|
35 | m_desc = other.m_desc;
|
---|
36 | other.m_data = nullptr;
|
---|
37 | other.m_size = 0;
|
---|
38 | }
|
---|
39 | return *this;
|
---|
40 | }
|
---|
41 |
|
---|
42 |
|
---|
43 | const data_descriptor& descriptor() const { return m_desc; }
|
---|
44 | uint32 element_size() const { return m_desc.element_size(); }
|
---|
45 | uint32 size() const { return m_size; }
|
---|
46 | uint32 raw_size() const { return m_size * m_desc.element_size(); }
|
---|
47 | const uint8* raw_data() const { return m_data; }
|
---|
48 |
|
---|
49 | // TODO: constexpr compile-time cast check?
|
---|
50 | template < typename Struct >
|
---|
51 | const Struct* data_cast() const
|
---|
52 | {
|
---|
53 | return reinterpret_cast<const Struct*>( m_data );
|
---|
54 | }
|
---|
55 |
|
---|
56 | bool has_slot( slot vslot ) const { return m_desc.has_slot( vslot ); }
|
---|
57 |
|
---|
58 | ~raw_data_channel()
|
---|
59 | {
|
---|
60 | if ( m_data != nullptr ) delete[] m_data;
|
---|
61 | }
|
---|
62 |
|
---|
63 | protected:
|
---|
64 |
|
---|
65 | template < typename Struct >
|
---|
66 | friend class data_channel_access;
|
---|
67 | friend class raw_data_channel_access;
|
---|
68 | friend class data_channel_creator;
|
---|
69 | friend class data_channel_set;
|
---|
70 | friend class key_channel_set_creator;
|
---|
71 |
|
---|
72 | raw_data_channel() : m_data( nullptr ), m_size( 0 ) {}
|
---|
73 | raw_data_channel( const raw_data_channel& ) = delete;
|
---|
74 | raw_data_channel& operator=( const raw_data_channel& ) = delete;
|
---|
75 |
|
---|
76 | uint8* m_data;
|
---|
77 | uint32 m_size;
|
---|
78 | data_descriptor m_desc;
|
---|
79 | };
|
---|
80 |
|
---|
81 | class data_channel_set
|
---|
82 | {
|
---|
83 | public:
|
---|
84 | friend class data_channel_set_creator;
|
---|
85 | friend class raw_data_channel_access;
|
---|
86 |
|
---|
87 | typedef const raw_data_channel* const_iterator;
|
---|
88 |
|
---|
89 | size_t size() const { return m_size; }
|
---|
90 |
|
---|
91 | const raw_data_channel* get_channel( size_t index ) const
|
---|
92 | {
|
---|
93 | if ( m_size > index ) return &m_channels[index];
|
---|
94 | return nullptr;
|
---|
95 | }
|
---|
96 |
|
---|
97 | const raw_data_channel* get_channel( slot s ) const
|
---|
98 | {
|
---|
99 | for ( uint32 c = 0; c < m_size; ++c )
|
---|
100 | if ( m_channels[c].has_slot( s ) )
|
---|
101 | return &m_channels[c];
|
---|
102 | return nullptr;
|
---|
103 | }
|
---|
104 |
|
---|
105 | template < typename Struct >
|
---|
106 | const raw_data_channel* get_channel() const
|
---|
107 | {
|
---|
108 | data_descriptor compare;
|
---|
109 | compare.initialize<Struct>();
|
---|
110 | for ( uint32 c = 0; c < m_size; ++c )
|
---|
111 | {
|
---|
112 | if ( m_channels[c].descriptor() == compare )
|
---|
113 | {
|
---|
114 | return &m_channels[c];
|
---|
115 | }
|
---|
116 | }
|
---|
117 | return nullptr;
|
---|
118 | }
|
---|
119 |
|
---|
120 | size_t get_channel_size( slot s ) const
|
---|
121 | {
|
---|
122 | const raw_data_channel* channel = get_channel( s );
|
---|
123 | return channel ? channel->size() : 0;
|
---|
124 | }
|
---|
125 |
|
---|
126 | size_t get_channel_size( size_t channel ) const
|
---|
127 | {
|
---|
128 | if ( m_size > channel ) return m_channels[channel].size();
|
---|
129 | return 0;
|
---|
130 | }
|
---|
131 |
|
---|
132 | template < typename Struct >
|
---|
133 | size_t get_channel_size() const
|
---|
134 | {
|
---|
135 | data_descriptor compare;
|
---|
136 | compare.initialize<Struct>();
|
---|
137 | for ( uint32 c = 0; c < m_size; ++c )
|
---|
138 | {
|
---|
139 | if ( m_channels[c].descriptor() == compare )
|
---|
140 | {
|
---|
141 | return m_channels[c].size();
|
---|
142 | }
|
---|
143 | }
|
---|
144 | return 0;
|
---|
145 | }
|
---|
146 |
|
---|
147 | template < typename VTX >
|
---|
148 | const VTX* get_channel_data() const
|
---|
149 | {
|
---|
150 | data_descriptor compare;
|
---|
151 | compare.initialize<VTX>();
|
---|
152 | for ( uint32 c = 0; c < m_size; ++c )
|
---|
153 | {
|
---|
154 | if ( m_channels[c].descriptor() == compare )
|
---|
155 | {
|
---|
156 | return m_channels[c].data_cast<VTX>( );
|
---|
157 | }
|
---|
158 | }
|
---|
159 | return nullptr;
|
---|
160 | }
|
---|
161 |
|
---|
162 | int get_channel_index( slot s ) const
|
---|
163 | {
|
---|
164 | for ( uint32 c = 0; c < m_size; ++c )
|
---|
165 | if ( m_channels[c].has_slot( s ) )
|
---|
166 | return int( c );
|
---|
167 | return -1;
|
---|
168 | }
|
---|
169 |
|
---|
170 | const_iterator begin() const { return &m_channels[0]; }
|
---|
171 | const_iterator end() const { return &m_channels[ m_size ]; }
|
---|
172 | protected:
|
---|
173 |
|
---|
174 | data_channel_set()
|
---|
175 | {
|
---|
176 | m_size = 0;
|
---|
177 | }
|
---|
178 | raw_data_channel m_channels[4];
|
---|
179 | uint32 m_size;
|
---|
180 | };
|
---|
181 |
|
---|
182 | }
|
---|
183 |
|
---|
184 | #endif // NV_INTERFACE_DATA_CHANNEL_HH
|
---|