1 /*
2 Open Asset Import Library (assimp)
3 ----------------------------------------------------------------------
4 
5 Copyright (c) 2006-2017, assimp team
6 
7 All rights reserved.
8 
9 Redistribution and use of this software in source and binary forms,
10 with or without modification, are permitted provided that the
11 following conditions are met:
12 
13 * Redistributions of source code must retain the above
14   copyright notice, this list of conditions and the
15   following disclaimer.
16 
17 * Redistributions in binary form must reproduce the above
18   copyright notice, this list of conditions and the
19   following disclaimer in the documentation and/or other
20   materials provided with the distribution.
21 
22 * Neither the name of the assimp team, nor the names of its
23   contributors may be used to endorse or promote products
24   derived from this software without specific prior
25   written permission of the assimp team.
26 
27 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 
39 ----------------------------------------------------------------------
40 */
41 
42 /** @file  FBXProperties.h
43  *  @brief FBX dynamic properties
44  */
45 #ifndef INCLUDED_AI_FBX_PROPERTIES_H
46 #define INCLUDED_AI_FBX_PROPERTIES_H
47 
48 #include "FBXCompileConfig.h"
49 #include <memory>
50 #include <string>
51 
52 namespace Assimp {
53 namespace FBX {
54 
55 // Forward declarations
56 class Element;
57 
58 /** Represents a dynamic property. Type info added by deriving classes,
59  *  see #TypedProperty.
60  Example:
61  @verbatim
62    P: "ShininessExponent", "double", "Number", "",0.5
63  @endvebatim
64 */
65 class Property {
66 protected:
67     Property();
68 
69 public:
70     virtual ~Property();
71 
72 public:
73     template <typename T>
As()74     const T* As() const {
75         return dynamic_cast<const T*>(this);
76     }
77 };
78 
79 template<typename T>
80 class TypedProperty : public Property {
81 public:
TypedProperty(const T & value)82     explicit TypedProperty(const T& value)
83     : value(value) {
84         // empty
85     }
86 
Value()87     const T& Value() const {
88         return value;
89     }
90 
91 private:
92     T value;
93 };
94 
95 
96 typedef std::fbx_unordered_map<std::string,std::shared_ptr<Property> > DirectPropertyMap;
97 typedef std::fbx_unordered_map<std::string,const Property*>            PropertyMap;
98 typedef std::fbx_unordered_map<std::string,const Element*>             LazyPropertyMap;
99 
100 /**
101  *  Represents a property table as can be found in the newer FBX files (Properties60, Properties70)
102  */
103 class PropertyTable {
104 public:
105     // in-memory property table with no source element
106     PropertyTable();
107     PropertyTable(const Element& element, std::shared_ptr<const PropertyTable> templateProps);
108     ~PropertyTable();
109 
110     const Property* Get(const std::string& name) const;
111 
112     // PropertyTable's need not be coupled with FBX elements so this can be NULL
GetElement()113     const Element* GetElement() const {
114         return element;
115     }
116 
TemplateProps()117     const PropertyTable* TemplateProps() const {
118         return templateProps.get();
119     }
120 
121     DirectPropertyMap GetUnparsedProperties() const;
122 
123 private:
124     LazyPropertyMap lazyProps;
125     mutable PropertyMap props;
126     const std::shared_ptr<const PropertyTable> templateProps;
127     const Element* const element;
128 };
129 
130 // ------------------------------------------------------------------------------------------------
131 template <typename T>
132 inline
PropertyGet(const PropertyTable & in,const std::string & name,const T & defaultValue)133 T PropertyGet(const PropertyTable& in, const std::string& name, const T& defaultValue) {
134     const Property* const prop = in.Get(name);
135     if( nullptr == prop) {
136         return defaultValue;
137     }
138 
139     // strong typing, no need to be lenient
140     const TypedProperty<T>* const tprop = prop->As< TypedProperty<T> >();
141     if( nullptr == tprop) {
142         return defaultValue;
143     }
144 
145     return tprop->Value();
146 }
147 
148 // ------------------------------------------------------------------------------------------------
149 template <typename T>
150 inline
PropertyGet(const PropertyTable & in,const std::string & name,bool & result)151 T PropertyGet(const PropertyTable& in, const std::string& name, bool& result) {
152     const Property* const prop = in.Get(name);
153     if( nullptr == prop) {
154         result = false;
155         return T();
156     }
157 
158     // strong typing, no need to be lenient
159     const TypedProperty<T>* const tprop = prop->As< TypedProperty<T> >();
160     if( nullptr == tprop) {
161         result = false;
162         return T();
163     }
164 
165     result = true;
166     return tprop->Value();
167 }
168 
169 } //! FBX
170 } //! Assimp
171 
172 #endif // INCLUDED_AI_FBX_PROPERTIES_H
173