AAX SDK  2.4.1
Avid Audio Extensions Development Kit
AAX_CStateDisplayDelegate.h
Go to the documentation of this file.
1 /*================================================================================================*/
2 /*
3  *
4  * Copyright 2014-2017, 2019 by Avid Technology, Inc.
5  * All rights reserved.
6  *
7  * CONFIDENTIAL: This document contains confidential information. Do not
8  * read or examine this document unless you are an Avid Technology employee
9  * or have signed a non-disclosure agreement with Avid Technology which protects
10  * the confidentiality of this document. DO NOT DISCLOSE ANY INFORMATION
11  * CONTAINED IN THIS DOCUMENT TO ANY THIRD-PARTY WITHOUT THE PRIOR WRITTEN CONSENT
12  * OF Avid Technology, INC.
13  *
14  */
15 
22 /*================================================================================================*/
23 
24 
25 #ifndef AAX_CSTATEDISPLAYDELEGATE_H
26 #define AAX_CSTATEDISPLAYDELEGATE_H
27 
28 #include "AAX_IDisplayDelegate.h"
29 #include "AAX_CString.h"
30 
31 #include <vector>
32 #if defined(WINDOWS_VERSION) || defined(LINUX_VERSION)
33 #include <algorithm>
34 #endif
35 
36 
37 
47 template <typename T>
49 {
50 public:
58  explicit AAX_CStateDisplayDelegate( const char * iStateStrings[], T iMinState = 0 );
59 
68  explicit AAX_CStateDisplayDelegate( int32_t inNumStates, const char * iStateStrings[], T iMinState = 0 );
69 
75  explicit AAX_CStateDisplayDelegate( const std::vector<AAX_IString*>& iStateStrings, T iMinState = 0 );
76 
78 
79  //Virtual Overrides
81  bool ValueToString(T value, AAX_CString* valueString) const AAX_OVERRIDE;
82  bool ValueToString(T value, int32_t maxNumChars, AAX_CString* valueString) const AAX_OVERRIDE;
83  bool StringToValue(const AAX_CString& valueString, T* value) const AAX_OVERRIDE;
84 
85  //AAX_CStateDisplayDelegate
86  void AddShortenedStrings( const char * iStateStrings[], int iLength );
87  bool Compare( const AAX_CString& valueString, const AAX_CString& stateString ) const;
88 
89 private:
90  AAX_CStateDisplayDelegate(); //private contructor to prevent its use externally.
91 
92  T mMinState;
93  std::vector<AAX_CString> mStateStrings;
94 
95  struct StringTable
96  {
97  int mStrLength;
98  std::vector<AAX_CString> mStateStrings;
99  };
100  static bool StringTableSortFunc(struct StringTable i, struct StringTable j)
101  {
102  return (i.mStrLength < j.mStrLength);
103  }
104 
105  std::vector<struct StringTable> mShortenedStrings;
106 };
107 
108 template <typename T>
109 AAX_CStateDisplayDelegate<T>::AAX_CStateDisplayDelegate( const char * iStateStrings[], T iMinState /* = 0 */ )
110 {
111  mMinState = iMinState;
112  for ( int index = 0; iStateStrings[ index ] != 0; ++index )
113  mStateStrings.push_back( AAX_CString( iStateStrings[ index ] ) );
114 }
115 
116 template <typename T>
117 AAX_CStateDisplayDelegate<T>::AAX_CStateDisplayDelegate( int32_t inNumStates, const char * iStateStrings[], T iMinState /* = 0 */ )
118 {
119  mMinState = iMinState;
120  for ( int index = 0; (index < inNumStates) && (iStateStrings[ index ] != 0); ++index )
121  mStateStrings.push_back( AAX_CString( iStateStrings[ index ] ) );
122 }
123 
124 template <typename T>
125 AAX_CStateDisplayDelegate<T>::AAX_CStateDisplayDelegate( const std::vector<AAX_IString*>& iStateStrings, T iMinState /* = 0 */ )
126 {
127  mMinState = iMinState;
128  for ( std::vector<AAX_IString*>::const_iterator iter = iStateStrings.begin(); iter != iStateStrings.end(); ++iter )
129  {
130  if (*iter)
131  {
132  mStateStrings.push_back( *(*iter) );
133  }
134  }
135 }
136 
137 template <typename T>
139 {
140  mMinState = iOther.mMinState;
141 
142  std::vector<AAX_CString>::const_iterator iter = iOther.mStateStrings.begin();
143  for ( ; iter != iOther.mStateStrings.end(); ++iter )
144  mStateStrings.push_back( AAX_CString( *iter ) );
145 
146  if ( iOther.mShortenedStrings.size() > 0 )
147  {
148  for ( int i = 0; i < (int)iOther.mShortenedStrings.size(); i++ )
149  mShortenedStrings.push_back( iOther.mShortenedStrings.at(i) );
150  }
151 }
152 
153 template <typename T>
154 void AAX_CStateDisplayDelegate<T>::AddShortenedStrings( const char * iStateStrings[], int iStrLength )
155 {
156  struct StringTable shortendTable;
157  shortendTable.mStrLength = iStrLength;
158  for ( int index = 0; iStateStrings[ index ] != 0; ++index )
159  shortendTable.mStateStrings.push_back( AAX_CString( iStateStrings[ index ] ) );
160  mShortenedStrings.push_back(shortendTable);
161 
162  // keep structure sorted by str lengths
163  std::sort(mShortenedStrings.begin(), mShortenedStrings.end(), AAX_CStateDisplayDelegate::StringTableSortFunc );
164 }
165 
166 template <typename T>
168 {
169  return new AAX_CStateDisplayDelegate(*this);
170 }
171 
172 template <typename T>
174 {
175  T index = value - mMinState;
176  if ( index >= (T) 0 && index < (T) mStateStrings.size() )
177  {
178  *valueString = mStateStrings[ index ];
179  return true;
180  }
181 
182  return false;
183 }
184 
185 template <typename T>
186 bool AAX_CStateDisplayDelegate<T>::ValueToString(T value, int32_t maxNumChars, AAX_CString* valueString) const
187 {
188  // if we don't ahve any shortened strings, just return the full length version
189  if ( mShortenedStrings.size() == 0 )
190  return this->ValueToString(value, valueString);
191 
192  // iterate through shortened strings from longest to shortest
193  // taking the first set that is short enough
194  T index = value - mMinState;
195 
196  if ( index < (T) 0 || index >= (T) mStateStrings.size() )
197  return true;
198 
199  // first see if the normal string is short enough
200  if ( mStateStrings[ index ].Length() < uint32_t(maxNumChars) )
201  {
202  *valueString = mStateStrings[ index ];
203  return true;
204  }
205 
206  for ( int i = (int)mShortenedStrings.size()-1; i >= 0; i-- )
207  {
208  struct StringTable shortStrings = mShortenedStrings.at(i);
209  if ( shortStrings.mStrLength <= maxNumChars )
210  {
211  if ( index >= (T) 0 && index < (T) shortStrings.mStateStrings.size() )
212  {
213  *valueString = shortStrings.mStateStrings[ index ];
214  return true;
215  }
216  }
217  }
218 
219  // if we can't find one short enough, just use the shortest version we can find
220  struct StringTable shortestStrings = mShortenedStrings.at(0);
221  if ( index >= (T) 0 && index < (T) shortestStrings.mStateStrings.size() )
222  {
223  *valueString = shortestStrings.mStateStrings[ index ];
224  return true;
225  }
226 
227  return false;
228 }
229 
230 template <typename T>
231 bool AAX_CStateDisplayDelegate<T>::StringToValue(const AAX_CString& valueString, T* value) const
232 {
233  std::vector<AAX_CString>::const_iterator iter = mStateStrings.begin();
234  for ( T index = 0; iter != mStateStrings.end(); ++index, ++iter )
235  {
236  if (Compare(valueString,*iter))
237  {
238  *value = index + mMinState;
239  return true;
240  }
241  }
242 
243  *value = mMinState;
244  return false;
245 }
246 
247 template <typename T>
248 bool AAX_CStateDisplayDelegate<T>::Compare( const AAX_CString& valueString, const AAX_CString& stateString ) const
249 {
250  return valueString==stateString;
251 }
252 
253 
254 
255 
256 
257 #endif //AAX_CSTATEDISPLAYDELEGATE_H
#define AAX_OVERRIDE
override keyword macro
Definition: AAX.h:141
A generic AAX string class with similar functionality to std::string.
Defines the display behavior for a parameter.
A generic display format conforming to AAX_IDisplayDelegate.
Definition: AAX_CStateDisplayDelegate.h:49
void AddShortenedStrings(const char *iStateStrings[], int iLength)
Definition: AAX_CStateDisplayDelegate.h:154
bool StringToValue(const AAX_CString &valueString, T *value) const AAX_OVERRIDE
Converts a string to a real parameter value.
Definition: AAX_CStateDisplayDelegate.h:231
bool Compare(const AAX_CString &valueString, const AAX_CString &stateString) const
Definition: AAX_CStateDisplayDelegate.h:248
bool ValueToString(T value, AAX_CString *valueString) const AAX_OVERRIDE
Converts a real parameter value to a string representation.
Definition: AAX_CStateDisplayDelegate.h:173
AAX_IDisplayDelegate< T > * Clone() const AAX_OVERRIDE
Constructs and returns a copy of the display delegate.
Definition: AAX_CStateDisplayDelegate.h:167
A generic AAX string class with similar functionality to std::string
Definition: AAX_CString.h:46
Classes for parameter value string conversion.
Definition: AAX_IDisplayDelegate.h:69