1 //-*****************************************************************************
2 //
3 // Copyright (c) 2009-2013,
4 //  Sony Pictures Imageworks, Inc. and
5 //  Industrial Light & Magic, a division of Lucasfilm Entertainment Company Ltd.
6 //
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are
11 // met:
12 // *       Redistributions of source code must retain the above copyright
13 // notice, this list of conditions and the following disclaimer.
14 // *       Redistributions in binary form must reproduce the above
15 // copyright notice, this list of conditions and the following disclaimer
16 // in the documentation and/or other materials provided with the
17 // distribution.
18 // *       Neither the name of Sony Pictures Imageworks, nor
19 // Industrial Light & Magic nor the names of their contributors may be used
20 // to endorse or promote products derived from this software without specific
21 // prior written permission.
22 //
23 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 //
35 //-*****************************************************************************
36 
37 #include <Alembic/AbcCoreHDF5/OrData.h>
38 #include <Alembic/AbcCoreHDF5/OrImpl.h>
39 #include <Alembic/AbcCoreHDF5/CprData.h>
40 #include <Alembic/AbcCoreHDF5/CprImpl.h>
41 #include <Alembic/AbcCoreHDF5/ReadUtil.h>
42 #include <Alembic/AbcCoreHDF5/HDF5Util.h>
43 
44 namespace Alembic {
45 namespace AbcCoreHDF5 {
46 namespace ALEMBIC_VERSION_NS {
47 
48 //-*****************************************************************************
VisitAllLinksCB(hid_t iGroup,const char * iName,const H5L_info_t * iLinfo,void * iOpData)49 static herr_t VisitAllLinksCB( hid_t iGroup,
50                                const char *iName,
51                                const H5L_info_t *iLinfo,
52                                void *iOpData )
53 {
54     std::vector<std::string> *visitor = ( std::vector<std::string> * )iOpData;
55 
56     // in the old days ".prop" was the special top compound
57     std::string name = iName;
58     if ( name != ".prop" )
59     {
60         // Create a proto object.
61         visitor->push_back( name );
62     }
63 
64     // Keep iterating!
65     return 0;
66 }
67 
68 //-*****************************************************************************
OrData(ObjectHeaderPtr iHeader,H5Node & iParentGroup,int32_t iArchiveVersion)69 OrData::OrData( ObjectHeaderPtr iHeader,
70                 H5Node & iParentGroup,
71                 int32_t iArchiveVersion )
72     : m_children( NULL )
73 {
74     ABCA_ASSERT( iHeader, "Invalid header" );
75     ABCA_ASSERT( iParentGroup.isValidObject(), "Invalid group" );
76 
77     m_group = OpenGroup( iParentGroup, iHeader->getName().c_str() );
78     ABCA_ASSERT( m_group.isValidObject(),
79         "Could not open object group: "
80         << iHeader->getFullName() );
81 
82     std::vector<std::string> objNames;
83 
84     herr_t status = H5Literate( m_group.getObject(),
85                                 H5_INDEX_CRT_ORDER,
86                                 H5_ITER_INC,
87                                 NULL,
88                                 VisitAllLinksCB,
89                                 ( void * )&objNames );
90 
91     ABCA_ASSERT( status >= 0,
92                  "OrData::OrData: H5Literate failed" );
93 
94     std::vector < std::string >::iterator namesIt;
95     uint32_t i = 0;
96     if ( !objNames.empty() )
97     {
98         m_children = new Child[ objNames.size() ];
99     }
100 
101     std::string parentFullName = iHeader->getFullName();
102     if ( parentFullName != "/" )
103     {
104         parentFullName += "/";
105     }
106 
107     for ( namesIt = objNames.begin(); namesIt != objNames.end();
108           ++namesIt, ++i )
109     {
110         m_childrenMap[ *namesIt ] = i;
111 
112         m_children[i].header.reset( new AbcA::ObjectHeader( *namesIt,
113             parentFullName + *namesIt, AbcA::MetaData() ) );
114         m_children[i].loadedMetaData = false;
115     }
116 
117     m_oldGroup = m_group;
118 
119 
120     m_data = Alembic::Util::shared_ptr<CprData>(
121         new CprData( m_group, iArchiveVersion, ".prop" ) );
122 }
123 
124 //-*****************************************************************************
~OrData()125 OrData::~OrData()
126 {
127     CloseObject( m_oldGroup );
128     delete [] m_children;
129 }
130 
131 //-*****************************************************************************
132 AbcA::CompoundPropertyReaderPtr
getProperties(AbcA::ObjectReaderPtr iParent)133 OrData::getProperties( AbcA::ObjectReaderPtr iParent )
134 {
135     Alembic::Util::scoped_lock l( m_childObjectsMutex );
136     AbcA::CompoundPropertyReaderPtr ret = m_top.lock();
137     if ( ! ret )
138     {
139         // time to make a new one
140         ret = Alembic::Util::shared_ptr<CprImpl>(
141             new CprImpl( iParent, m_data ) );
142 
143         m_top = ret;
144     }
145 
146     return ret;
147 }
148 
149 //-*****************************************************************************
getNumChildren()150 size_t OrData::getNumChildren()
151 {
152     return m_childrenMap.size();
153 }
154 
155 //-*****************************************************************************
156 const AbcA::ObjectHeader &
getChildHeader(AbcA::ObjectReaderPtr iParent,size_t i)157 OrData::getChildHeader( AbcA::ObjectReaderPtr iParent, size_t i )
158 {
159     ABCA_ASSERT( i < m_childrenMap.size(),
160         "Out of range index in OrData::getChildHeader: " << i );
161 
162     Alembic::Util::scoped_lock l( m_childObjectsMutex );
163     if ( ! m_children[i].loadedMetaData )
164     {
165         H5Node group = OpenGroup( m_group,
166             m_children[i].header->getName().c_str() );
167 ;
168         ABCA_ASSERT( group.isValidObject(),
169         "Could not open object group: "
170         << m_children[i].header->getFullName() );
171 
172         ReadMetaData( group, ".prop.meta",
173             m_children[i].header->getMetaData() );
174 
175         CloseObject( group );
176     }
177 
178     return *( m_children[i].header );
179 }
180 
181 //-*****************************************************************************
182 const AbcA::ObjectHeader *
getChildHeader(AbcA::ObjectReaderPtr iParent,const std::string & iName)183 OrData::getChildHeader( AbcA::ObjectReaderPtr iParent,
184                         const std::string &iName )
185 {
186     ChildrenMap::iterator fiter = m_childrenMap.find( iName );
187     if ( fiter == m_childrenMap.end() )
188     {
189         return NULL;
190     }
191 
192     return & getChildHeader( iParent, fiter->second );
193 }
194 
195 //-*****************************************************************************
196 AbcA::ObjectReaderPtr
getChild(AbcA::ObjectReaderPtr iParent,const std::string & iName)197 OrData::getChild( AbcA::ObjectReaderPtr iParent, const std::string &iName )
198 {
199     ChildrenMap::iterator fiter = m_childrenMap.find( iName );
200     if ( fiter == m_childrenMap.end() )
201     {
202         return AbcA::ObjectReaderPtr();
203     }
204 
205     return getChild( iParent, fiter->second );
206 }
207 
208 //-*****************************************************************************
209 AbcA::ObjectReaderPtr
getChild(AbcA::ObjectReaderPtr iParent,size_t i)210 OrData::getChild( AbcA::ObjectReaderPtr iParent, size_t i )
211 {
212     ABCA_ASSERT( i < m_childrenMap.size(),
213         "Out of range index in OrData::getChild: " << i );
214 
215     Alembic::Util::scoped_lock l( m_children[i].lock );
216     AbcA::ObjectReaderPtr optr = m_children[i].made.lock();
217     if ( ! optr )
218     {
219         // we haven't fully loaded the meta data
220         if ( ! m_children[i].loadedMetaData )
221         {
222             getChildHeader( iParent, i );
223         }
224 
225         // Make a new one.
226         optr = Alembic::Util::shared_ptr<OrImpl>(
227             new OrImpl( iParent, m_group, m_children[i].header ) );
228 
229         m_children[i].made = optr;
230     }
231     return optr;
232 }
233 
234 } // End namespace ALEMBIC_VERSION_NS
235 } // End namespace AbcCoreHDF5
236 } // End namespace Alembic
237