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

Last change on this file since 421 was 421, checked in by epyon, 10 years ago
  • move ops for data_channel_set
  • string_table works on string_views, stores hashes
File size: 5.0 KB
RevLine 
[418]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
86                typedef const raw_data_channel* const_iterator;
87
[421]88                data_channel_set( data_channel_set&& other )
89                {
90                        for ( uint32 c = 0; c < other.m_size; ++c )
91                                m_channels[c] = move( other.m_channels[c] );
92                        m_size = other.m_size;
93                        other.m_size = 0;
94                }
95
96                data_channel_set& operator=( data_channel_set&& other )
97                {
98                        if ( this != &other )
99                        {
100                                for ( uint32 c = 0; c < other.m_size; ++c )
101                                        m_channels[c] = move( other.m_channels[c] );
102                                m_size = other.m_size;
103                                other.m_size = 0;
104                        }
105                        return *this;
106                }
107
[418]108                size_t size() const { return m_size; }
109
110                const raw_data_channel* get_channel( size_t index ) const
111                {
112                        if ( m_size > index ) return &m_channels[index];
113                        return nullptr;
114                }
115
116                const raw_data_channel* get_channel( slot s ) const
117                {
118                        for ( uint32 c = 0; c < m_size; ++c )
119                                if ( m_channels[c].has_slot( s ) )
120                                return &m_channels[c];
121                        return nullptr;
122                }
123
124                template < typename Struct >
125                const raw_data_channel* get_channel() const
126                {
127                        data_descriptor compare;
128                        compare.initialize<Struct>();
129                        for ( uint32 c = 0; c < m_size; ++c )
130                        {
131                                if ( m_channels[c].descriptor() == compare )
132                                {
133                                        return &m_channels[c];
134                                }
135                        }
136                        return nullptr;
137                }
138
139                size_t get_channel_size( slot s ) const
140                {
141                        const raw_data_channel* channel = get_channel( s );
142                        return channel ? channel->size() : 0;
143                }
144
145                size_t get_channel_size( size_t channel ) const
146                {
147                        if ( m_size > channel ) return m_channels[channel].size();
148                        return 0;
149                }
150
151                template < typename Struct >
152                size_t get_channel_size() const
153                {
154                        data_descriptor compare;
155                        compare.initialize<Struct>();
156                        for ( uint32 c = 0; c < m_size; ++c )
157                        {
158                                if ( m_channels[c].descriptor() == compare )
159                                {
160                                        return m_channels[c].size();
161                                }
162                        }
163                        return 0;
164                }
165
166                template < typename VTX >
167                const VTX* get_channel_data() const
168                {
169                        data_descriptor compare;
170                        compare.initialize<VTX>();
171                        for ( uint32 c = 0; c < m_size; ++c )
172                        {
173                                if ( m_channels[c].descriptor() == compare )
174                                {
175                                        return m_channels[c].data_cast<VTX>( );
176                                }
177                        }
178                        return nullptr;
179                }
180
181                int get_channel_index( slot s ) const
182                {
183                        for ( uint32 c = 0; c < m_size; ++c )
184                                if ( m_channels[c].has_slot( s ) )
185                                return int( c );
186                        return -1;
187                }
188
[419]189                data_descriptor get_interpolation_key() const
190                {
191                        data_descriptor result;
192                        for ( uint32 c = 0; c < m_size; ++c )
193                        {
194                                for ( const auto& cslot : m_channels[c].descriptor() )
195                                        if ( cslot.vslot != slot::TIME )
196                                        result.push_slot( cslot.etype, cslot.vslot );
197                        }
198                        return result;
199                }
200
[418]201                const_iterator begin() const { return &m_channels[0]; }
202                const_iterator end()   const { return &m_channels[ m_size ]; }
203        protected:
204
205                data_channel_set()
206                {
207                        m_size = 0;
208                }
209                raw_data_channel m_channels[4];
210                uint32 m_size;
211        };
212
213}
214
215#endif // NV_INTERFACE_DATA_CHANNEL_HH
Note: See TracBrowser for help on using the repository browser.