00001
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #ifndef MXFLIB__MDTYPE_H
00038 #define MXFLIB__MDTYPE_H
00039
00040
00041 #include <string>
00042 #include <list>
00043 #include <map>
00044
00045 namespace mxflib
00046 {
00047 typedef std::list<std::string> StringList;
00048 }
00049
00050 namespace mxflib
00051 {
00052 enum MDContainerType
00053 {
00054 NONE,
00055 SET,
00056 PACK,
00057 BATCH,
00058 ARRAY
00059 };
00060 enum MDTypeClass
00061 {
00062 BASIC,
00063 INTERPRETATION,
00064 TYPEARRAY,
00065 COMPOUND,
00066 ENUM
00067 };
00068 enum MDArrayClass
00069 {
00070 ARRAYARRAY,
00071 ARRAYBATCH
00072 };
00073 }
00074
00075
00076 namespace mxflib
00077 {
00079
00081 class MapIndex
00082 {
00083 public:
00084 bool IsNum;
00085 UInt32 Number;
00086 std::string String;
00087
00088 public:
00089
00090 MapIndex(UInt32 Num) { IsNum = true; Number = Num; String = Int2String(Num); };
00091 MapIndex(std::string Str) { IsNum = false; String = Str; };
00092 MapIndex(const MapIndex& Map)
00093 { IsNum = Map.IsNum;
00094 if(IsNum) Number = Map.Number;
00095 String = Map.String;
00096 };
00097
00098 bool operator<(const MapIndex& Other) const
00099 {
00100 if((IsNum==true) && (Other.IsNum==true)) return (Number < Other.Number);
00101 if((IsNum==false) && (Other.IsNum==false)) return (String < Other.String);
00102
00103
00104 return Other.IsNum;
00105 }
00106
00108 MapIndex& operator=(UInt32 Num)
00109 {
00110 if(IsNum) { Number = Num; String = Int2String(Num); }
00111 return *this;
00112 };
00113
00115 MapIndex& operator=(std::string Str)
00116 {
00117 if(IsNum)
00118 {
00119 Number = atoi(Str.c_str());
00120 String = Int2String(Number);
00121 }
00122 else
00123 {
00124 String = Str;
00125 }
00126 return *this;
00127 };
00128
00130 MapIndex& operator=(const MapIndex& Map)
00131 {
00132 if(IsNum)
00133 {
00134 Number = atoi(Map.String.c_str());
00135 String = Int2String(Number);
00136 }
00137 else
00138 {
00139 String = Map.String;
00140 }
00141 return *this;
00142 };
00143
00145 const char *c_str() const
00146 {
00147 return (const char *)String.c_str();
00148 }
00149
00151 bool operator==(const MapIndex& Other) const
00152 {
00153 if(Other.IsNum != IsNum) return false;
00154 if(IsNum) return (Number == Other.Number);
00155 return (String == Other.String);
00156 }
00157 };
00158 }
00159
00160
00161 namespace mxflib
00162 {
00163
00164 class MDType;
00165
00167 typedef SmartPtr<MDType> MDTypePtr;
00168
00170 typedef ParentPtr<MDType> MDTypeParent;
00171
00173 typedef std::list<MDTypePtr> MDTypeList;
00174
00176
00177
00178
00179 typedef std::map<std::string, MDTypePtr> MDTypeMap;
00180 }
00181
00182
00183 namespace mxflib
00184 {
00186 class MDType : public RefCount<MDType> , public MDTypeMap
00187 {
00188 protected:
00189 std::string TypeName;
00190 MDTypeClass Class;
00191 MDArrayClass ArrayClass;
00192 MDTraitsPtr Traits;
00193 ULPtr TypeUL;
00194 bool Endian;
00195 TypeRef RefType;
00196 std::string RefTarget;
00197
00198 public:
00200 typedef std::pair<std::string, MDValuePtr> NamedValue;
00201
00203 typedef std::list<NamedValue> NamedValueList;
00204
00205 protected:
00206 NamedValueList EnumValues;
00207
00208 public:
00209 MDTypeParent Base;
00210
00212 StringList ChildOrder;
00213 int Size;
00214
00216
00217
00218 protected:
00220
00223 MDType(std::string TypeName, MDTypeClass TypeClass, ULPtr &UL, MDTraitsPtr TypeTraits)
00224 : TypeName(TypeName), Class(TypeClass), ArrayClass(ARRAYARRAY), Traits(TypeTraits), TypeUL(UL), Endian(false),
00225 RefType(TypeRefUndefined)
00226 { };
00227
00229 MDType();
00230
00232 MDType(const MDType &rhs);
00233
00235 void AddSub(std::string SubName, MDTypePtr SubType);
00236
00237 public:
00239 MDTypePtr EffectiveType(void);
00240
00242 MDTypeClass EffectiveClass(void) const;
00243
00245 MDTypePtr EffectiveBase(void) const;
00246
00248 TypeRef EffectiveRefType(void) const;
00249
00251 std::string EffectiveRefTarget(void) const;
00252
00254
00256 UInt32 EffectiveSize(void) const;
00257
00259
00260 bool HandlesSubdata(void) const
00261 {
00262 if(Traits) return Traits->HandlesSubdata();
00263 return false;
00264 }
00265
00267 void SetEndian(bool Val) { Endian = Val; };
00268
00270 bool GetEndian(void) const { return Endian; };
00271
00273 void SetArrayClass(MDArrayClass Val) { ArrayClass = Val; };
00274
00276 MDArrayClass GetArrayClass(void) { return ArrayClass; };
00277
00279 const std::string &Name(void) const { return TypeName; }
00280
00282 const ULPtr &GetTypeUL(void) const { return TypeUL; }
00283
00285 NamedValueList &GetEnumValues(void) { return EnumValues; }
00286
00288 void SetRefType(TypeRef Val) { RefType = Val; }
00289
00291 TypeRef GetRefType(void) const { return RefType; }
00292
00294 void SetRefTarget(std::string Val) { RefTarget = Val; }
00295
00297 std::string GetRefTarget(void) const { return RefTarget; }
00298
00299
00300
00301
00302 protected:
00303 static MDTypeList Types;
00304
00306 static std::map<UL, MDTypePtr> ULLookup;
00307
00309 static std::map<UL, MDTypePtr> ULLookupVer1;
00310
00312 static MDTypeMap NameLookup;
00313
00314 public:
00316 static MDTypePtr AddBasic(std::string TypeName, ULPtr &UL, int TypeSize);
00317
00319 static MDTypePtr AddInterpretation(std::string TypeName, MDTypePtr BaseType, ULPtr &UL, int Size = 0);
00320
00322 static MDTypePtr AddArray(std::string TypeName, MDTypePtr BaseType, ULPtr &UL, int ArraySize = 0);
00323
00325 static MDTypePtr AddCompound(std::string TypeName, ULPtr &UL);
00326
00328 static MDTypePtr AddEnum(std::string TypeName, MDTypePtr BaseType, ULPtr &UL);
00329
00331 bool AddEnumValue(std::string Name, MDValuePtr &Value);
00332
00334 bool AddEnumValue(std::string Name, std::string Value);
00335
00337 bool AddEnumValue(std::string Name, ULPtr &Value);
00338
00340 static MDTypePtr Find(std::string TypeName, bool SearchAll = false) { return Find(TypeName, MXFLibSymbols, SearchAll); }
00341
00343 static MDTypePtr Find(std::string TypeName, SymbolSpacePtr &SymSpace, bool SearchAll = false);
00344
00346 static MDTypePtr Find(const UL &BaseUL);
00347
00349 static MDTypePtr Find(ULPtr &BaseUL) { return Find(*BaseUL); }
00350
00351
00352
00353
00354
00355 public:
00357 void SetTraits(MDTraitsPtr Tr)
00358 {
00359 Traits = Tr;
00360 };
00361
00363 MDTraitsPtr GetTraits(void) const { return Traits; };
00364
00365 protected:
00367 typedef std::map<std::string, MDTraitsPtr> TraitsMapType;
00368
00370 static TraitsMapType TraitsMap;
00371
00373 typedef std::map<UL, MDTraitsPtr> TraitsULMapType;
00374
00376 static TraitsULMapType TraitsULMap;
00377
00378 protected:
00380 static void AddType(MDTypePtr &Type, ULPtr &TypeUL);
00381
00382 public:
00384
00386 static bool AddTraitsMapping(std::string TypeName, std::string TraitsName);
00387
00389 static bool UpdateTraitsMapping(std::string TypeName, std::string TraitsName)
00390 {
00391
00392 return AddTraitsMapping(TypeName, TraitsName);
00393 }
00394
00396
00398 static bool AddTraitsMapping(const UL &TypeUL, std::string TraitsName);
00399
00401 static bool UpdateTraitsMapping(const UL &TypeUL, std::string TraitsName)
00402 {
00403
00404 return AddTraitsMapping(TypeUL, TraitsName);
00405 }
00406
00408
00410 static MDTraitsPtr LookupTraitsMapping(std::string TypeName, std::string DefaultTraitsName = "");
00411
00413
00415 static MDTraitsPtr LookupTraitsMapping(const UL &TypeUL, const UL &DefaultTraitsUL);
00416
00418
00420 static MDTraitsPtr LookupTraitsMapping(std::string TypeName, const UL &DefaultTraitsUL);
00421
00423
00425 static MDTraitsPtr LookupTraitsMapping(const UL &TypeUL, std::string DefaultTraitsName = "");
00426
00427
00428 friend class MDValue;
00429 };
00430 }
00431
00432 namespace mxflib
00433 {
00435
00437 inline std::string AddTraitsMapping(std::string TypeName, MDTraitsPtr Tr)
00438 {
00439
00440 MDTraitsPtr TrLookup = MDTraits::Find(Tr->Name());
00441
00442 if(!TrLookup) MDTraits::Add(Tr->Name(), Tr);
00443
00444 if(MDType::AddTraitsMapping(TypeName, Tr->Name()))
00445 return Tr->Name();
00446 else
00447 return "";
00448 }
00449
00450
00452
00454 inline std::string UpdateTraitsMapping(std::string TypeName, MDTraitsPtr Tr)
00455 {
00456
00457 MDTraitsPtr TrLookup = MDTraits::Find(Tr->Name());
00458
00459 if(!TrLookup) MDTraits::Add(Tr->Name(), Tr);
00460
00461 if(MDType::UpdateTraitsMapping(TypeName, Tr->Name()))
00462 return Tr->Name();
00463 else
00464 return "";
00465 }
00466
00467
00469
00471 inline std::string AddTraitsMapping(const UL &Type, MDTraitsPtr Tr)
00472 {
00473
00474 MDTraitsPtr TrLookup = MDTraits::Find(Tr->Name());
00475
00476 if(!TrLookup) MDTraits::Add(Tr->Name(), Tr);
00477
00478 if(MDType::AddTraitsMapping(Type, Tr->Name()))
00479 return Tr->Name();
00480 else
00481 return "";
00482 }
00483
00484
00486
00488 inline std::string UpdateTraitsMapping(const UL &Type, MDTraitsPtr Tr)
00489 {
00490
00491 MDTraitsPtr TrLookup = MDTraits::Find(Tr->Name());
00492
00493 if(!TrLookup) MDTraits::Add(Tr->Name(), Tr);
00494
00495 if(MDType::UpdateTraitsMapping(Type, Tr->Name()))
00496 return Tr->Name();
00497 else
00498 return "";
00499 }
00500 }
00501
00502
00503 namespace mxflib
00504 {
00506 typedef std::map<MapIndex, MDValuePtr> MDValueMap;
00507 }
00508
00509
00510 namespace mxflib
00511 {
00513 class MDValue : public RefCount<MDValue>, public MDValueMap
00514 {
00515 private:
00516 MDTypePtr Type;
00517 DataChunk Data;
00518
00519
00520
00521 public:
00522
00523
00524 public:
00525 MDValue(const std::string &BaseType);
00526 MDValue(MDTypePtr BaseType);
00527 void Init(void);
00528 ~MDValue() {};
00529
00530 void AddChild(MDValuePtr Child, int Index = -1);
00531 void Resize(UInt32 Index);
00532
00533 MDValuePtr operator[](int Index);
00534 MDValuePtr Child(int Index) { return operator[](Index); };
00535
00537 MDValuePtr operator[](const std::string ChildName);
00538 MDValuePtr Child(const std::string ChildName) { return operator[](ChildName); };
00539
00541 MDValuePtr operator[](const UL &Child);
00542 MDValuePtr Child(const UL &Child) { return operator[](Child); };
00543
00545 bool operator==(MDValuePtr &RHS) { return operator==(*RHS); }
00546
00548 bool operator==(MDValue &RHS)
00549 {
00550 if(Type->EffectiveType() != RHS.Type->EffectiveType()) return false;
00551 if(Data.Size != RHS.Data.Size) return false;
00552 return (memcmp(Data.Data, RHS.Data.Data, Data.Size) == 0);
00553 }
00554
00556 MDValue &operator=(MDValue &RHS)
00557 {
00558
00559 if(Type->EffectiveType() == RHS.Type->EffectiveType())
00560 {
00561 Data.Set(RHS.Data);
00562 }
00563
00564 else
00565 {
00566 SetString(RHS.GetString());
00567 }
00568
00569 return *this;
00570 }
00571
00572
00573
00574 void SetInt(Int32 Val) { Type->Traits->SetInt(this, Val); };
00575 void SetInt64(Int64 Val) { Type->Traits->SetInt64(this, Val); };
00576 void SetUInt(UInt32 Val) { Type->Traits->SetUInt(this, Val); };
00577 void SetUInt64(UInt64 Val) { Type->Traits->SetUInt64(this, Val); };
00578 void SetUint(UInt32 Val) { Type->Traits->SetUInt(this, Val); };
00579 void SetUint64(UInt64 Val) { Type->Traits->SetUInt64(this, Val); };
00580 void SetString(std::string Val) { Type->Traits->SetString(this, Val); };
00581 Int32 GetInt(void) { return Type->Traits->GetInt(this); };
00582 Int64 GetInt64(void) { return Type->Traits->GetInt64(this); };
00583 UInt32 GetUInt(void) { return Type->Traits->GetUInt(this); };
00584 UInt64 GetUInt64(void) { return Type->Traits->GetUInt64(this); };
00585 UInt32 GetUint(void) { return Type->Traits->GetUInt(this); };
00586 UInt64 GetUint64(void) { return Type->Traits->GetUInt64(this); };
00587 std::string GetString(void) { return Type->Traits->GetString(this); };
00588
00589
00590
00591 Int32 GetInt(const char *ChildName, Int32 Default = 0) { MDValuePtr Ptr = operator[](ChildName); if (Ptr) return Ptr->GetInt(); else return Default; };
00592 Int64 GetInt64(const char *ChildName, Int64 Default = 0) { MDValuePtr Ptr = operator[](ChildName); if (Ptr) return Ptr->GetInt64(); else return Default; };
00593 UInt32 GetUInt(const char *ChildName, UInt32 Default = 0) { MDValuePtr Ptr = operator[](ChildName); if (Ptr) return Ptr->GetUInt(); else return Default; };
00594 UInt64 GetUInt64(const char *ChildName, UInt64 Default = 0) { MDValuePtr Ptr = operator[](ChildName); if (Ptr) return Ptr->GetUInt64(); else return Default; };
00595 UInt32 GetUint(const char *ChildName, UInt32 Default = 0) { MDValuePtr Ptr = operator[](ChildName); if (Ptr) return Ptr->GetUInt(); else return Default; };
00596 UInt64 GetUint64(const char *ChildName, UInt64 Default = 0) { MDValuePtr Ptr = operator[](ChildName); if (Ptr) return Ptr->GetUInt64(); else return Default; };
00597 std::string GetString(const char *ChildName, std::string Default = "") { MDValuePtr Ptr = operator[](ChildName); if (Ptr) return Ptr->GetString(); else return Default; };
00598 void SetInt(const char *ChildName, Int32 Val) { MDValuePtr Ptr = operator[](ChildName); if (Ptr) Ptr->SetInt(Val); };
00599 void SetInt64(const char *ChildName, Int64 Val) { MDValuePtr Ptr = operator[](ChildName); if (Ptr) Ptr->SetInt64(Val); };
00600 void SetUInt(const char *ChildName, UInt32 Val) { MDValuePtr Ptr = operator[](ChildName); if (Ptr) Ptr->SetUInt(Val); };
00601 void SetUInt64(const char *ChildName, UInt64 Val) { MDValuePtr Ptr = operator[](ChildName); if (Ptr) Ptr->SetUInt64(Val); };
00602 void SetUint(const char *ChildName, UInt32 Val) { MDValuePtr Ptr = operator[](ChildName); if (Ptr) Ptr->SetUInt(Val); };
00603 void SetUint64(const char *ChildName, UInt64 Val) { MDValuePtr Ptr = operator[](ChildName); if (Ptr) Ptr->SetUInt64(Val); };
00604 void SetString(const char *ChildName, std::string Val) { MDValuePtr Ptr = operator[](ChildName); if (Ptr) Ptr->SetString(Val); };
00605
00606
00607
00608 Int32 GetInt(const UL &Child, Int32 Default = 0) { MDValuePtr Ptr = operator[](Child); if (Ptr) return Ptr->GetInt(); else return Default; };
00609 Int64 GetInt64(const UL &Child, Int64 Default = 0) { MDValuePtr Ptr = operator[](Child); if (Ptr) return Ptr->GetInt64(); else return Default; };
00610 UInt32 GetUInt(const UL &Child, UInt32 Default = 0) { MDValuePtr Ptr = operator[](Child); if (Ptr) return Ptr->GetUInt(); else return Default; };
00611 UInt64 GetUInt64(const UL &Child, UInt64 Default = 0) { MDValuePtr Ptr = operator[](Child); if (Ptr) return Ptr->GetUInt64(); else return Default; };
00612 UInt32 GetUint(const UL &Child, UInt32 Default = 0) { MDValuePtr Ptr = operator[](Child); if (Ptr) return Ptr->GetUInt(); else return Default; };
00613 UInt64 GetUint64(const UL &Child, UInt64 Default = 0) { MDValuePtr Ptr = operator[](Child); if (Ptr) return Ptr->GetUInt64(); else return Default; };
00614 std::string GetString(const UL &Child, std::string Default = "") { MDValuePtr Ptr = operator[](Child); if (Ptr) return Ptr->GetString(); else return Default; };
00615 void SetInt(const UL &Child, Int32 Val) { MDValuePtr Ptr = operator[](Child); if (Ptr) Ptr->SetInt(Val); };
00616 void SetInt64(const UL &Child, Int64 Val) { MDValuePtr Ptr = operator[](Child); if (Ptr) Ptr->SetInt64(Val); };
00617 void SetUInt(const UL &Child, UInt32 Val) { MDValuePtr Ptr = operator[](Child); if (Ptr) Ptr->SetUInt(Val); };
00618 void SetUInt64(const UL &Child, UInt64 Val) { MDValuePtr Ptr = operator[](Child); if (Ptr) Ptr->SetUInt64(Val); };
00619 void SetUint(const UL &Child, UInt32 Val) { MDValuePtr Ptr = operator[](Child); if (Ptr) Ptr->SetUInt(Val); };
00620 void SetUint64(const UL &Child, UInt64 Val) { MDValuePtr Ptr = operator[](Child); if (Ptr) Ptr->SetUInt64(Val); };
00621 void SetString(const UL &Child, std::string Val) { MDValuePtr Ptr = operator[](Child); if (Ptr) Ptr->SetString(Val); };
00622
00623 void ReadValue(const char *ChildName, const DataChunk &Source) { MDValuePtr Ptr = operator[](ChildName); if (Ptr) Ptr->ReadValue(Source); };
00624 void ReadValue(const char *ChildName, DataChunkPtr &Source) { MDValuePtr Ptr = operator[](ChildName); if (Ptr) Ptr->ReadValue(Source); };
00625
00626 void ReadValue(const UL &Child, const DataChunk &Source) { MDValuePtr Ptr = operator[](Child); if (Ptr) Ptr->ReadValue(Source); };
00627 void ReadValue(const UL &Child, DataChunkPtr &Source) { MDValuePtr Ptr = operator[](Child); if (Ptr) Ptr->ReadValue(Source); };
00628
00629
00630
00631 size_t MakeSize(size_t NewSize);
00632
00633 size_t ReadValue(const DataChunk &Chunk) { return ReadValue(Chunk.Data, Chunk.Size); };
00634 size_t ReadValue(DataChunkPtr &Chunk) { return ReadValue(Chunk->Data, Chunk->Size); };
00635 size_t ReadValue(const UInt8 *Buffer, size_t Size, int Count=0);
00636
00638 const DataChunk& GetData(void) { return (const DataChunk&) Data; };
00639
00641 DataChunkPtr PutData(void);
00642
00644
00645 void SetData(size_t MemSize, const UInt8 *Buffer)
00646 {
00647 Data.Resize(MemSize);
00648 Data.Set(MemSize, Buffer);
00649 };
00650
00651
00652 const std::string &Name(void) const { ASSERT(Type); return Type->TypeName; };
00653
00654
00655 MDTypePtr GetType(void) { return Type; };
00656 MDTypePtr EffectiveType(void) { return Type->EffectiveType(); };
00657 MDTypePtr EffectiveBase(void) { return Type->EffectiveBase(); };
00658 };
00659 }
00660
00661
00662
00663 namespace mxflib
00664 {
00665 inline MDValuePtr MDValuePtr::operator[](int Index)
00666 {
00667
00668 ASSERT(!(operator->()->GetType()->HandlesSubdata()));
00669 return operator->()->operator[](Index);
00670 };
00671 inline MDValuePtr MDValuePtr::operator[](const std::string ChildName)
00672 {
00673
00674 ASSERT(!(operator->()->GetType()->HandlesSubdata()));
00675 return operator->()->operator[](ChildName);
00676 };
00677 }
00678
00679
00680 #endif // MXFLIB__MDTYPE_H
00681