source: trunk/nv/interface/data_channel.hh @ 424

Last change on this file since 424 was 424, checked in by epyon, 10 years ago
  • refactoring WIP! (compiles though - warnings on purpose)
File size: 5.5 KB
Line 
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
14namespace 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
71                raw_data_channel() : m_data( nullptr ), m_size( 0 ) {}
72                raw_data_channel( const raw_data_channel& ) = delete;
73                raw_data_channel& operator=( const raw_data_channel& ) = delete;
74
75                uint8*          m_data;
76                uint32          m_size;
77                data_descriptor m_desc;
78        };
79
80        class data_channel_set
81        {
82        public:
83                friend class data_channel_set_creator;
84                friend class raw_data_channel_access;
85                friend class mesh_nodes_creator;
86
87                typedef const raw_data_channel* const_iterator;
88
89                data_channel_set( data_channel_set&& other )
90                {
91                        for ( uint32 c = 0; c < other.m_size; ++c )
92                                m_channels[c] = move( other.m_channels[c] );
93                        m_size      = other.m_size;
94                        m_parent_id = other.m_parent_id;
95                        m_transform = other.m_transform;
96                        m_name      = other.m_name;
97                        other.m_size = 0;
98                }
99
100                data_channel_set& operator=( data_channel_set&& other )
101                {
102                        if ( this != &other )
103                        {
104                                for ( uint32 c = 0; c < other.m_size; ++c )
105                                        m_channels[c] = move( other.m_channels[c] );
106                                m_size      = other.m_size;
107                                m_parent_id = other.m_parent_id;
108                                m_transform = other.m_transform;
109                                m_name      = other.m_name;
110                                other.m_size = 0;
111                        }
112                        return *this;
113                }
114
115                size_t size() const { return m_size; }
116
117                const raw_data_channel* get_channel( size_t index ) const
118                {
119                        if ( m_size > index ) return &m_channels[index];
120                        return nullptr;
121                }
122
123                const raw_data_channel* get_channel( slot s ) const
124                {
125                        for ( uint32 c = 0; c < m_size; ++c )
126                                if ( m_channels[c].has_slot( s ) )
127                                return &m_channels[c];
128                        return nullptr;
129                }
130
131                template < typename Struct >
132                const raw_data_channel* get_channel() const
133                {
134                        data_descriptor compare;
135                        compare.initialize<Struct>();
136                        for ( uint32 c = 0; c < m_size; ++c )
137                        {
138                                if ( m_channels[c].descriptor() == compare )
139                                {
140                                        return &m_channels[c];
141                                }
142                        }
143                        return nullptr;
144                }
145
146                size_t get_channel_size( slot s ) const
147                {
148                        const raw_data_channel* channel = get_channel( s );
149                        return channel ? channel->size() : 0;
150                }
151
152                size_t get_channel_size( size_t channel ) const
153                {
154                        if ( m_size > channel ) return m_channels[channel].size();
155                        return 0;
156                }
157
158                template < typename Struct >
159                size_t get_channel_size() const
160                {
161                        data_descriptor compare;
162                        compare.initialize<Struct>();
163                        for ( uint32 c = 0; c < m_size; ++c )
164                        {
165                                if ( m_channels[c].descriptor() == compare )
166                                {
167                                        return m_channels[c].size();
168                                }
169                        }
170                        return 0;
171                }
172
173                template < typename VTX >
174                const VTX* get_channel_data() const
175                {
176                        data_descriptor compare;
177                        compare.initialize<VTX>();
178                        for ( uint32 c = 0; c < m_size; ++c )
179                        {
180                                if ( m_channels[c].descriptor() == compare )
181                                {
182                                        return m_channels[c].data_cast<VTX>( );
183                                }
184                        }
185                        return nullptr;
186                }
187
188                int get_channel_index( slot s ) const
189                {
190                        for ( uint32 c = 0; c < m_size; ++c )
191                                if ( m_channels[c].has_slot( s ) )
192                                return int( c );
193                        return -1;
194                }
195
196                data_descriptor get_interpolation_key() const
197                {
198                        data_descriptor result;
199                        for ( uint32 c = 0; c < m_size; ++c )
200                        {
201                                for ( const auto& cslot : m_channels[c].descriptor() )
202                                        if ( cslot.vslot != slot::TIME )
203                                        result.push_slot( cslot.etype, cslot.vslot );
204                        }
205                        return result;
206                }
207
208                const_iterator begin() const { return &m_channels[0]; }
209                const_iterator end()   const { return &m_channels[ m_size ]; }
210
211                uint64 get_name() const { return m_name; }
212                sint16 get_parent_id() const { return m_parent_id; }
213                const mat4& get_transform() const { return m_transform; }
214
215        protected:
216
217                data_channel_set()
218                {
219                        m_size      = 0;
220                        m_name      = 0;
221                        m_parent_id = -1;
222                }
223
224                raw_data_channel m_channels[4];
225
226                uint32 m_size;
227                uint64 m_name;
228                sint16 m_parent_id;
229                mat4   m_transform;
230        };
231
232}
233
234#endif // NV_INTERFACE_DATA_CHANNEL_HH
Note: See TracBrowser for help on using the repository browser.