Katana Plug-in APIs 0.1

FnAttribute.h

00001 // Copyright (c) 2011 The Foundry Visionmongers Ltd. All Rights Reserved.
00002 
00003 #ifndef FoundryKatanaAttribute_H
00004 #define FoundryKatanaAttribute_H
00005 
00006 #include <stdint.h>
00007 #include <cstring>
00008 #include <stdexcept>
00009 #include <string>
00010 #include <utility>
00011 #include <vector>
00012 
00013 #include "FnPlatform/StringView.h"
00014 #include "FnPlatform/internal/Portability.h"
00015 #include "FnPluginSystem/FnPluginSystem.h"
00016 
00017 #include "FnAttribute/FnAttributeAPI.h"
00018 #include "FnAttribute/FnAttributeBase.h"
00019 #include "FnAttribute/FnConstVector.h"
00020 #include "FnAttribute/FnSampleAccessor.h"
00021 #include "FnAttribute/ns.h"
00022 #include "FnAttribute/suite/FnAttributeSuite.h"
00023 
00024 FNATTRIBUTE_NAMESPACE_ENTER
00025 {
00038     FNATTRIBUTE_API
00039     bool Bootstrap(const std::string& katanaPath);
00040 
00044     FNATTRIBUTE_API
00045     void Initialize(const FnAttributeHostSuite *);
00046 
00070     class FNATTRIBUTE_API DataAttribute : public Attribute
00071     {
00072     public:
00076         DataAttribute() {}
00077 
00081         int64_t getTupleSize() const
00082         {
00083             return getSuite()->getTupleSize(getHandle());
00084         }
00089         int64_t getNumberOfValues() const
00090         {
00091             return getSuite()->getNumberOfValues(getHandle());
00092         }
00096         int64_t getNumberOfTuples() const
00097         {
00098             return getSuite()->getNumberOfTuples(getHandle());
00099         }
00103         int64_t getNumberOfTimeSamples() const
00104         {
00105             return getSuite()->getNumberOfTimeSamples(getHandle());
00106         }
00111         float getSampleTime(int64_t index) const
00112         {
00113             return getSuite()->getSampleTime(getHandle(), index);
00114         }
00115 
00126         bool getBoundingSampleTimes(float *ltime, float *rtime, float sampletime) const
00127         {
00128             return (0 != getSuite()->getBoundingSampleTimes(getHandle(), ltime, rtime, sampletime));
00129         }
00130 
00131         DataAttribute(const Attribute& rhs) : Attribute(0x0)
00132         {
00133             checkAndAcceptDataHandle(rhs);
00134         }
00135 
00136         DataAttribute& operator=(const Attribute& rhs)
00137         {
00138             checkAndAcceptDataHandle(rhs);
00139             return *this;
00140         }
00141 
00142 #if FNKAT_CXX11
00143         DataAttribute(Attribute&& rhs) :  // NOLINT(runtime/explicit)
00144             Attribute(nullptr)
00145         {
00146             checkAndMoveDataHandle(std::move(rhs));
00147         }
00148 
00149         DataAttribute& operator=(Attribute&& rhs)
00150         {
00151             checkAndMoveDataHandle(std::move(rhs));
00152             return *this;
00153         }
00154 #endif  // FNKAT_CXX11
00155 
00157 
00158     protected:
00159         DataAttribute(FnAttributeHandle handle) : Attribute(handle) {}
00160 
00164         static const float kZero[];
00165 
00166     private:
00167         void checkAndAcceptDataHandle(const Attribute &rhs)
00168         {
00169             if (rhs.isValid())
00170             {
00171                 FnKatAttributeType type = rhs.getType();
00172                 if (type == kFnKatAttributeTypeInt    || type == kFnKatAttributeTypeFloat ||
00173                     type == kFnKatAttributeTypeDouble || type == kFnKatAttributeTypeString)
00174                 {
00175                     acceptHandle(rhs);
00176                     return;
00177                 }
00178             }
00179 
00180             clearHandle();
00181         }
00182 #if FNKAT_CXX11
00183         void checkAndMoveDataHandle(Attribute&& rhs)
00184         {
00185             if (rhs.isValid())
00186             {
00187                 const FnKatAttributeType type = rhs.getType();
00188                 if (type == kFnKatAttributeTypeInt    ||
00189                     type == kFnKatAttributeTypeFloat  ||
00190                     type == kFnKatAttributeTypeDouble ||
00191                     type == kFnKatAttributeTypeString)
00192                 {
00193                     moveHandle(std::move(rhs));
00194                     return;
00195                 }
00196             }
00197 
00198             clearHandle();
00199         }
00200 #endif  // FNKAT_CXX11
00201         //@endcond
00202     };
00203 
00207     class FNATTRIBUTE_API NullAttribute : public Attribute
00208     {
00209     public:
00210         NullAttribute() : Attribute(getSuite()->createNullAttr()) {}
00211         NullAttribute(const Attribute& rhs) : Attribute(0x0)
00212         {
00213             checkAndAcceptHandle(rhs, kFnKatAttributeTypeNull);
00214         }
00215 
00216         NullAttribute& operator=(const Attribute& rhs)
00217         {
00218             checkAndAcceptHandle(rhs, kFnKatAttributeTypeNull);
00219             return *this;
00220         }
00221 
00222 #if FNKAT_CXX11
00223         NullAttribute(Attribute&& rhs) :  // NOLINT(runtime/explicit)
00224             Attribute(nullptr)
00225         {
00226             checkAndMoveHandle(std::move(rhs), kFnKatAttributeTypeNull);
00227         }
00228 
00229         NullAttribute& operator=(Attribute&& rhs)
00230         {
00231             checkAndMoveHandle(std::move(rhs), kFnKatAttributeTypeNull);
00232             return *this;
00233         }
00234 #endif  // FNKAT_CXX11
00235 
00236         static FnKatAttributeType getKatAttributeType()
00237         {
00238             return kFnKatAttributeTypeNull;
00239         }
00240     private:
00241         NullAttribute(FnAttributeHandle handle); // NOTE: not implemented
00242     };
00243 
00247     class FNATTRIBUTE_API IntAttribute : public DataAttribute
00248     {
00249     public:
00250         typedef int32_t value_type;
00251         typedef IntConstVectorBase internal_array_type;
00252         typedef IntConstVector array_type;
00253 
00254         typedef SampleAccessor<value_type> accessor_type;
00255         typedef SampleAccessorBase<value_type> internal_accessor_type;
00256         typedef Sample<value_type> sample_type;
00257 
00261         IntAttribute() : DataAttribute() {}
00262 
00267         IntAttribute(value_type value) : DataAttribute(getSuite()->createIntAttr1(value)) {}
00268 
00286         IntAttribute(const value_type* values,
00287                      int64_t valueCount,
00288                      int64_t tupleSize,
00289                      void* context = NULL,
00290                      FnAttributeFreeOwnedDataFunc freeOwnedDataFunc = NULL)
00291             : DataAttribute(
00292                   getSuite()->createIntAttrFactory(DataAttribute::kZero,
00293                                                    1,
00294                                                    &values,
00295                                                    valueCount,
00296                                                    tupleSize,
00297                                                    context,
00298                                                    freeOwnedDataFunc))
00299         {
00300         }
00301 
00329         IntAttribute(const float* times,
00330                      int64_t timeCount,
00331                      const value_type** values,
00332                      int64_t valueCount,
00333                      int64_t tupleSize,
00334                      void* context = NULL,
00335                      FnAttributeFreeOwnedDataFunc freeOwnedDataFunc = NULL)
00336             : DataAttribute(getSuite()->createIntAttrFactory(times,
00337                                                              timeCount,
00338                                                              values,
00339                                                              valueCount,
00340                                                              tupleSize,
00341                                                              context,
00342                                                              freeOwnedDataFunc))
00343         {
00344         }
00345 
00350         static FnKatAttributeType getKatAttributeType()
00351         {
00352             return kFnKatAttributeTypeInt;
00353         }
00354 
00362 #if FNKAT_CXX11
00363         accessor_type getSamples() const & { return accessor_type(*this); }
00364 
00366         accessor_type getSamples() &&
00367         {
00368             return accessor_type(std::move(*this));
00369         }
00371 #else
00372         internal_accessor_type getSamples() const
00373         {
00374             return internal_accessor_type(*this);
00375         }
00376 #endif  // FNKAT_CXX11
00377 
00385 #if FNKAT_CXX11
00386         array_type getNearestSample(float time) const &
00387         {
00388             return array_type(*this, time);
00389         }
00390 
00392         array_type getNearestSample(float time) &&
00393         {
00394             return array_type(std::move(*this), time);
00395         }
00397 #else
00398         internal_array_type getNearestSample(float time) const
00399         {
00400             return internal_array_type(*this, time);
00401         }
00402 #endif  // FNKAT_CXX11
00403 
00413         value_type getValue(const value_type defValue = 0, bool throwOnError=true) const
00414         {
00415             value_type value;
00416             FnAttributeHandle handle = getHandle();
00417             if (getSuite()->getIntValue(&handle, 0.0f, 0, &value))
00418             {
00419                 return value;
00420             }
00421             if (throwOnError)
00422             {
00423                 throw std::runtime_error("Error getting int value from IntAttribute.");
00424             }
00425             else
00426             {
00427                 return defValue;
00428             }
00429         }
00430 
00455         template <typename T, size_t N>
00456         T getValuesAs(const T& defValue = T(), bool throwOnError = true) const
00457         {
00458             accessor_type accessor(this);
00459             if (!accessor.empty() &&
00460                 N <= static_cast<size_t>(accessor.getNumberOfValues()))
00461             {
00462                 const sample_type& sample = accessor.getNearestSample(0.0f);
00463                 return sample.getAs<T, N>(defValue);
00464             }
00465             else if (throwOnError)
00466             {
00467                 throw std::runtime_error(
00468                     "Error getting int values from IntAttribute.");
00469             }
00470             else
00471             {
00472                 return defValue;
00473             }
00474         }
00475 
00476         IntAttribute(const Attribute& rhs) : DataAttribute(0x0)
00477         {
00478             checkAndAcceptHandle(rhs, kFnKatAttributeTypeInt);
00479         }
00480 
00481         IntAttribute& operator=(const Attribute& rhs)
00482         {
00483             checkAndAcceptHandle(rhs, kFnKatAttributeTypeInt);
00484             return *this;
00485         }
00486 
00487 #if FNKAT_CXX11
00488         IntAttribute(Attribute&& rhs) :  // NOLINT(runtime/explicit)
00489             DataAttribute()
00490         {
00491             checkAndMoveHandle(std::move(rhs), kFnKatAttributeTypeInt);
00492         }
00493 
00494         IntAttribute& operator=(Attribute&& rhs)
00495         {
00496             checkAndMoveHandle(std::move(rhs), kFnKatAttributeTypeInt);
00497             return *this;
00498         }
00499 #endif  // FNKAT_CXX11
00500 
00501     private:
00502         IntAttribute(FnAttributeHandle handle); // NOTE: not implemented
00503     };
00504 
00508     class FNATTRIBUTE_API FloatAttribute : public DataAttribute
00509     {
00510     public:
00511         typedef float value_type;
00512         typedef FloatConstVectorBase internal_array_type;
00513         typedef FloatConstVector array_type;
00514 
00515         typedef SampleAccessor<value_type> accessor_type;
00516         typedef SampleAccessorBase<value_type> internal_accessor_type;
00517         typedef Sample<value_type> sample_type;
00518 
00522         FloatAttribute() : DataAttribute() {}
00527         FloatAttribute(value_type value) : DataAttribute(getSuite()->createFloatAttr1(value)) {}
00539         FloatAttribute(const value_type* values,
00540                        int64_t valueCount,
00541                        int64_t tupleSize,
00542                        void* context = NULL,
00543                        FnAttributeFreeOwnedDataFunc freeOwnedDataFunc = NULL)
00544             : DataAttribute(
00545                   getSuite()->createFloatAttrFactory(DataAttribute::kZero,
00546                                                      1,
00547                                                      &values,
00548                                                      valueCount,
00549                                                      tupleSize,
00550                                                      context,
00551                                                      freeOwnedDataFunc))
00552         {
00553         }
00554 
00582         FloatAttribute(const float* times,
00583                        int64_t timeCount,
00584                        const value_type** values,
00585                        int64_t valueCount,
00586                        int64_t tupleSize,
00587                        void* context = NULL,
00588                        FnAttributeFreeOwnedDataFunc freeOwnedDataFunc = NULL)
00589             : DataAttribute(
00590                   getSuite()->createFloatAttrFactory(times,
00591                                                      timeCount,
00592                                                      values,
00593                                                      valueCount,
00594                                                      tupleSize,
00595                                                      context,
00596                                                      freeOwnedDataFunc))
00597         {
00598         }
00599 
00604         static FnKatAttributeType getKatAttributeType()
00605         {
00606             return kFnKatAttributeTypeFloat;
00607         }
00608 
00616 #if FNKAT_CXX11
00617         accessor_type getSamples() const & { return accessor_type(*this); }
00618 
00620         accessor_type getSamples() &&
00621         {
00622             return accessor_type(std::move(*this));
00623         }
00625 #else
00626         internal_accessor_type getSamples() const
00627         {
00628             return internal_accessor_type(*this);
00629         }
00630 #endif  // FNKAT_CXX11
00631 
00639 #if FNKAT_CXX11
00640         array_type getNearestSample(float time) const &
00641         {
00642             return array_type(*this, time);
00643         }
00644 
00646         array_type getNearestSample(float time) &&
00647         {
00648             return array_type(std::move(*this), time);
00649         }
00651 #else
00652         internal_array_type getNearestSample(float time) const
00653         {
00654             return internal_array_type(*this, time);
00655         }
00656 #endif  // FNKAT_CXX11
00657 
00664         void fillInterpSample(value_type * array, int64_t valueCount,
00665             float sampleTime,
00666             const value_type defValue = 0.f, bool throwOnError=true) const;
00667 
00677         value_type getValue(const value_type defValue = 0.f, bool throwOnError=true) const
00678         {
00679             value_type value;
00680             FnAttributeHandle handle = getHandle();
00681             if (getSuite()->getFloatValue(&handle, 0.0f, 0, &value))
00682             {
00683                 return value;
00684             }
00685             if (throwOnError)
00686             {
00687                 throw std::runtime_error("Error getting float value from FloatAttribute.");
00688             }
00689             else
00690             {
00691                 return defValue;
00692             }
00693         }
00694 
00719         template <typename T, size_t N>
00720         T getValuesAs(const T& defValue = T(), bool throwOnError = true) const
00721         {
00722             accessor_type accessor(this);
00723             if (!accessor.empty() &&
00724                 N <= static_cast<size_t>(accessor.getNumberOfValues()))
00725             {
00726                 const sample_type& sample = accessor.getNearestSample(0.0f);
00727                 return sample.getAs<T, N>(defValue);
00728             }
00729             else if (throwOnError)
00730             {
00731                 throw std::runtime_error(
00732                     "Error getting float values from FloatAttribute.");
00733             }
00734             else
00735             {
00736                 return defValue;
00737             }
00738         }
00739 
00740         FloatAttribute(const Attribute& rhs) : DataAttribute(0x0)
00741         {
00742             checkAndAcceptHandle(rhs, kFnKatAttributeTypeFloat);
00743         }
00744 
00745         FloatAttribute& operator=(const Attribute& rhs)
00746         {
00747             checkAndAcceptHandle(rhs, kFnKatAttributeTypeFloat);
00748             return *this;
00749         }
00750 
00751 #if FNKAT_CXX11
00752         FloatAttribute(Attribute&& rhs) :  // NOLINT(runtime/explicit)
00753             DataAttribute()
00754         {
00755             checkAndMoveHandle(std::move(rhs), kFnKatAttributeTypeFloat);
00756         }
00757 
00758         FloatAttribute& operator=(Attribute&& rhs)
00759         {
00760             checkAndMoveHandle(std::move(rhs), kFnKatAttributeTypeFloat);
00761             return *this;
00762         }
00763 #endif  // FNKAT_CXX11
00764 
00765     private:
00766         FloatAttribute(FnAttributeHandle handle); // NOTE: not implemented
00767     };
00768 
00772     class FNATTRIBUTE_API DoubleAttribute : public DataAttribute
00773     {
00774     public:
00775         typedef double value_type;
00776         typedef DoubleConstVectorBase internal_array_type;
00777         typedef DoubleConstVector array_type;
00778 
00779         typedef SampleAccessor<value_type> accessor_type;
00780         typedef SampleAccessorBase<value_type> internal_accessor_type;
00781         typedef Sample<value_type> sample_type;
00782 
00786         DoubleAttribute() : DataAttribute() {}
00791         DoubleAttribute(value_type value) :DataAttribute(getSuite()->createDoubleAttr1(value)) {}
00792 
00804         DoubleAttribute(const value_type* values,
00805                         int64_t valueCount,
00806                         int64_t tupleSize,
00807                         void* context = NULL,
00808                         FnAttributeFreeOwnedDataFunc freeOwnedDataFunc = NULL)
00809             : DataAttribute(
00810                   getSuite()->createDoubleAttrFactory(DataAttribute::kZero,
00811                                                       1,
00812                                                       &values,
00813                                                       valueCount,
00814                                                       tupleSize,
00815                                                       context,
00816                                                       freeOwnedDataFunc))
00817         {
00818         }
00819 
00847         DoubleAttribute(const float* times,
00848                         int64_t timeCount,
00849                         const value_type** values,
00850                         int64_t valueCount,
00851                         int64_t tupleSize,
00852                         void* context = NULL,
00853                         FnAttributeFreeOwnedDataFunc freeOwnedDataFunc = NULL)
00854             : DataAttribute(
00855                   getSuite()->createDoubleAttrFactory(times,
00856                                                       timeCount,
00857                                                       values,
00858                                                       valueCount,
00859                                                       tupleSize,
00860                                                       context,
00861                                                       freeOwnedDataFunc))
00862         {
00863         }
00864 
00869         static FnKatAttributeType getKatAttributeType()
00870         {
00871             return kFnKatAttributeTypeDouble;
00872         }
00873 
00881 #if FNKAT_CXX11
00882         accessor_type getSamples() const & { return accessor_type(*this); }
00883 
00885         accessor_type getSamples() &&
00886         {
00887             return accessor_type(std::move(*this));
00888         }
00890 #else
00891         internal_accessor_type getSamples() const
00892         {
00893             return internal_accessor_type(*this);
00894         }
00895 #endif  // FNKAT_CXX11
00896 
00904 #if FNKAT_CXX11
00905         array_type getNearestSample(float time) const &
00906         {
00907             return array_type(*this, time);
00908         }
00909 
00911         array_type getNearestSample(float time) &&
00912         {
00913             return array_type(std::move(*this), time);
00914         }
00916 #else
00917         internal_array_type getNearestSample(float time) const
00918         {
00919             return internal_array_type(*this, time);
00920         }
00921 #endif  // FNKAT_CXX11
00922 
00929         void fillInterpSample(value_type * array, int64_t valueCount,
00930             float sampleTime,
00931             const value_type defValue = 0.0, bool throwOnError=true) const;
00932 
00942         value_type getValue(const value_type defValue = 0.0, bool throwOnError=true) const
00943         {
00944             value_type value;
00945             FnAttributeHandle handle = getHandle();
00946             if (getSuite()->getDoubleValue(&handle, 0.0f, 0, &value))
00947             {
00948                 return value;
00949             }
00950             if (throwOnError)
00951             {
00952                 throw std::runtime_error("Error getting double value from DoubleAttribute.");
00953             }
00954             else
00955             {
00956                 return defValue;
00957             }
00958         }
00959 
00984         template <typename T, size_t N>
00985         T getValuesAs(const T& defValue = T(), bool throwOnError = true) const
00986         {
00987             accessor_type accessor(this);
00988             if (!accessor.empty() &&
00989                 N <= static_cast<size_t>(accessor.getNumberOfValues()))
00990             {
00991                 const sample_type& sample = accessor.getNearestSample(0.0f);
00992                 return sample.getAs<T, N>(defValue);
00993             }
00994             else if (throwOnError)
00995             {
00996                 throw std::runtime_error(
00997                     "Error getting double values from DoubleAttribute.");
00998             }
00999             else
01000             {
01001                 return defValue;
01002             }
01003         }
01004 
01005         DoubleAttribute(const Attribute& rhs) : DataAttribute(0x0)
01006         {
01007             checkAndAcceptHandle(rhs, kFnKatAttributeTypeDouble);
01008         }
01009 
01010         DoubleAttribute& operator=(const Attribute& rhs)
01011         {
01012             checkAndAcceptHandle(rhs, kFnKatAttributeTypeDouble);
01013             return *this;
01014         }
01015 
01016 #if FNKAT_CXX11
01017         DoubleAttribute(Attribute&& rhs) :  // NOLINT(runtime/explicit)
01018             DataAttribute()
01019         {
01020             checkAndMoveHandle(std::move(rhs), kFnKatAttributeTypeDouble);
01021         }
01022 
01023         DoubleAttribute& operator=(Attribute&& rhs)
01024         {
01025             checkAndMoveHandle(std::move(rhs), kFnKatAttributeTypeDouble);
01026             return *this;
01027         }
01028 #endif  // FNKAT_CXX11
01029 
01030     private:
01031         DoubleAttribute(FnAttributeHandle handle); // NOTE: not implemented
01032     };
01033 
01037     class FNATTRIBUTE_API StringAttribute : public DataAttribute
01038     {
01039     public:
01040         typedef std::string value_type;
01041         typedef StringConstVectorBase internal_array_type;
01042         typedef StringConstVector array_type;
01043 
01044         typedef SampleAccessor<const char*> accessor_type;
01045         typedef SampleAccessorBase<const char*> internal_accessor_type;
01046         typedef Sample<const char*> sample_type;
01047 
01051         StringAttribute() : DataAttribute() {}
01052 
01057         StringAttribute(const std::string & value) : DataAttribute(getSuite()->createStringAttr1(value.c_str())) {}
01058 
01063         StringAttribute(const char * value) : DataAttribute(getSuite()->createStringAttr1(value)) {}
01064 
01072         StringAttribute(const value_type* values,
01073                         int64_t valueCount,
01074                         int64_t tupleSize);
01075 
01082         StringAttribute(const std::vector<std::string> & stringvec, int64_t tupleSize=1);
01083 
01092         StringAttribute(const char ** values, int64_t valueCount, int64_t tupleSize) :
01093             DataAttribute(getSuite()->createStringAttr2(values, valueCount, tupleSize)) {}
01094 
01099         static FnKatAttributeType getKatAttributeType()
01100         {
01101             return kFnKatAttributeTypeString;
01102         }
01103 
01127         StringAttribute(const float* times,
01128                         int64_t timeCount,
01129                         const char*** values,
01130                         int64_t valueCount,
01131                         int64_t tupleSize,
01132                         void* context = NULL,
01133                         FnAttributeFreeOwnedDataFunc freeOwnedDataFunc = NULL)
01134             : DataAttribute(
01135                   getSuite()->createStringAttrFactory(times,
01136                                                       timeCount,
01137                                                       values,
01138                                                       valueCount,
01139                                                       tupleSize,
01140                                                       context,
01141                                                       freeOwnedDataFunc))
01142         {
01143         }
01144 
01152 #if FNKAT_CXX11
01153         accessor_type getSamples() const & { return accessor_type(*this); }
01154 
01156         accessor_type getSamples() &&
01157         {
01158             return accessor_type(std::move(*this));
01159         }
01161 #else
01162         internal_accessor_type getSamples() const
01163         {
01164             return internal_accessor_type(*this);
01165         }
01166 #endif  // FNKAT_CXX11
01167 
01175 #if FNKAT_CXX11
01176         array_type getNearestSample(float time) const &
01177         {
01178             return array_type(*this, time);
01179         }
01180 
01182         array_type getNearestSample(float time) &&
01183         {
01184             return array_type(std::move(*this), time);
01185         }
01187 #else
01188         internal_array_type getNearestSample(float time) const
01189         {
01190             return internal_array_type(*this, time);
01191         }
01192 #endif  // FNKAT_CXX11
01193 
01203         value_type getValue(const value_type& defValue = value_type(),
01204                             bool throwOnError = true) const
01205         {
01206             if (const char* value = getValueCStr(NULL, throwOnError))
01207             {
01208                 return value;
01209             }
01210             else
01211             {
01212                 return defValue;
01213             }
01214         }
01215 
01235         template <typename T, size_t N>
01236         T getValuesAs(const T& defValue = T(), bool throwOnError = true) const
01237         {
01238             accessor_type accessor(this);
01239             if (!accessor.empty() &&
01240                 N <= static_cast<size_t>(accessor.getNumberOfValues()))
01241             {
01242                 const sample_type& sample = accessor.getNearestSample(0.0f);
01243                 return sample.getAs<T, N>(defValue);
01244             }
01245             else if (throwOnError)
01246             {
01247                 throw std::runtime_error(
01248                     "Error getting string values from StringAttribute.");
01249             }
01250             else
01251             {
01252                 return defValue;
01253             }
01254         }
01255 
01268         const char* getValueCStr(const char* defValue = "",
01269                                  bool throwOnError = true) const
01270             FNKAT_LVALUE_REF_QUALIFIER
01271         {
01272             const char* value;
01273             FnAttributeHandle* handlePtr =
01274                 const_cast<FnAttributeHandle*>(&getHandle());
01275             if (getSuite()->getStringValue(handlePtr, 0.0f, 0, &value))
01276             {
01277                 return value;
01278             }
01279             if (throwOnError)
01280             {
01281                 throw std::runtime_error("Error getting string value from StringAttribute.");
01282             }
01283             else
01284             {
01285                 return defValue;
01286             }
01287         }
01288 
01289 #if FNKAT_CXX11
01290 
01291         const char* getValueCStr(const char* defValue = "",
01292                                  bool throwOnError = true) && = delete;
01294 #endif  // FNKAT_CXX11
01295 
01296         bool operator==(const char* str) const {
01297             if (isValid()) {
01298                 int64_t valueCount=0;
01299                 FnAttributeHandle handle = getHandle();
01300                 const char** data = getSuite()->getStringNearestSample(
01301                         handle, 0.f, &valueCount);
01302 
01303                 // Tagged pointer then?
01304                 const char* value[1];
01305                 if (!data && getSuite()->getStringValue(&handle,
01306                         0.0f, 0, &value[0]))
01307                 {
01308                     data = value;
01309                     valueCount = 1;
01310                 }
01311 
01312                 if (valueCount==1) // len>1 arrays should not equal scalars
01313                     return strcmp(data[0], str) == 0;
01314             }
01315             return false;
01316         }
01317 
01318         bool operator!=(const char* str) const {
01319             return !operator==(str);
01320         }
01321 
01322         bool operator==(const std::string& str) const {
01323             return this->operator==(str.c_str());
01324         }
01325 
01326         bool operator!=(const std::string& str) const {
01327             return !operator==(str);
01328         }
01329 
01330         StringAttribute(const Attribute& rhs) : DataAttribute(0x0)
01331         {
01332             checkAndAcceptHandle(rhs, kFnKatAttributeTypeString);
01333         }
01334 
01335         StringAttribute& operator=(const Attribute& rhs)
01336         {
01337             checkAndAcceptHandle(rhs, kFnKatAttributeTypeString);
01338             return *this;
01339         }
01340 
01341 #if FNKAT_CXX11
01342         StringAttribute(Attribute&& rhs) :  // NOLINT(runtime/explicit)
01343             DataAttribute()
01344         {
01345             checkAndMoveHandle(std::move(rhs), kFnKatAttributeTypeString);
01346         }
01347 
01348         StringAttribute& operator=(Attribute&& rhs)
01349         {
01350             checkAndMoveHandle(std::move(rhs), kFnKatAttributeTypeString);
01351             return *this;
01352         }
01353 #endif  // FNKAT_CXX11
01354 
01355     private:
01356         StringAttribute(FnAttributeHandle handle); // NOTE: not implemented
01357     };
01358 
01363     class FNATTRIBUTE_API GroupAttribute : public Attribute
01364     {
01365     public:
01366 
01367         typedef std::pair<std::string, Attribute> NamedAttr_Type;
01368         typedef std::vector<NamedAttr_Type> NamedAttrVector_Type;
01369 
01373         GroupAttribute() : Attribute() {}
01374 
01375         explicit GroupAttribute(const bool groupInherit)
01376             : Attribute(getSuite()->createGroupAttr(NULL, NULL, 0,
01377                                                     (uint8_t)groupInherit))
01378         {
01379         }
01380 
01381         // Note: These constructors do not support delimited child names
01382         // (i.e., no dots in the name). If you wish to construct
01383         // a GroupAttribute with a deep hierarchy, use GroupBuilder instead.
01384 
01385         GroupAttribute(const std::string & name1, const Attribute & attr1,
01386                        const bool groupInherit)  : Attribute(NULL)
01387         {
01388             const char * names[] = { name1.c_str() };
01389             FnAttributeHandle attrs[] = { attr1.getHandle() };
01390             stealHandle(getSuite()->createGroupAttr(
01391                 names, attrs, 1, (uint8_t) groupInherit));
01392         }
01393 
01394         GroupAttribute(const char * name1, const Attribute & attr1,
01395                        const bool groupInherit)  : Attribute(NULL)
01396         {
01397             const char * names[] = { name1 };
01398             FnAttributeHandle attrs[] = { attr1.getHandle() };
01399             stealHandle(getSuite()->createGroupAttr(
01400                 names, attrs, 1, (uint8_t) groupInherit));
01401         }
01402 
01403         GroupAttribute(const std::string & name1, const Attribute & attr1,
01404                        const std::string & name2, const Attribute & attr2,
01405                        const bool groupInherit) : Attribute(NULL)
01406         {
01407             const char * names[] = { name1.c_str(), name2.c_str() };
01408             FnAttributeHandle attrs[] = { attr1.getHandle(), attr2.getHandle() };
01409             stealHandle(getSuite()->createGroupAttr(
01410                 names, attrs, 2, (uint8_t) groupInherit));
01411         }
01412 
01413         GroupAttribute(const char * name1, const Attribute & attr1,
01414                        const char * name2, const Attribute & attr2,
01415                        const bool groupInherit) : Attribute(NULL)
01416         {
01417             const char * names[] = { name1, name2 };
01418             FnAttributeHandle attrs[] = { attr1.getHandle(), attr2.getHandle() };
01419             stealHandle(getSuite()->createGroupAttr(
01420                 names, attrs, 2, (uint8_t) groupInherit));
01421         }
01422 
01423         GroupAttribute(const std::string & name1, const Attribute & attr1,
01424                        const std::string & name2, const Attribute & attr2,
01425                        const std::string & name3, const Attribute & attr3,
01426                        const bool groupInherit) : Attribute(NULL)
01427         {
01428             const char * names[] = { name1.c_str(), name2.c_str(),
01429                 name3.c_str() };
01430             FnAttributeHandle attrs[] = { attr1.getHandle(), attr2.getHandle(),
01431                 attr3.getHandle() };
01432             stealHandle(getSuite()->createGroupAttr(
01433                 names, attrs, 3, (uint8_t) groupInherit));
01434         }
01435 
01436         GroupAttribute(const char * name1, const Attribute & attr1,
01437                        const char * name2, const Attribute & attr2,
01438                        const char * name3, const Attribute & attr3,
01439                        const bool groupInherit) : Attribute(NULL)
01440         {
01441             const char * names[] = { name1, name2, name3 };
01442             FnAttributeHandle attrs[] = { attr1.getHandle(), attr2.getHandle(),
01443                 attr3.getHandle() };
01444             stealHandle(getSuite()->createGroupAttr(
01445                 names, attrs, 3, (uint8_t) groupInherit));
01446         }
01447 
01448         GroupAttribute(const std::string & name1, const Attribute & attr1,
01449                        const std::string & name2, const Attribute & attr2,
01450                        const std::string & name3, const Attribute & attr3,
01451                        const std::string & name4, const Attribute & attr4,
01452                        const bool groupInherit) : Attribute(NULL)
01453         {
01454             const char * names[] = { name1.c_str(), name2.c_str(),
01455                 name3.c_str(), name4.c_str() };
01456             FnAttributeHandle attrs[] = { attr1.getHandle(), attr2.getHandle(),
01457                 attr3.getHandle(), attr4.getHandle() };
01458             stealHandle(getSuite()->createGroupAttr(
01459                 names, attrs, 4, (uint8_t) groupInherit));
01460         }
01461 
01462         GroupAttribute(const char * name1, const Attribute & attr1,
01463                        const char * name2, const Attribute & attr2,
01464                        const char * name3, const Attribute & attr3,
01465                        const char * name4, const Attribute & attr4,
01466                        const bool groupInherit) : Attribute(NULL)
01467         {
01468             const char * names[] = { name1, name2, name3, name4 };
01469             FnAttributeHandle attrs[] = { attr1.getHandle(), attr2.getHandle(),
01470                 attr3.getHandle(), attr4.getHandle() };
01471             stealHandle(getSuite()->createGroupAttr(
01472                 names, attrs, 4, (uint8_t) groupInherit));
01473         }
01474 
01475         GroupAttribute(const std::string & name1, const Attribute & attr1,
01476                        const std::string & name2, const Attribute & attr2,
01477                        const std::string & name3, const Attribute & attr3,
01478                        const std::string & name4, const Attribute & attr4,
01479                        const std::string & name5, const Attribute & attr5,
01480                        const bool groupInherit) : Attribute(NULL)
01481         {
01482             const char * names[] = { name1.c_str(), name2.c_str(),
01483                 name3.c_str(), name4.c_str(), name5.c_str() };
01484             FnAttributeHandle attrs[] = { attr1.getHandle(), attr2.getHandle(),
01485                 attr3.getHandle(), attr4.getHandle(), attr5.getHandle() };
01486             stealHandle(getSuite()->createGroupAttr(
01487                 names, attrs, 5, (uint8_t) groupInherit));
01488         }
01489 
01490         GroupAttribute(const char * name1, const Attribute & attr1,
01491                        const char * name2, const Attribute & attr2,
01492                        const char * name3, const Attribute & attr3,
01493                        const char * name4, const Attribute & attr4,
01494                        const char * name5, const Attribute & attr5,
01495                        const bool groupInherit) : Attribute(NULL)
01496         {
01497             const char * names[] = { name1, name2, name3, name4, name5 };
01498             FnAttributeHandle attrs[] = { attr1.getHandle(), attr2.getHandle(),
01499                 attr3.getHandle(), attr4.getHandle(), attr5.getHandle() };
01500             stealHandle(getSuite()->createGroupAttr(
01501                 names, attrs, 5, (uint8_t) groupInherit));
01502         }
01503 
01504         GroupAttribute(const NamedAttrVector_Type &children,
01505                        const bool inheritChildren);
01506 
01511         static FnKatAttributeType getKatAttributeType()
01512         {
01513             return kFnKatAttributeTypeGroup;
01514         }
01515 
01519         int64_t getNumberOfChildren() const
01520         {
01521             return getSuite()->getNumberOfChildren(getHandle());
01522         }
01523 
01530         std::string getChildName(int64_t index) const
01531         {
01532             int32_t nameSize = 0;
01533             const char *name = getSuite()->getChildNameByIndex(getHandle(), index, &nameSize);
01534             if (nameSize > 0)
01535             {
01536                 return std::string(name, static_cast<std::size_t>(nameSize));
01537             }
01538             return std::string();
01539         }
01540 
01546         const char* getChildNameCStr(int64_t index) const
01547             FNKAT_LVALUE_REF_QUALIFIER
01548         {
01549             int32_t nameSize = 0;
01550             return getChildNameCStr(index, nameSize);
01551         }
01552 
01559         const char* getChildNameCStr(int64_t index, int32_t &namelen) const
01560             FNKAT_LVALUE_REF_QUALIFIER
01561         {
01562             const char *name = getSuite()->getChildNameByIndex(
01563                 getHandle(), index, &namelen);
01564             return name;
01565         }
01566 
01572         FnPlatform::StringView getChildNameStringView(int64_t index) const
01573             FNKAT_LVALUE_REF_QUALIFIER
01574         {
01575             int32_t namelen = 0;
01576             const char* name = getSuite()->getChildNameByIndex(
01577                 getHandle(), index, &namelen);
01578             return FnPlatform::StringView(name, namelen);
01579         }
01580 
01581 #if FNKAT_CXX11
01582 
01583         const char* getChildNameCStr(int64_t index) && = delete;
01584         const char* getChildNameCStr(int64_t index, int32_t &len) && = delete;
01585         FnPlatform::StringView
01586             getChildNameStringView(int64_t index) && = delete;
01588 #endif  // FNKAT_CXX11
01589 
01596         Attribute getChildByIndex(int64_t index) const
01597         {
01598             return Attribute::CreateAndSteal(getSuite()->getChildByIndex(getHandle(), index));
01599         }
01600 
01607         Attribute getChildByName(FnPlatform::StringView name) const
01608         {
01609             return getChildByName(name.data(),
01610                                   static_cast<int32_t>(name.size()));
01611         }
01612 
01620         Attribute getChildByName(const char* name, int32_t length) const
01621         {
01622             return Attribute::CreateAndSteal(
01623                 getSuite()->getChildByName(getHandle(), name, length));
01624         }
01625 
01626         bool getGroupInherit() const
01627         {
01628             return (0 != getSuite()->getGroupInherit(getHandle()));
01629         }
01630 
01631         void fillChildVector(NamedAttrVector_Type * children) const;
01632 
01633         GroupAttribute(const Attribute& rhs) : Attribute(0x0)
01634         {
01635             checkAndAcceptHandle(rhs, kFnKatAttributeTypeGroup);
01636         }
01637 
01638         GroupAttribute& operator=(const Attribute& rhs)
01639         {
01640             checkAndAcceptHandle(rhs, kFnKatAttributeTypeGroup);
01641             return *this;
01642         }
01643 
01644 #if FNKAT_CXX11
01645         GroupAttribute(Attribute&& rhs) :  // NOLINT(runtime/explicit)
01646             Attribute(nullptr)
01647         {
01648             checkAndMoveHandle(std::move(rhs), kFnKatAttributeTypeGroup);
01649         }
01650 
01651         GroupAttribute& operator=(Attribute&& rhs)
01652         {
01653             checkAndMoveHandle(std::move(rhs), kFnKatAttributeTypeGroup);
01654             return *this;
01655         }
01656 #endif  // FNKAT_CXX11
01657 
01658     private:
01659         GroupAttribute(FnAttributeHandle handle); // NOTE: not implemented
01660     };
01661 
01679     FNATTRIBUTE_API
01680     std::string DelimiterEncode(const std::string & str);
01681 
01695     FNATTRIBUTE_API
01696     std::string DelimiterDecode(const std::string & str);
01697 
01701     FNATTRIBUTE_API
01702     uint64_t GetTotalSize();
01703 
01707 }
01708 FNATTRIBUTE_NAMESPACE_EXIT
01709 
01710 #endif // FoundryKatanaAttribute_H
 All Classes Functions Variables Typedefs Enumerations Enumerator