InDesign SDK  20.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
SchemaUtils.h
1 //========================================================================================
2 //
3 // $File$
4 //
5 // Owner: Steve Pellegrin
6 //
7 // $Author$
8 //
9 // $DateTime$
10 //
11 // $Revision$
12 //
13 // $Change$
14 //
15 // Copyright 1997-2010 Adobe Systems Incorporated. All rights reserved.
16 //
17 // NOTICE: Adobe permits you to use, modify, and distribute this file in accordance
18 // with the terms of the Adobe license agreement accompanying it. If you have received
19 // this file from a source other than Adobe, then your use, modification, or
20 // distribution of it requires the prior written permission of Adobe.
21 //
22 //
23 // Adobe patent application tracking # P409, entitled 'Schema-based file Conversion',
24 // inventors: Robin Briggs, Steve Pellegrin
25 //
26 //========================================================================================
27 
28 #if !defined(__SchemaUtils__)
29 #define __SchemaUtils__
30 
31 // ----- Includes -----
32 
33 #include <algorithm>
34 #include <cmath>
35 #include "K2Vector.h"
36 #include "SchemaTypes.h"
37 #include "SimpleLink.h"
38 #include <map>
39 
40 // ----- ID.h files -----
41 
42 #include "FormatID.h"
43 #include "VersionID.h"
44 
45 
46 class IPMStream;
47 class NameInfo;
48 class PMMatrix;
49 class PMPoint;
50 class PMReal;
51 class PMRect;
52 class PMString;
53 class WideString;
54 class URI;
55 struct IDResource;
56 
57 
58 namespace Schemas
59 {
60  //========================================================================================
61  // TYPE DEFINITIONS
62  //========================================================================================
63  typedef K2Vector<ClassID> ClassIDVector;
64  typedef K2Vector<ImplementationID> ImplIDVector;
65  typedef K2Vector<int32> MajorFormatVector;
66  typedef K2Vector<PluginID> PluginVector;
67  class Version;
68  typedef K2Vector<Version> VersionVector;
69  class SchemaInfo;
70  typedef K2Vector<SchemaInfo> SchemaInfoVector;
71  class IteratorSchemaInfo;
72  typedef K2Vector<IteratorSchemaInfo> IteratorSchemaInfoVector;
73  class PathStep;
74  typedef K2Vector<PathStep> PathStepVector;
75  class Implementation;
76  typedef K2Vector<Implementation> ImplVector;
77  class Field;
78  typedef K2Vector<Field> FieldVector;
79  class FieldValue;
80  typedef K2Vector<FieldValue> ValueVector;
81  class Conversion;
82  typedef K2Vector<Conversion> ConversionVector;
83  class Directive;
84  typedef K2Vector<Directive *> DirectivePtrVector;
85 
86 
87  //========================================================================================
88  // CLASS DECLARATIONS
89  //========================================================================================
90  //________________________________________________________________________________________
91  // Version
92  //________________________________________________________________________________________
98  class Version: public VersionID
99  {
100  public:
101  typedef base_type data_type;
102 
103  Version()
104  : VersionID(kInvalidPlugin, -1, -1), fForced(kFalse)
105  {}
106 
107  Version(const VersionID &version, bool16 forced = kFalse)
108  : VersionID(version), fForced(forced)
109  {}
110 
111  Version(PluginID pluginID, const FormatID &format, bool16 forced = kFalse)
112  : VersionID(pluginID, format), fForced(forced)
113  {}
114 
115  bool16 WasForced() const
116  {return fForced;}
117 
118  private:
119  bool16 fForced;
120  };
121 
122 
123  //________________________________________________________________________________________
124  // Conversion
125  //________________________________________________________________________________________
132  {
133  public:
134  typedef base_type data_type;
135  Conversion()
136  {}
137  Conversion(const Version &from, const Version &to)
138  : fFromVersion(from), fToVersion(to)
139  {}
140 
141  Version GetFromVersion() const
142  {return fFromVersion;}
143  Version GetToVersion() const
144  {return fToVersion;}
145 
146  bool16 operator==(const Conversion& other) const
147  {return (fFromVersion == other.fFromVersion) && (fToVersion == other.fToVersion);}
148 
149  private:
150  Version fFromVersion;
151  Version fToVersion;
152  };
153 
154 
155  //________________________________________________________________________________________
156  // FieldValue
157  //________________________________________________________________________________________
164  {
165  public:
166  class DataBlob
167  {
168  public:
169  DataBlob()
170  : fType(kInvalidField), fBuffer(nil), fBufferSize(0)
171  {}
172  DataBlob(FieldType type)
173  : fType(type), fBuffer(nil), fBufferSize(0)
174  {}
175  DataBlob(const DataBlob &other)
176  : fType(other.fType), fBuffer(nil), fBufferSize(0)
177  {Copy(other);}
178 
179  ~DataBlob()
180  {SetSize(0);}
181 
182  int32 GetSize() const
183  {return fBufferSize;}
184  IPMStream *QueryReadStream();
185  void ReadWrite(IPMStream *s);
186 
187  bool16 operator==(const DataBlob& other) const;
188  DataBlob & operator=(const DataBlob &other)
189  {Copy(other); return *this;}
190 
191  public:
192  static bool16 IsDataBlobType(FieldType t)
193  {return ((t == kDataBlob16) || (t == kDataBlob32) || (t == kDataBlob8));}
194 
195  private:
196  void SetSize(int32 size);
197  void Copy(const DataBlob &other);
198 
199  private:
200  FieldType fType;
201  int32 fBufferSize;
202  uchar * fBuffer;
203  };
204 
205  public:
206  typedef object_type data_type;
207 
208  FieldValue()
209  : fType(kInvalidField)
210  {SetUnionType(fType, fValue);}
211  FieldValue(FieldType type)
212  : fType(type)
213  {SetUnionType(fType, fValue);}
214  FieldValue(FieldType type, IPMStream *s);
215  FieldValue(const FieldValue &other);
216 
217  ~FieldValue();
218 
219  FieldType GetType() const
220  {return fType;}
221 
222  bool16 GetBool8(bool8 &v) const;
223  bool16 GetBool16(bool16 &v) const;
224  bool16 GetInt8(int8 &v) const;
225  bool16 GetUint8(uint8 &v) const;
226  bool16 GetInt16(int16 &v) const;
227  bool16 GetUint16(uint16 &v) const;
228  bool16 GetInt32(int32 &v) const;
229  bool16 GetUint32(uint32 &v) const;
230  bool16 GetReal(PMReal &v) const;
231  bool16 GetClassID(ClassID &v) const;
232  bool16 GetImplementationID(ImplementationID &v) const;
233  bool16 GetInterfaceID(PMIID &v) const;
234  bool16 GetServiceID(ServiceID &v) const;
235  bool16 GetWidgetID(WidgetID &v) const;
236  bool16 GetPluginID(PluginID &v) const;
237  bool16 GetErrorID(ErrorCode &v) const;
238  bool16 GetObject(UID &v) const;
239  bool16 GetReference(UID &v) const;
240  bool16 GetPMString(PMString &v) const;
241  bool16 GetPMPoint(PMPoint &v) const;
242  bool16 GetPMRect(PMRect &v) const;
243  bool16 GetPMMatrix(PMMatrix &v) const;
244  bool16 GetNameInfo(NameInfo &v) const;
245  bool16 GetDataBlob(DataBlob **v) const;
246  bool16 GetWideString(WideString &v) const;
247  bool16 GetURI(URI &v) const;
248 
249  void SetInt16(int16 v)
250  {
251  ASSERT(fType == kInt16);
252  fValue.dInt16 = v;
253  }
254 
255  void ReadWrite(IPMStream *s);
256  bool16 CopyValue(const FieldValue &other);
257 
258  bool16 operator==(const FieldValue& other) const;
259  FieldValue & operator=(const FieldValue &other);
260 
261  private:
262  union ValueUnion
263  {
264  bool8 dBool8;
265  bool16 dBool16;
266  int8 dInt8;
267  uint8 dUint8;
268  int16 dInt16;
269  uint16 dUint16;
270  int32 dInt32;
271  uint32 dUint32;
272  uint32 dIDValue;
273  PMReal * dReal;
274  PMString * dPMString;
275  PMPoint * dPMPoint;
276  PMRect * dPMRect;
277  PMMatrix * dPMMatrix;
278  NameInfo * dNameInfo;
279  DataBlob * dDataBlob;
280  WideString * dWideString;
281  URI * dURI;
282  };
283 
284  private:
285  static void SetUnionType(FieldType newT, ValueUnion &v, FieldType oldT = kInvalidField);
286  static void ClearUnion(FieldType t, ValueUnion &v)
287  {SetUnionType(kInvalidField, v, t);}
288  static bool16 ConvertValue(FieldType sType, const ValueUnion &sValue, FieldType tType, ValueUnion &tValue);
289  static bool16 ConvertBool8(bool8 sValue, FieldType tType, ValueUnion &tValue);
290  static bool16 ConvertBool16(bool16 sValue, FieldType tType, ValueUnion &tValue);
291  static bool16 ConvertInt8(int8 sValue, FieldType tType, ValueUnion &tValue);
292  static bool16 ConvertUint8(uint8 sValue, FieldType tType, ValueUnion &tValue);
293  static bool16 ConvertInt16(int16 sValue, FieldType tType, ValueUnion &tValue);
294  static bool16 ConvertUint16(uint16 sValue, FieldType tType, ValueUnion &tValue);
295  static bool16 ConvertInt32(int32 sValue, FieldType tType, ValueUnion &tValue);
296  static bool16 ConvertUint32(uint32 sValue, FieldType tType, ValueUnion &tValue);
297  static bool16 ConvertReal(const PMReal &sValue, FieldType tType, ValueUnion &tValue);
298  static bool16 ConvertID(uint32 sValue, FieldType tType, ValueUnion &tValue);
299  static bool16 ConvertPMString(const PMString &sValue, FieldType tType, ValueUnion &tValue);
300  static PMReal MakePMReal(IPMStream *s);
301  static PMString MakePMString(IPMStream *s);
302  static WideString MakeWideString(IPMStream *s);
303  static URI MakeURI(IPMStream *s);
304 
305  private:
306  void ReadDefaultValue(IPMStream *s);
307 
308  private:
309  FieldType fType;
310  ValueUnion fValue;
311  };
312 
313 
314  //________________________________________________________________________________________
315  // Instance
316  //________________________________________________________________________________________
322  class Instance
323  {
324  public:
325  typedef uint32 InstanceValue;
326 
327  public:
328  Instance()
329  : fValueList()
330  {}
331  Instance(const Instance &other)
332  : fValueList(other.fValueList)
333  {}
334 
335  void PushLevel(InstanceValue v = 0)
336  {fValueList.push_back(v);}
337  void PopLevel()
338  {fValueList.pop_back();}
339  void SetValue(InstanceValue v)
340  {fValueList.back() = v;};
341 
342  bool16 operator==(const Instance& other) const
343  {return (fValueList == other.fValueList);}
344  bool16 operator<(const Instance& other) const;
345 
346  Instance &operator=(const Instance &other)
347  {fValueList = other.fValueList; return *this;}
348 
349  private:
351 
352  private:
353  InstanceVector fValueList;
354  };
355 
356 
357  //________________________________________________________________________________________
358  // FieldKey
359  //________________________________________________________________________________________
365  class FieldKey
366  {
367  public:
368  FieldKey(FieldID id = kInvalidFieldID)
369  : fID(id), fInstance()
370  {}
371  FieldKey(FieldID id, const Instance &i)
372  : fID(id), fInstance(i)
373  {}
374  FieldKey(const FieldKey &other)
375  : fID(other.fID), fInstance(other.fInstance)
376  {}
377 
378  FieldID GetID() const
379  {return fID;}
380  const Instance &GetInstance() const
381  {return fInstance;}
382  bool16 IsValid() const
383  {return fID != kInvalidFieldID;}
384 
385  void SetID(FieldID id)
386  {fID = id;}
387  void SetInstance(const Instance &i)
388  {fInstance = i;}
389  void Invalidate()
390  {SetID(kInvalidFieldID);}
391 
392  bool16 operator<(const FieldKey &other) const
393  {return (fID == other.fID) ? (fInstance < other.fInstance) : (fID < other.fID);}
394  bool16 operator==(const FieldKey &other) const
395  {return ((fID == other.fID) && (fInstance == other.fInstance));}
396 
397  FieldKey &operator=(const FieldKey &other)
398  {fID = other.fID; fInstance = other.fInstance; return *this;}
399 
400  private:
401  FieldID fID;
402  Instance fInstance;
403  };
404 
405 
406  //________________________________________________________________________________________
407  // Field
408  //________________________________________________________________________________________
414  class Field
415  {
416  public:
417  typedef object_type data_type;
418 
419  Field()
420  : fKey(kInvalidFieldID), fType(kInvalidField)
421  {}
422  Field(FieldID id, const FieldValue &fv)
423  : fKey(id), fType(kInvalidField)
424  {AddValue(fv);}
425  Field(const FieldValue &fv)
426  : fKey(kInvalidFieldID), fType(kInvalidField)
427  {AddValue(fv);}
428  Field(FieldID id, const Instance &instance, FieldType type = kInvalidField)
429  : fKey(id, instance), fType(type)
430  {}
431  Field(const Field &other)
432  : fType(other.fType), fKey(other.fKey), fValueList(other.fValueList)
433  {}
434  Field(const Field &other, const Instance &instance)
435  : fType(other.fType), fKey(other.fKey.GetID(), instance), fValueList(other.fValueList)
436  {}
437  Field(IPMStream *s, FieldType t);
438 
439  void ReadWrite(IPMStream *s, uint32 skipCount = 0, uint32 getCount = kMaxUInt32);
440  bool16 CopyValue(const Field &other, uint32 skipCount = 0, uint32 getCount = kMaxUInt32);
441  const FieldKey &GetKey() const
442  {return fKey;}
443 
444  FieldType GetType() const
445  {return fType;}
446 
447  uint32 GetValueCount() const
448  {return fValueList.size();}
449  ValueVector::const_iterator Begin() const
450  {return fValueList.begin();}
451  ValueVector::const_iterator End() const
452  {return fValueList.end();}
453  void AddValue(const FieldValue &v);
454 
455  bool16 operator==(const Field& other) const
456  {return ((fType == other.fType) && (fKey == other.fKey) && (fValueList == other.fValueList));}
457  Field &operator=(const Field &other)
458  {fType = other.fType; fKey = other.fKey; fValueList = other.fValueList; return *this;}
459 
460  private:
461  void ProcessIterationCount(FieldValue &count, IPMStream *s);
462  void AddDataBlob(IPMStream *s);
463  void ReadDefaultValue(FieldType t, IPMStream *s);
464 
465  private:
466  FieldType fType;
467  FieldKey fKey;
468  ValueVector fValueList;
469  };
470 
471 
472  //________________________________________________________________________________________
473  // Schema
474  //________________________________________________________________________________________
480  class Schema
481  {
482  public:
483  Schema()
484  {}
485  Schema(IPMStream *s)
486  {Read(s);}
487  Schema(const Schema &other)
488  : fFieldList(other.fFieldList)
489  {}
490 
491  void Read(IPMStream *s);
492 
493  FieldVector::const_iterator GetBeginIterator() const
494  {return fFieldList.begin();}
495  FieldVector::const_iterator GetEndIterator() const
496  {return fFieldList.end();}
497 
498  bool16 IsEmpty() const
499  {return (fFieldList.size() < 2);}
500  bool16 ContainsFieldType(FieldType type) const;
501 
502  bool16 operator==(const Schema& other) const
503  {return (fFieldList == other.fFieldList);}
504  bool16 operator!=(const Schema& other) const
505  {return !(*this == other);}
506 
507  Schema &operator=(const Schema &other)
508  {fFieldList = other.fFieldList; return *this;}
509 
510  private:
511  void ProcessFields(int16 count, IPMStream *s);
512  void ProcessFieldArray(IPMStream *s);
513 
514  private:
515  FieldVector fFieldList;
516  };
517 
518 
519  //________________________________________________________________________________________
520  // SchemaInfo
521  //________________________________________________________________________________________
528  {
529  public:
530  typedef object_type data_type;
531 
532  SchemaInfo()
533  : fIsClass(kFalse), fIDValue(0)
534  {}
535  SchemaInfo(ClassID id, const Version &version)
536  : fIsClass(kTrue), fIDValue(id.Get()), fVersion(version)
537  {}
538  SchemaInfo(ImplementationID id, const Version &version)
539  : fIsClass(kFalse), fIDValue(id.Get()), fVersion(version)
540  {}
541  SchemaInfo(const SchemaInfo &other)
542  {Copy(other);}
543 
544  bool16 AppliesTo(ClassID id) const
545  {return AppliesTo(kTrue, id.Get());}
546  bool16 AppliesTo(ImplementationID id) const
547  {return AppliesTo(kFalse, id.Get());}
548  bool16 AppliesTo(bool16 isClass, uint32 idValue) const
549  {return (fIsClass == isClass) && (idValue == fIDValue);}
550 
551  bool16 Below(ClassID id) const
552  {return Below(kTrue, id.Get());}
553  bool16 Below(ImplementationID id) const
554  {return Below(kFalse, id.Get());}
555  bool16 Below(bool16 isClass, uint32 idValue) const
556  {return (fIsClass == isClass) ? (fIDValue < idValue) : (fIsClass < isClass);}
557 
558  bool16 Above(ClassID id) const
559  {return Above(kTrue, id.Get());}
560  bool16 Above(ImplementationID id) const
561  {return Above(kFalse, id.Get());}
562  bool16 Above(bool16 isClass, uint32 idValue) const
563  {return (fIsClass == isClass) ? (fIDValue > idValue) : (fIsClass > isClass);}
564 
565  uint32 GetIDValue() const
566  {return fIDValue;}
567  const Version &GetVersion() const
568  {return fVersion;}
569  const Schema &GetSchema() const
570  {return fSchema;}
571 
572  void ReadSchema(IPMStream *s)
573  {fSchema.Read(s);}
574 
575  SchemaInfo & operator=(const SchemaInfo &other);
576  bool16 operator==(const SchemaInfo &other) const;
577  bool16 operator<(const SchemaInfo &other) const;
578 
579  private:
580  void Copy(const SchemaInfo &other)
581  {fIsClass = other.fIsClass; fIDValue = other.fIDValue;
582  fVersion = other.fVersion; fSchema = other.fSchema;}
583 
584 
585  private:
586  bool16 fIsClass;
587  uint32 fIDValue;
588  Version fVersion;
589  Schema fSchema;
590  };
591 
592 
593  //________________________________________________________________________________________
594  // IteratorSchemaInfo
595  //________________________________________________________________________________________
602  {
603  public:
604  typedef base_type data_type;
605 
607  : fIsClass(false), fIDValue(0), fFormat(), fSchemaInfo(nil)
608  {}
609  IteratorSchemaInfo(ClassID clsID, const FormatID &format, const SchemaInfo *info = nil)
610  : fIsClass(true), fIDValue(clsID.Get()), fFormat(format), fSchemaInfo(info)
611  {}
612  IteratorSchemaInfo(ImplementationID implID, const FormatID &format, const SchemaInfo *info = nil)
613  : fIsClass(false), fIDValue(implID.Get()), fFormat(format), fSchemaInfo(info)
614  {}
615  IteratorSchemaInfo(bool16 isClass, uint32 idValue, const FormatID &format, const SchemaInfo *info = nil)
616  : fIsClass(isClass), fIDValue(idValue), fFormat(format), fSchemaInfo(info)
617  {}
619  {Copy(other);}
620 
621  const Schema * GetSchema() const
622  {return (fSchemaInfo == nil) ? nil : &fSchemaInfo->GetSchema();}
623 
624  bool16 operator==(const IteratorSchemaInfo& other) const
625  {return ((fIsClass == other.fIsClass) && (fIDValue == other.fIDValue) && (fFormat == other.fFormat));}
626 
627  IteratorSchemaInfo & operator=(const IteratorSchemaInfo &other)
628  {Copy(other); return *this;}
629 
630  private:
631  void Copy(const IteratorSchemaInfo &other)
632  {fIsClass = other.fIsClass; fIDValue = other.fIDValue;
633  fFormat = other.fFormat; fSchemaInfo = other.fSchemaInfo;}
634 
635  private:
636  bool16 fIsClass;
637  uint32 fIDValue;
638  FormatID fFormat;
639  const SchemaInfo * fSchemaInfo;
640  };
641 
642 
643  //________________________________________________________________________________________
644  // Directive
645  //________________________________________________________________________________________
651  class Directive
652  {
653  public:
654  typedef base_type data_type;
655  Directive()
656  {}
657  Directive(int16 type, const Version &version)
658  : fType(type), fVersion(version)
659  {}
660 
661  virtual int16 GetType() const
662  {return fType;}
663  virtual const Version & GetVersion() const
664  {return fVersion;}
665 
666  bool16 InRange(const VersionID &fromV, const VersionID &toV) const;
667 
668  private:
669  int16 fType;
670  Version fVersion;
671  };
672 
673 
674  //________________________________________________________________________________________
675  // ClassDirective
676  //________________________________________________________________________________________
682  class ClassDirective: public Directive
683  {
684  public:
685  typedef base_type data_type;
687  {}
688  ClassDirective(int16 type, ClassID classID, const Version &version)
689  : Directive(type, version), fOldClassID(classID), fNewClassID(kInvalidClass)
690  {}
691  ClassDirective(int16 type, ClassID oldClassID, ClassID newClassID, const Version &version)
692  : Directive(type, version), fOldClassID(oldClassID), fNewClassID(newClassID)
693  {}
694 
695  ClassID GetClassID() const
696  {return fOldClassID;}
697  ClassID GetOldClassID() const
698  {return fOldClassID;}
699  ClassID GetNewClassID() const
700  {return fNewClassID;}
701 
702  private:
703  ClassID fOldClassID;
704  ClassID fNewClassID;
705  };
706 
707 
708  //________________________________________________________________________________________
709  // ImplementationDirective
710  //________________________________________________________________________________________
717  {
718  public:
719  typedef base_type data_type;
721  {}
722  ImplementationDirective(int16 type, ClassID context, ImplementationID implID, const Version &version)
723  : Directive(type, version), fContext(context), fOldImplID(implID), fNewImplID(kInvalidImpl)
724  {}
725  ImplementationDirective(int16 type, ClassID context, ImplementationID oldImplID, ImplementationID newImplID, const Version &version)
726  : Directive(type, version), fContext(context), fOldImplID(oldImplID), fNewImplID(newImplID)
727  {}
728 
729  ClassID GetContext() const
730  {return fContext;}
731  ImplementationID GetImplID() const
732  {return fOldImplID;}
733  ImplementationID GetOldImplID() const
734  {return fOldImplID;}
735  ImplementationID GetNewImplID() const
736  {return fNewImplID;}
737 
738  private:
739  ClassID fContext;
740  ImplementationID fOldImplID;
741  ImplementationID fNewImplID;
742  };
743 
744 
745  //________________________________________________________________________________________
746  // MoveDirective(s)
747  //________________________________________________________________________________________
753  template <class T>
754  class MoveDirective: public Directive
755  {
756  public:
757  typedef base_type data_type;
758 
759  MoveDirective()
760  {}
761  MoveDirective(int16 type, T fromID, const Version &fromVersion, T toID, const Version &toVersion, const Version &myVersion)
762  : Directive(type, myVersion), fFromID(fromID), fFromVersion(fromVersion), fToID(toID), fToVersion(toVersion)
763  {}
764 
765  T GetFromID() const
766  {return fFromID;}
767  T GetToID() const
768  {return fToID;}
769  const Version & GetFromVersion() const
770  {return fFromVersion;}
771  const Version & GetToVersion() const
772  {return fToVersion;}
773 
774  bool operator==(const MoveDirective<T>&) const
775  {ASSERT_FAIL("appease K2Vector.Location"); return false;}
776 
777  private:
778  T fFromID;
779  Version fFromVersion;
780  T fToID;
781  Version fToVersion;
782  };
783 
786 
787  //________________________________________________________________________________________
788  // PluginDirective
789  //________________________________________________________________________________________
796  {
797  public:
798  typedef base_type data_type;
800  {}
801  PluginDirective(int16 type, PluginID pluginID, const Version &version)
802  : Directive(type, version), fFromVersion(pluginID, FormatID(0, 0)), fToVersion()
803  {}
804  PluginDirective(int16 type, const Version &fromVersion, const Version &toVersion, const Version &myVersion)
805  : Directive(type, myVersion), fFromVersion(fromVersion), fToVersion(toVersion)
806  {}
807 
808  PluginID GetPluginID() const
809  {return fFromVersion.GetPluginID();}
810 
811  private:
812  Version fFromVersion;
813  Version fToVersion;
814  };
815 
816 
817  //________________________________________________________________________________________
818  // PathStep
819  //________________________________________________________________________________________
825  class PathStep
826  {
827  public:
828  typedef base_type data_type;
829  PathStep()
830  {}
831  PathStep(int32 version, int32 predecessor)
832  : fVersion(version), fPredecessor(predecessor)
833  {}
834 
835  int32 GetVersion() const
836  {return fVersion;}
837  int32 GetPredecessor() const
838  {return fPredecessor;}
839 
840  bool16 operator==(const PathStep& other) const
841  {return ((fVersion == other.fVersion) && (fPredecessor == other.fPredecessor));}
842 
843  private:
844  int32 fVersion;
845  int32 fPredecessor;
846  };
847 
848 
849  //________________________________________________________________________________________
850  // Implementation
851  //________________________________________________________________________________________
858  {
859  public:
860  typedef base_type data_type;
862  {}
863  Implementation(PluginID provider, ClassID boss, ImplementationID tag = kInvalidImpl)
864  : fProvider(provider), fBoss(boss), fTag(tag)
865  {}
867  : fProvider(d.GetVersion().GetPluginID()), fBoss(d.GetClassID()), fTag(kInvalidImpl)
868  {}
870  : fProvider(d.GetVersion().GetPluginID()), fBoss(d.GetContext()), fTag(d.GetImplID())
871  {}
872 
873  PluginID GetProvider() const
874  {return fProvider;}
875  ClassID GetBoss() const
876  {return fBoss;}
877  ImplementationID GetTag() const
878  {return fTag;}
879 
880  bool16 operator==(const Implementation& other) const
881  {return ((fProvider == other.fProvider) && (fBoss == other.fBoss) && (fTag == other.fTag));}
882 
883  private:
884  PluginID fProvider;
885  ClassID fBoss;
886  ImplementationID fTag;
887  };
888 
889 
890  //________________________________________________________________________________________
891  // ConvertInfo
892  //________________________________________________________________________________________
899  {
900  private:
901  enum Mode
902  {
903  kDontConvert,
904  kConvert,
905  kRemove,
906  kReplace
907  };
908 
909  public:
910  typedef base_type data_type;
911 
912  ConvertInfo()
913  : fMode(kDontConvert), fIDValue(0), fContextValue(0), fConversionIndex(-1), fSourceInfo(), fTargetInfo(), fNewIDValue(0)
914  {}
915  ConvertInfo(ImplementationID impl, ClassID context, int32 cIndex)
916  : fMode(kDontConvert), fIsClass(kFalse),fIDValue(impl.Get()), fContextValue(context.Get()), fConversionIndex(cIndex), fSourceInfo(), fTargetInfo(), fNewIDValue(0)
917  {}
918  ConvertInfo(ClassID cls, ImplementationID context, int32 cIndex)
919  : fMode(kDontConvert), fIsClass(kTrue),fIDValue(cls.Get()), fContextValue(context.Get()), fConversionIndex(cIndex), fSourceInfo(), fTargetInfo(), fNewIDValue(0)
920  {}
921  ConvertInfo(bool16 isClass, uint32 idValue, uint32 contextValue, int32 cIndex)
922  : fMode(kDontConvert), fIsClass(isClass),fIDValue(idValue), fContextValue(contextValue), fConversionIndex(cIndex), fSourceInfo(), fTargetInfo(), fNewIDValue(0)
923  {}
924 
925 
926  const SchemaInfo * GetSourceSchemaInfo() const
927  {return fSourceInfo;}
928  const SchemaInfo * GetTargetSchemaInfo() const
929  {return fTargetInfo;}
930  uint32 GetNewID() const
931  {return fNewIDValue;}
932 
933  void SetRemove()
934  {fMode = kRemove;}
935  void SetReplace(uint32 newIDValue)
936  {fMode = kReplace; fNewIDValue = newIDValue;}
937  void SetConvert(const SchemaInfo *sInfo, const SchemaInfo *tInfo)
938  {fMode = kConvert; fSourceInfo = sInfo; fTargetInfo = tInfo;}
939  bool16 NoConversion() const
940  {return (fMode == kDontConvert);}
941  bool16 ShouldRemove() const
942  {return (fMode == kRemove);}
943  bool16 ShouldReplace() const
944  {return (fMode == kReplace);}
945  bool16 ShouldConvert() const
946  {return (fMode == kConvert);}
947 
948  bool16 operator==(const ConvertInfo& other) const;
949  bool16 operator<(const ConvertInfo& other) const;
950  bool16 operator>(const ConvertInfo& other) const;
951 
952  private:
953  const SchemaInfo * fSourceInfo;
954  const SchemaInfo * fTargetInfo;
955  Mode fMode;
956  uint32 fIDValue;
957  uint32 fContextValue;
958  int32 fConversionIndex;
959  uint32 fNewIDValue;
960  bool16 fIsClass;
961  };
962 
963 
964  //________________________________________________________________________________________
965  // DataSet
966  //________________________________________________________________________________________
972  class DataSet
973  {
974  public:
975  DataSet();
976  DataSet(const Schema &schema, IPMStream *stream);
977  DataSet(const Schema &schema, DataSet &data);
978 
979  // To fill the DataSet from a schema.
980  void Fill(const Schema &schema, IPMStream *stream);
981 
982  // To fill the DataSet manually with a simple field.
983  void AppendField(const Field &f);
984 
985  // To fill the DataSet manually with a simple field.
986  Schemas::FieldValue AppendSimpleField(Schemas::FieldType type,
987  FieldID id, IPMStream* stream);
988 
989  // To fill the DataSet manually for an Attribute boss list
990  void AppendAttributeBossList(FieldID id, FieldType countType,
991  FieldType dataType, IPMStream* stream);
992 
993  bool16 IsEmpty() const
994  {return fFieldList.empty();}
995 
996  FieldVector::iterator Begin()
997  {return fFieldList.begin();}
998  FieldVector::iterator End()
999  {return fFieldList.end();}
1000 
1001  bool16 HasError() const
1002  {return fErrorFieldKey.IsValid();}
1003  void Write(IPMStream *s);
1004  const Field *FindField(Field &goal);
1005 
1006  private:
1007  // *****
1008  // Helper classes: Fetch a value from some source and store it in a Field.
1009  // *****
1010  class ValueSource
1011  {
1012  public:
1013  ValueSource()
1014  {}
1015  virtual ~ValueSource()
1016  {}
1017 
1018  virtual bool16 GetValue(Field &f, uint32 skipCount = 0, uint32 getCount = kMaxUInt32) = 0;
1019  };
1020 
1021  class StreamValueSource : public ValueSource
1022  {
1023  public:
1024  StreamValueSource(IPMStream *s)
1025  : fStream(s)
1026  {}
1027  virtual ~StreamValueSource()
1028  {}
1029 
1030  virtual bool16 GetValue(Field &f, uint32 skipCount, uint32 getCount);
1031 
1032  private:
1033  IPMStream * fStream;
1034  };
1035 
1036  class DataValueSource : public ValueSource
1037  {
1038  public:
1039  DataValueSource(DataSet &d)
1040  : fData(&d)
1041  {}
1042  virtual ~DataValueSource()
1043  {}
1044 
1045  virtual bool16 GetValue(Field &f, uint32 skipCount, uint32 getCount);
1046 
1047  private:
1048  DataSet * fData;
1049  };
1050 
1051  // *****
1052  // Helper: Map to lookup Fields by ID/Instance.
1053  // *****
1054  typedef std::map<FieldKey, const Field *> FieldIDMap;
1055 
1056  private:
1057  void Initialize(const Schema &schema, ValueSource &vs);
1058  FieldVector::const_iterator ProcessFields(Instance &inst, int16 count, FieldVector::const_iterator pF, ValueSource &vs);
1059  FieldVector::const_iterator ProcessSimpleField(Instance &inst, FieldVector::const_iterator pF, ValueSource &vs);
1060  FieldVector::const_iterator ProcessFieldArray(Instance &inst, FieldVector::const_iterator pF, ValueSource &vs);
1061  FieldVector::const_iterator ProcessAttributeBoss(Instance &inst, FieldVector::const_iterator pF, ValueSource &vs);
1062  FieldVector::const_iterator ProcessAttributeBossList(Instance &inst, FieldVector::const_iterator pF, ValueSource &vs);
1063  FieldVector::const_iterator SkipFields(int16 count, FieldVector::const_iterator pF);
1064  FieldVector::const_iterator SkipFieldArray(FieldVector::const_iterator pF);
1065  void BuildMap();
1066  const Field *FindFieldInList(Field &goal) const;
1067  const Field *FindFieldInMap(Field &goal) const;
1068 
1069  private:
1070  FieldVector fFieldList;
1071  FieldKey fErrorFieldKey;
1072  bool16 fMappingDecisionMade;
1073  FieldIDMap fFieldMap;
1074  };
1075 
1076 
1077 } // End Schemas namespace
1078 
1079 
1080 //________________________________________________________________________________________
1081 // SchemaManager
1082 //________________________________________________________________________________________
1089 {
1090 public:
1091  SchemaManager()
1092  : fPluginID(kInvalidPlugin)
1093  {}
1095  {BuildSchemaList(p);}
1096 
1097  void SetPluginID(PluginID p)
1098  {BuildSchemaList(p);}
1099  PluginID GetPluginID() const
1100  {return fPluginID;}
1101 
1102  void GetVersions(Schemas::VersionVector &v) const;
1103 
1104  bool16 HasSchemas(ClassID id) const
1105  {return HasSchemas(kTrue, id.Get());}
1106  bool16 HasSchemas(ImplementationID id) const
1107  {return HasSchemas(kFalse, id.Get());}
1108  bool16 HasSchemas(bool16 isClass, uint32 idValue) const;
1109 
1110  const Schemas::SchemaInfo *FindClosestSchema(ClassID id, const Schemas::Version &version, const Schemas::MajorFormatVector &path) const
1111  {return FindClosestSchema(kTrue, id.Get(), version, path);}
1112  const Schemas::SchemaInfo *FindClosestSchema(ImplementationID id, const Schemas::Version &version, const Schemas::MajorFormatVector &path) const
1113  {return FindClosestSchema(kFalse, id.Get(), version, path);}
1114  const Schemas::SchemaInfo *FindClosestSchema(bool16 isClass, uint32 idValue, const Schemas::Version &version, const Schemas::MajorFormatVector &path) const;
1115 
1116 private:
1117  void BuildSchemaList(PluginID p);
1118  void ProcessSchema(IPMStream *s);
1119  void ProcessVersionList(IPMStream *s);
1120  bool16 FindSchemaVersion(const Schemas::Version &version) const;
1121  bool IndividualSchemaStreamCallback(IDResource r, void* userData);
1122  bool SchemaListStreamCallback(IDResource r, void* userData);
1123  bool VersionListResourceCallback(IDResource r, void* userData);
1124 
1125 private:
1126  PluginID fPluginID;
1127  Schemas::SchemaInfoVector fIDList;
1128 };
1129 
1130 
1131 //________________________________________________________________________________________
1132 // DirectiveManager
1133 //________________________________________________________________________________________
1140 {
1141 public:
1143  : fPluginID(kInvalidPlugin)
1144  {}
1146  {BuildDirectiveList(p);}
1147  ~DirectiveManager()
1148  {ClearDirectiveList();}
1149 
1150  void SetPluginID(PluginID p)
1151  {BuildDirectiveList(p);}
1152  PluginID GetPluginID() const
1153  {return fPluginID;}
1154 
1155  void GetAllVersions(Schemas::VersionVector &v) const;
1156  void GetPluginVersions(PluginID plugin, int16 dirType, Schemas::VersionVector &v) const;
1157  void GetVersions(int16 dirType, Schemas::VersionVector &v) const
1158  {GetPluginVersions(kInvalidPlugin, dirType, v);}
1159  const Schemas::ClassDirective * HaveDirectiveForClass(int16 dirType, ClassID classID, const Schemas::Conversion &conv) const;
1160  const Schemas::ImplementationDirective * HaveDirectiveForImplementation(int16 dirType, ClassID context, ImplementationID implID, const Schemas::Conversion &conv) const;
1161 
1162  void GetDirectivesOfType(int16 dirType, Schemas::DirectivePtrVector &list) const;
1163 
1164 private:
1165  bool DirectiveListCallback(IDResource r, void* userData);
1166  void BuildDirectiveList(PluginID p);
1167  void ClearDirectiveList();
1168 
1169 private:
1170  PluginID fPluginID;
1171  Schemas::DirectivePtrVector fDirectivePtrList;
1172 };
1173 
1174 
1175 //________________________________________________________________________________________
1176 // PathManager
1177 //________________________________________________________________________________________
1184 {
1185 public:
1186  PathManager()
1187  : fPluginID(kInvalidPlugin)
1188  {}
1190  {BuildStepList(p);}
1191 
1192  void SetPluginID(PluginID p)
1193  {BuildStepList(p);}
1194  PluginID GetPluginID() const
1195  {return fPluginID;}
1196 
1197  bool16 GetPath(int32 fromVersion, int32 toVersion, Schemas::MajorFormatVector &list) const
1198  {return FindPath(fromVersion, toVersion, &list);}
1199  bool16 PathExists(int32 fromVersion, int32 toVersion) const
1200  {return FindPath(fromVersion, toVersion);}
1201  bool16 FindBestPath(const Schemas::Version &from, const Schemas::Version &to, Schemas::MajorFormatVector &path) const;
1202 
1203 public:
1204  static bool16 IsReachable(int32 fromVersion, int32 toVersion, const Schemas::MajorFormatVector &path);
1205 
1206 private:
1207  bool StepListCallback(IDResource r, void* userData);
1208  void BuildStepList(PluginID p);
1209  void AddImplicitSteps(int32 fromVersion, int32 toVersion) const;
1210  bool16 ExplicitStepsExist(int32 version) const;
1211  bool16 FindPath(int32 fromVersion, int32 toVersion, Schemas::MajorFormatVector *list = nil) const;
1212  bool16 FindSubpath(int32 fromVersion, int32 toVersion, Schemas::MajorFormatVector *resultList, bool16 reversed) const;
1213  void ClearList(Schemas::MajorFormatVector *list) const
1214  {if (list != nil) list->clear();}
1215  void AddToList(Schemas::MajorFormatVector *list, int32 step, bool16 reversed) const
1216  {if (list != nil) {if (reversed) list->insert(list->begin(), step); else list->push_back(step);}}
1217  void RemoveLastFromList(Schemas::MajorFormatVector *list, bool16 reversed) const
1218  {if (list != nil) {if (reversed) list->erase(list->begin()); else list->pop_back();}}
1219 
1220 private:
1221  static Schemas::MajorFormatVector::const_iterator FindInPath(Schemas::MajorFormatVector::const_iterator pStart,
1222  Schemas::MajorFormatVector::const_iterator pEnd, int32 majorVersion);
1223 
1224 private:
1225  PluginID fPluginID;
1226  mutable Schemas::PathStepVector fStepList;
1227 };
1228 
1229 
1230 //________________________________________________________________________________________
1231 // PluginManager
1232 //________________________________________________________________________________________
1239 {
1240 public:
1241  PluginManager()
1242  {}
1243  PluginManager(const Schemas::VersionVector& versions)
1244  {CollectPlugins(versions);}
1245 
1246  void SetPluginID(PluginID p)
1247  {fPluginID = p;}
1248 
1249  void SetVersionList(const Schemas::VersionVector& versions)
1250  {CollectPlugins(versions);}
1251  void SetDirectiveMgr(DirectiveManager &dm)
1252  {BuildDirectiveList(dm);}
1253 
1254  int32 CountPlugins() const
1255  {return fPluginList.size();}
1256  PluginID GetNthPlugin(int32 n) const;
1257  bool16 GetPluginVersion(Schemas::Version &v) const;
1258 
1259  int32 CountRemovedPlugins(const VersionID &fromVersion, const VersionID &toVersion) const;
1260  PluginID GetNthRemovedPlugin(const VersionID &fromVersion, const VersionID &toVersion, int32 n) const;
1261  int32 CountIgnoredPlugins(const VersionID &fromVersion, const VersionID &toVersion) const;
1262  PluginID GetNthIgnoredPlugin(const VersionID &fromVersion, const VersionID &toVersion, int32 n) const;
1263 
1264 public:
1265  static PluginID GetPluginID(IPMUnknown *tag);
1266 
1267 private:
1268  void CollectPlugins(const Schemas::VersionVector& versions);
1269  void BuildDirectiveList(DirectiveManager &dm);
1270 
1271 private:
1272  PluginID fPluginID;
1273  Schemas::PluginVector fPluginList;
1274  Schemas::DirectivePtrVector fDirectivePtrList;
1275 };
1276 
1277 
1278 #endif // __SchemaUtils__