InDesign SDK  20.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ScriptIDEnumMap.h
1 //========================================================================================
2 //
3 // $File$
4 //
5 // Owner: Jonathan W. Brown
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 CONFIDENTIAL
24 //
25 //========================================================================================
26 
27 #ifndef __ScriptIDEnumMap__
28 #define __ScriptIDEnumMap__
29 
30 #include "IScriptErrorUtils.h"
31 #include "IScriptRequestData.h"
32 
35 template<class T>
37 {
38 public:
41  struct Entry
42  {
44  T t;
47  } ;
48 
53  ScriptIDMap( const Entry* firstEntry, size_t tableSize )
54  {
55  fMappingTable = firstEntry ;
56  fNumEntries = (int32)(tableSize / sizeof( Entry )) ;
57  }
58 
64  bool16 TranslateFromScriptID( T& t, const ScriptID& script_id ) const
65  {
66  for ( int32 i = 0 ; i < fNumEntries ; ++i )
67  {
68  if ( fMappingTable[i].id == script_id )
69  {
70  t = fMappingTable[i].t ;
71  return kTrue ;
72  }
73  }
74  return kFalse ;
75  }
76 
82  bool16 TranslateToScriptID( ScriptID& script_id, const T& t ) const
83  {
84  for ( int32 i = 0 ; i < fNumEntries ; ++i )
85  {
86  if ( fMappingTable[i].t == t )
87  {
88  script_id = fMappingTable[i].id ;
89  return kTrue ;
90  }
91  }
92  return kFalse ;
93  }
94 
102  ErrorCode GetForGetProperty( const IScript* script, IScriptRequestData* data, const ScriptID& propID, const T& t ) const
103  {
104  ScriptID script_id ;
105  if ( !TranslateToScriptID( script_id, t ) )
106  {
107  ASSERT_FAIL( "ScriptIDMap::TranslateToScriptID failed to find a scripting enum to match the current value of a property" ) ;
108  return kFailure ;
109  }
110 
111  ScriptData scriptData ;
112  scriptData.SetEnumeration( script_id ) ;
113  data->AppendReturnData( script, propID, scriptData ) ;
114  return kSuccess ;
115  }
116 
124  ErrorCode GetForSetProperty( const IScript* script, IScriptRequestData* data, const ScriptID& propID, T& t ) const
125  {
126  ScriptData scriptData ;
127  ErrorCode err = data->ExtractRequestData( propID, scriptData ) ;
128  if ( err != kSuccess ) return err ;
129 
130  ScriptID script_id ;
131  err = scriptData.GetEnumeration( &script_id ) ;
132  if ( err != kSuccess ) return Utils<IScriptErrorUtils>()->SetInvalidParameterErrorData( data, propID ) ;
133 
134  if ( !TranslateFromScriptID( t, script_id ) )
135  return Utils<IScriptErrorUtils>()->SetInvalidParameterErrorData( data, propID ) ;
136 
137  return kSuccess ;
138  }
139 
140 protected:
141  const Entry* fMappingTable ;
142  int32 fNumEntries ;
143 } ;
144 
145 //Here int32 represents any enum value
147 typedef ScriptIDMap<int32>::Entry ScriptIDEnumMapEntry ;
148 
149 #endif //__ScriptIDEnumMap__