InDesign SDK  20.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
DataWrapper.h
1 //========================================================================================
2 //
3 // $File$
4 //
5 // Owner: EricM
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 // A DataWrapper is a safe way of passing around information that is owned
24 // by a persistent object. It maintains a reference to the persistent object
25 // and a pointer to the data that is wanted. Thus, when the datawrapper leaves
26 // scope, the persistent object is released, which allows the datawrapper's data
27 // to be deleted.
28 //
29 //========================================================================================
30 
31 #ifndef __DATAWRAPPER__
32 #define __DATAWRAPPER__
33 
34 #include "VOSObject.h"
35 #include "WideString.h"
36 
37 
44 template <class DataT>
46 {
47  public:
48  DataWrapper() :fBlock(nil), fData(nil)
49  {}
50  DataWrapper(VOS_Object* block, const DataT* list) :fBlock(block), fData((DataT *)list)
51  { if (fBlock) fBlock->AddRef(); }
52 
53  DataWrapper(const DataWrapper<DataT>& copy) :fBlock(copy.fBlock), fData(copy.fData)
54  { if (fBlock) fBlock->AddRef(); }
55 
56  DataWrapper<DataT>& operator=(const DataWrapper<DataT>& copy);
57 
58  ~DataWrapper()
59  { if (fBlock) fBlock->Release(); }
60 
65  operator const DataT* () const
66  { return fData; }
67 
72  const DataT* get() const
73  { return fData; }
74 
75  private:
76  VOS_Object *fBlock;
77  DataT *fData;
78 };
79 
80 
81 
88 template <> class DataWrapper<textchar>
89 {
90  public:
91  DataWrapper() :fBlock(nil), fData(nil), fCharOffset(0)
92  {}
93  DataWrapper(VOS_Object* block, const WideString* str, int32 charOffset)
94  :fBlock(block), fData(str), fCharOffset(charOffset)
95  { if (fBlock) fBlock->AddRef(); }
96 
98  :fBlock(copy.fBlock), fData(copy.fData), fCharOffset(copy.fCharOffset)
99  { if (fBlock) fBlock->AddRef(); }
100 
101  DataWrapper<textchar>& operator=(const DataWrapper<textchar>& copy);
102 
103  ~DataWrapper()
104  { if (fBlock) fBlock->Release(); }
105 
109  operator const textchar*()
110  { return fData ? fData->GrabUTF16Buffer(nil) + fData->CodePointIndexToUTF16Index(fCharOffset) : nil; }
111 
117  WideString::const_iterator GetIteratorAt(int32 charOffset) const;
118 
121  void ClearOffset()
122  { fCharOffset = 0; }
123 
129  int32 CharOffsetToUTF16Offset(int32 charOffset) const;
130 
133  bool16 HasSurrogates() const
134  { return fData->HasMultiWordUnicode(); }
135 
138  const WideString* GetData() const
139  { return fData; }
140 
141  private:
142  DataWrapper<textchar>& operator+=(int32 /*charOffset*/); // not provided -- make the compiler catch this bad call...
143  DataWrapper<textchar>& operator-=(int32 /*charOffset*/); // not provided -- make the compiler catch this bad call...
144 
145  private:
146  VOS_Object *fBlock;
147  const WideString *fData;
148  int32 fCharOffset;
149 };
150 
151 
152 
153 #endif
154  // __DATAWRAPPER__
155