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