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

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