|
Katana Plug-in APIs 0.1
|
00001 // Copyright (c) 2017 The Foundry Visionmongers Ltd. All Rights Reserved. 00002 #ifndef KATANA_PLUGINAPI_FNATTRIBUTE_FNATTRIBUTEBASE_H_ 00003 #define KATANA_PLUGINAPI_FNATTRIBUTE_FNATTRIBUTEBASE_H_ 00004 #include <stdint.h> 00005 #include <cstddef> 00006 #include <string> 00007 #include <utility> 00008 #include <vector> 00009 00010 #include "FnAttribute/FnAttributeAPI.h" 00011 #include "FnAttribute/ns.h" 00012 #include "FnAttribute/suite/FnAttributeSuite.h" 00013 #include "FnPlatform/internal/Portability.h" 00014 #include "FnPluginSystem/FnPluginSystem.h" 00015 00016 #if FNKAT_CXX11 00017 #include <type_traits> 00018 #endif 00019 00020 FNATTRIBUTE_NAMESPACE_ENTER 00021 { 00052 class Attribute; 00053 00054 struct FNATTRIBUTE_API Hash 00055 { 00056 uint64_t hash1; 00057 uint64_t hash2; 00058 00059 std::string str() const; 00060 uint64_t uint64() const { 00061 /* Per spooky hash docs, 00062 * It can produce 64-bit and 32-bit hash values too, at the same speed, 00063 * just use the bottom n bits. 00064 */ 00065 return hash1; 00066 } 00067 00068 Attribute attr() const; 00069 00070 Hash(): hash1(0), hash2(0) 00071 { } 00072 00073 Hash(uint64_t hash1_, uint64_t hash2_): hash1(hash1_), hash2(hash2_) 00074 { } 00075 00076 Hash(FnAttributeHash h) : hash1(h.hash1), hash2(h.hash2) 00077 { } 00078 00079 bool operator==(const Hash & rhs) const 00080 { 00081 return ((hash1 == rhs.hash1) && (hash2 == rhs.hash2)); 00082 } 00083 00084 bool operator!=(const Hash & rhs) const 00085 { 00086 return ((hash1 != rhs.hash1) || (hash2 != rhs.hash2)); 00087 } 00088 00089 bool operator<(const Hash & rhs) const 00090 { 00091 return (hash1 == rhs.hash1) ? (hash2 < rhs.hash2) : (hash1 < rhs.hash1); 00092 } 00093 }; 00094 00098 class FNATTRIBUTE_API Attribute 00099 { 00100 public: 00104 Attribute() : _handle() 00105 { 00106 #if FNKAT_CXX11 00107 static_assert(std::is_standard_layout<Attribute>::value, 00108 "Attribute should be standard layout"); 00109 static_assert(offsetof(FnAttribute::Attribute, _handle) == 0, 00110 "Attribute handle should be first member"); 00111 static_assert( 00112 std::is_same<FnAttributeHandle, decltype(_handle)>::value, 00113 "Attribute handle should be first member"); 00114 #endif 00115 } 00116 00117 ~Attribute() { 00118 if (_handle) 00119 getSuite()->releaseAttr(_handle); 00120 } 00121 00125 bool isValid() const {return _handle != 0x0;} 00126 00145 uint64_t getSize() const { return getSuite()->getSize(getHandle()); } 00146 00150 FnKatAttributeType getType() const { 00151 return getSuite()->getType(getHandle()); 00152 } 00153 00157 std::string getXML() const; 00158 00162 static Attribute parseXML(const char * xml); 00163 00167 void getBinary(std::vector<char> * buffer) const; 00168 00172 static Attribute parseBinary(const char * buffer, size_t size); 00173 00181 bool writeAttributeStream(void *stream, FnAttributeWriteStreamFunc func, FnKatStreamType streamType) const; 00182 00189 static Attribute readAttributeStream(void *stream, FnAttributeReadStreamFunc func, FnKatStreamType streamType); 00190 00191 Hash getHash() const { 00192 return Hash(getSuite()->getHash(getHandle())); 00193 } 00194 00195 Attribute(const Attribute& rhs) : _handle(0x0) 00196 { 00197 acceptHandle(rhs); 00198 } 00199 00200 Attribute& operator=(const Attribute& rhs) 00201 { 00202 acceptHandle(rhs); 00203 return *this; 00204 } 00205 00206 #if FNKAT_CXX11 00207 Attribute(Attribute&& rhs) : _handle(nullptr) 00208 { 00209 moveHandle(std::move(rhs)); 00210 } 00211 00212 Attribute& operator=(Attribute&& rhs) 00213 { 00214 moveHandle(std::move(rhs)); 00215 return *this; 00216 } 00217 #endif // FNKAT_CXX11 00218 00219 friend bool operator==(const Attribute & lhs, const Attribute & rhs); 00220 friend bool operator!=(const Attribute & lhs, const Attribute & rhs); 00221 00223 00224 const FnAttributeHandle& getHandle() const { return _handle; } 00225 00226 FnAttributeHandle getRetainedHandle() const FNKAT_LVALUE_REF_QUALIFIER 00227 { 00228 if (_handle!=0x0) getSuite()->retainAttr(_handle); 00229 return _handle; 00230 } 00231 00232 #if FNKAT_CXX11 00233 FnAttributeHandle getRetainedHandle() && 00234 { 00235 FnAttributeHandle h = _handle; 00236 _handle = nullptr; 00237 return h; 00238 } 00239 #endif // FNKAT_CXX11 00240 00241 static const FnAttributeHostSuite *getSuite() { 00242 return _attrSuite; 00243 } 00244 00245 static FnPlugStatus setHost(FnPluginHost *host); 00246 static void setSuite(const FnAttributeHostSuite *suite); 00247 00248 static Attribute CreateAndSteal(FnAttributeHandle handle) { 00249 return Attribute(handle); 00250 } 00251 00252 static Attribute CreateAndRetain(FnAttributeHandle handle) { 00253 if(handle) 00254 getSuite()->retainAttr(handle); 00255 return Attribute(handle); 00256 } 00257 00258 protected: 00259 Attribute(FnAttributeHandle handle) : _handle(handle) {} 00260 00261 // set internal _handle and grab a reference 00262 void acceptHandle(const Attribute &rhs) 00263 { 00264 getSuite()->retainAttr(rhs._handle); 00265 if (_handle!=0x0) getSuite()->releaseAttr(_handle); 00266 _handle = rhs._handle; 00267 } 00268 00269 // steal the specified handle, and do not increment it. 00270 void stealHandle(const FnAttributeHandle & handle) 00271 { 00272 if (_handle!=0x0) getSuite()->releaseAttr(_handle); 00273 _handle = handle; 00274 } 00275 00276 void clearHandle() 00277 { 00278 if (_handle!=0x0) getSuite()->releaseAttr(_handle); 00279 _handle = 0x0; 00280 } 00281 void checkAndAcceptHandle(const Attribute &rhs, FnKatAttributeType type) 00282 { 00283 if (rhs.isValid() && rhs.getType() == type) 00284 { 00285 acceptHandle(rhs); 00286 } 00287 else 00288 { 00289 clearHandle(); 00290 } 00291 } 00292 #if FNKAT_CXX11 00293 void moveHandle(Attribute&& rhs) 00294 { 00295 if (_handle) { getSuite()->releaseAttr(_handle); } 00296 _handle = rhs._handle; 00297 rhs._handle = nullptr; 00298 } 00299 void checkAndMoveHandle(Attribute&& rhs, FnKatAttributeType type) 00300 { 00301 if (rhs.isValid() && rhs.getType() == type) 00302 { 00303 moveHandle(std::move(rhs)); 00304 } 00305 else 00306 { 00307 clearHandle(); 00308 } 00309 } 00310 #endif // FNKAT_CXX11 00311 00312 private: 00313 static const FnAttributeHostSuite *_attrSuite; 00314 FnAttributeHandle _handle; 00315 00317 }; 00318 00319 inline bool operator==(const Attribute & lhs, const Attribute & rhs) 00320 { 00321 return (0 != lhs.getSuite()->isEqual(lhs.getHandle(), rhs.getHandle())); 00322 } 00323 00324 inline bool operator!=(const Attribute & lhs, const Attribute & rhs) 00325 { 00326 return (!(lhs==rhs)); 00327 } 00328 } 00329 FNATTRIBUTE_NAMESPACE_EXIT 00330 00331 #endif // KATANA_PLUGINAPI_FNATTRIBUTE_FNATTRIBUTEBASE_H_
1.7.3