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