AAX SDK  2.1.1
Avid Audio Extensions Development Kit
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AAX_CStateDisplayDelegate.h
Go to the documentation of this file.
1 /*================================================================================================*/
2 /*
3  *
4  * Copyright (c) 2009 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 Technolgy, 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 #ifdef WINDOWS_VERSION
33 #include <algorithm>
34 #endif
35 
36 
37 
47 template <typename T>
49 {
50 public:
51  AAX_CStateDisplayDelegate( const char * iStateStrings[], T iMinState = 0 );
53 
54  //Virtual Overrides
56  bool ValueToString(T value, AAX_CString* valueString) const;
57  bool ValueToString(T value, int32_t maxNumChars, AAX_CString* valueString) const;
58  bool StringToValue(const AAX_CString& valueString, T* value) const;
59 
60  void AddShortenedStrings( const char * iStateStrings[], int iLength );
61 
62  virtual bool Compare( const AAX_CString& valueString, const AAX_CString& stateString ) const;
63 
64 private:
65  AAX_CStateDisplayDelegate(); //private contructor to prevent its use externally.
66 
67  T mMinState;
68  std::vector<AAX_CString> mStateStrings;
69 
70  struct StringTable
71  {
72  int mStrLength;
73  std::vector<AAX_CString> mStateStrings;
74  };
75  static bool StringTableSortFunc(struct StringTable i, struct StringTable j)
76  {
77  return (i.mStrLength < j.mStrLength);
78  }
79 
80  std::vector<struct StringTable> mShortenedStrings;
81 };
82 
83 template <typename T>
84 AAX_CStateDisplayDelegate<T>::AAX_CStateDisplayDelegate( const char * iStateStrings[], T iMinState /* = 0 */ )
85 {
86  mMinState = iMinState;
87  for ( int index = 0; iStateStrings[ index ] != 0; ++index )
88  mStateStrings.push_back( AAX_CString( iStateStrings[ index ] ) );
89 }
90 
91 template <typename T>
93 {
94  mMinState = iOther.mMinState;
95 
96  std::vector<AAX_CString>::const_iterator iter = iOther.mStateStrings.begin();
97  for ( ; iter != iOther.mStateStrings.end(); ++iter )
98  mStateStrings.push_back( AAX_CString( *iter ) );
99 
100  if ( iOther.mShortenedStrings.size() > 0 )
101  {
102  for ( int i = 0; i < (int)iOther.mShortenedStrings.size(); i++ )
103  mShortenedStrings.push_back( iOther.mShortenedStrings.at(i) );
104  }
105 }
106 
107 template <typename T>
108 void AAX_CStateDisplayDelegate<T>::AddShortenedStrings( const char * iStateStrings[], int iStrLength )
109 {
110  struct StringTable shortendTable;
111  shortendTable.mStrLength = iStrLength;
112  for ( int index = 0; iStateStrings[ index ] != 0; ++index )
113  shortendTable.mStateStrings.push_back( AAX_CString( iStateStrings[ index ] ) );
114  mShortenedStrings.push_back(shortendTable);
115 
116  // keep structure sorted by str lengths
117  std::sort(mShortenedStrings.begin(), mShortenedStrings.end(), AAX_CStateDisplayDelegate::StringTableSortFunc );
118 }
119 
120 template <typename T>
122 {
123  return new AAX_CStateDisplayDelegate(*this);
124 }
125 
126 template <typename T>
128 {
129  T index = value - mMinState;
130  if ( index >= (T) 0 && index < (T) mStateStrings.size() )
131  {
132  *valueString = mStateStrings[ index ];
133  return true;
134  }
135 
136  return false;
137 }
138 
139 template <typename T>
140 bool AAX_CStateDisplayDelegate<T>::ValueToString(T value, int32_t maxNumChars, AAX_CString* valueString) const
141 {
142  // if we don't ahve any shortened strings, just return the full length version
143  if ( mShortenedStrings.size() == 0 )
144  return this->ValueToString(value, valueString);
145 
146  // iterate through shortened strings from longest to shortest
147  // taking the first set that is short enough
148  T index = value - mMinState;
149 
150  if ( index < (T) 0 || index >= (T) mStateStrings.size() )
151  return true;
152 
153  // first see if the normal string is short enough
154  if ( mStateStrings[ index ].Length() < uint32_t(maxNumChars) )
155  {
156  *valueString = mStateStrings[ index ];
157  return true;
158  }
159 
160  for ( int i = (int)mShortenedStrings.size()-1; i >= 0; i-- )
161  {
162  struct StringTable shortStrings = mShortenedStrings.at(i);
163  if ( shortStrings.mStrLength <= maxNumChars )
164  {
165  if ( index >= (T) 0 && index < (T) shortStrings.mStateStrings.size() )
166  {
167  *valueString = shortStrings.mStateStrings[ index ];
168  return true;
169  }
170  }
171  }
172 
173  // if we can't find one short enough, just use the shortest version we can find
174  struct StringTable shortestStrings = mShortenedStrings.at(0);
175  if ( index >= (T) 0 && index < (T) shortestStrings.mStateStrings.size() )
176  {
177  *valueString = shortestStrings.mStateStrings[ index ];
178  return true;
179  }
180 
181  return false;
182 }
183 
184 template <typename T>
185 bool AAX_CStateDisplayDelegate<T>::StringToValue(const AAX_CString& valueString, T* value) const
186 {
187  std::vector<AAX_CString>::const_iterator iter = mStateStrings.begin();
188  for ( T index = 0; iter != mStateStrings.end(); ++index, ++iter )
189  {
190  if (Compare(valueString,*iter))
191  {
192  *value = index + mMinState;
193  return true;
194  }
195  }
196 
197  *value = mMinState;
198  return false;
199 }
200 
201 template <typename T>
202 bool AAX_CStateDisplayDelegate<T>::Compare( const AAX_CString& valueString, const AAX_CString& stateString ) const
203 {
204  return valueString==stateString;
205 }
206 
207 
208 
209 
210 
211 #endif //AAX_CSTATEDISPLAYDELEGATE_H
A generic display format conforming to AAX_IDisplayDelegate.
Definition: AAX_CStateDisplayDelegate.h:48
Classes for parameter value string conversion.
Definition: AAX_IDisplayDelegate.h:68
bool ValueToString(T value, AAX_CString *valueString) const
Converts a real parameter value to a string representation.
Definition: AAX_CStateDisplayDelegate.h:127
virtual bool Compare(const AAX_CString &valueString, const AAX_CString &stateString) const
Definition: AAX_CStateDisplayDelegate.h:202
Defines the display behavior for a parameter.
A generic AAX string class with similar functionality to std::string.
Definition: AAX_CString.h:35
bool StringToValue(const AAX_CString &valueString, T *value) const
Converts a string to a real parameter value.
Definition: AAX_CStateDisplayDelegate.h:185
AAX_IDisplayDelegate< T > * Clone() const
Constructs and returns a copy of the display delegate.
Definition: AAX_CStateDisplayDelegate.h:121
A generic AAX string class with similar functionality to std::string.
void AddShortenedStrings(const char *iStateStrings[], int iLength)
Definition: AAX_CStateDisplayDelegate.h:108