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