1/*
2Open Asset Import Library (assimp)
3----------------------------------------------------------------------
4
5Copyright (c) 2006-2017, assimp team
6
7All rights reserved.
8
9Redistribution and use of this software in source and binary forms,
10with or without modification, are permitted provided that the
11following 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
27THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37OF 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
52namespace Assimp {
53namespace FBX {
54
55// Forward declarations
56class 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*/
65class Property {
66protected:
67 Property();
68
69public:
70 virtual ~Property();
71
72public:
73 template <typename T>
74 const T* As() const {
75 return dynamic_cast<const T*>(this);
76 }
77};
78
79template<typename T>
80class TypedProperty : public Property {
81public:
82 explicit TypedProperty(const T& value)
83 : value(value) {
84 // empty
85 }
86
87 const T& Value() const {
88 return value;
89 }
90
91private:
92 T value;
93};
94
95
96typedef std::fbx_unordered_map<std::string,std::shared_ptr<Property> > DirectPropertyMap;
97typedef std::fbx_unordered_map<std::string,const Property*> PropertyMap;
98typedef 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 */
103class PropertyTable {
104public:
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
113 const Element* GetElement() const {
114 return element;
115 }
116
117 const PropertyTable* TemplateProps() const {
118 return templateProps.get();
119 }
120
121 DirectPropertyMap GetUnparsedProperties() const;
122
123private:
124 LazyPropertyMap lazyProps;
125 mutable PropertyMap props;
126 const std::shared_ptr<const PropertyTable> templateProps;
127 const Element* const element;
128};
129
130// ------------------------------------------------------------------------------------------------
131template <typename T>
132inline
133T 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// ------------------------------------------------------------------------------------------------
149template <typename T>
150inline
151T 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