Jamba  3.1.0
ParamSerializers.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 pongasoft
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  *
16  * @author Yan Pujante
17  */
18 #pragma once
19 
20 #include <pongasoft/logging/logging.h>
21 #include <pluginterfaces/vst/vsttypes.h>
22 #include <base/source/fstreamer.h>
23 #include <string>
24 #include <iostream>
25 #include <memory>
26 #include <sstream>
27 
28 namespace pongasoft {
29 namespace VST {
30 
31 using namespace Steinberg;
32 using namespace Steinberg::Vst;
33 
42 template<typename T>
44 {
45 public:
46  using ParamType = T;
47  virtual tresult readFromStream(IBStreamer &iStreamer, ParamType &oValue) const = 0;
48  virtual tresult writeToStream(const ParamType &iValue, IBStreamer &oStreamer) const = 0;
49  // by default does nothing -- subclasses can override
50  virtual void writeToStream(ParamType const &iValue, std::ostream &oStream) const {}
51  virtual std::string toString(ParamType const &iValue, int32 iPrecision) const
52  {
53  std::ostringstream s;
54  s.precision(iPrecision);
55  s.setf(std::ios::fixed);
56  writeToStream(iValue, s);
57  return s.str();
58  }
59 };
60 
64 namespace IBStreamHelper {
65 
66 // readDouble - contrary to IBStreamer.readDouble, this method does NOT modify oValue if cannot be read
67 inline tresult readDouble(IBStreamer &iStreamer, double &oValue)
68 {
69  double value;
70  if(!iStreamer.readDouble(value))
71  return kResultFalse;
72  oValue = value;
73  return kResultOk;
74 }
75 
76 // readFloat - contrary to IBStreamer.readFloat, this method does NOT modify oValue if cannot be read
77 inline tresult readFloat(IBStreamer &iStreamer, float &oValue)
78 {
79  float value;
80  if(!iStreamer.readFloat(value))
81  return kResultFalse;
82  oValue = value;
83  return kResultOk;
84 }
85 
86 // readFloatArray - contrary to IBStreamer.readFloatArray, this method returns tresult and can use any Int type
87 template<typename Int>
88 inline tresult readFloatArray(IBStreamer &iStreamer, float *oValue, Int iCount)
89 {
90  for(Int i = 0; i < iCount; i++)
91  {
92  if(!iStreamer.readFloat(oValue[i]))
93  return kResultFalse;
94  }
95  return kResultOk;
96 }
97 
98 
99 // readInt64 - contrary to IBStreamer.readInt64, this method does NOT modify oValue if cannot be read
100 inline tresult readInt64(IBStreamer &iStreamer, int64 &oValue)
101 {
102  int64 value;
103  if(!iStreamer.readInt64(value))
104  return kResultFalse;
105  oValue = value;
106  return kResultOk;
107 }
108 
109 // readInt64 - contrary to IBStreamer.readInt64, this method does NOT modify oValue if cannot be read
110 inline tresult readInt64u(IBStreamer &iStreamer, uint64 &oValue)
111 {
112  uint64 value;
113  if(!iStreamer.readInt64u(value))
114  return kResultFalse;
115  oValue = value;
116  return kResultOk;
117 }
118 
119 // readInt32 - contrary to IBStreamer.readInt32, this method does NOT modify oValue if cannot be read
120 inline tresult readInt32(IBStreamer &iStreamer, int32 &oValue)
121 {
122  int32 value;
123  if(!iStreamer.readInt32(value))
124  return kResultFalse;
125  oValue = value;
126  return kResultOk;
127 }
128 
129 // readBool - contrary to IBStreamer.readBool, this method does NOT modify oValue if cannot be read
130 inline tresult readBool(IBStreamer &iStreamer, bool &oValue)
131 {
132  bool value;
133  if(!iStreamer.readBool(value))
134  return kResultFalse;
135  oValue = value;
136  return kResultOk;
137 }
138 
139 }
140 
144 class RawParamSerializer : public IParamSerializer<ParamValue>
145 {
146 public:
147  tresult readFromStream(IBStreamer &iStreamer, ParamType &oValue) const override
148  {
149  return IBStreamHelper::readDouble(iStreamer, oValue);
150  }
151 
152  tresult writeToStream(const ParamType &iValue, IBStreamer &oStreamer) const override
153  {
154  oStreamer.writeDouble(iValue);
155  return kResultOk;
156  }
157 
158  void writeToStream(ParamType const &iValue, std::ostream &oStream) const override
159  {
160  oStream << iValue;
161  }
162 };
163 
168 {
169 public:
170  tresult readFromStream(IBStreamer &iStreamer, ParamType &oValue) const override
171  {
172  return IBStreamHelper::readDouble(iStreamer, oValue);
173  }
174 
175  tresult writeToStream(const ParamType &iValue, IBStreamer &oStreamer) const override
176  {
177  oStreamer.writeDouble(iValue);
178  return kResultOk;
179  }
180 
181  void writeToStream(ParamType const &iValue, std::ostream &oStream) const override
182  {
183  oStream << iValue;
184  }
185 };
186 
192 template<int size = 128>
193 class CStringParamSerializer : public IParamSerializer<char[size]>
194 {
195 public:
196  using ParamType = char[size];
197 
198  // readFromStream
199  inline tresult readFromStream(IBStreamer &iStreamer, ParamType &oValue) const override
200  {
201  if(iStreamer.readRaw(static_cast<void*>(oValue), size) == size)
202  {
203  oValue[size - 1] = 0; // making sure it is null terminated
204  return kResultOk;
205  }
206  else
207  return kResultFalse;
208  }
209 
210  // writeToStream - IBStreamer
211  inline tresult writeToStream(const ParamType &iValue, IBStreamer &oStreamer) const override
212  {
213  if(oStreamer.writeRaw(static_cast<void const *>(iValue), size) == size)
214  return kResultOk;
215  else
216  return kResultFalse;
217  }
218 
219  // writeToStream - std::ostream
220  void writeToStream(ParamType const &iValue, std::ostream &oStream) const override
221  {
222  if(std::find(std::begin(iValue), std::end(iValue), 0) != std::end(iValue))
223  {
224  // this means that the string is null terminated... we are good
225  oStream << iValue;
226  }
227  else
228  {
229  char8 str[size];
230  std::copy(std::begin(iValue), std::end(iValue), std::begin(str));
231  str[size - 1] = 0; // make the copy null terminated
232  oStream << str;
233  DLOG_F(WARNING, "%s not properly null terminated!", str);
234  }
235  }
236 };
237 
238 }
239 }
Definition: ParamSerializers.h:167
tresult readDouble(IBStreamer &iStreamer, double &oValue)
Definition: ParamSerializers.h:67
tresult readFloat(IBStreamer &iStreamer, float &oValue)
Definition: ParamSerializers.h:77
virtual void writeToStream(ParamType const &iValue, std::ostream &oStream) const
Definition: ParamSerializers.h:50
tresult readInt64u(IBStreamer &iStreamer, uint64 &oValue)
Definition: ParamSerializers.h:110
Definition: Clock.h:22
Definition: ParamSerializers.h:144
ParamValue ParamType
Definition: ParamSerializers.h:46
tresult writeToStream(const ParamType &iValue, IBStreamer &oStreamer) const override
Definition: ParamSerializers.h:152
tresult readInt32(IBStreamer &iStreamer, int32 &oValue)
Definition: ParamSerializers.h:120
void writeToStream(ParamType const &iValue, std::ostream &oStream) const override
Definition: ParamSerializers.h:220
tresult readFloatArray(IBStreamer &iStreamer, float *oValue, Int iCount)
Definition: ParamSerializers.h:88
tresult writeToStream(const ParamType &iValue, IBStreamer &oStreamer) const override
Definition: ParamSerializers.h:211
Definition: ParamSerializers.h:193
void writeToStream(ParamType const &iValue, std::ostream &oStream) const override
Definition: ParamSerializers.h:158
tresult readFromStream(IBStreamer &iStreamer, ParamType &oValue) const override
Definition: ParamSerializers.h:199
tresult readBool(IBStreamer &iStreamer, bool &oValue)
Definition: ParamSerializers.h:130
char[size] ParamType
Definition: ParamSerializers.h:196
Definition: ParamSerializers.h:43
tresult readFromStream(IBStreamer &iStreamer, ParamType &oValue) const override
Definition: ParamSerializers.h:170
tresult writeToStream(const ParamType &iValue, IBStreamer &oStreamer) const override
Definition: ParamSerializers.h:175
tresult readInt64(IBStreamer &iStreamer, int64 &oValue)
Definition: ParamSerializers.h:100
virtual std::string toString(ParamType const &iValue, int32 iPrecision) const
Definition: ParamSerializers.h:51
void writeToStream(ParamType const &iValue, std::ostream &oStream) const override
Definition: ParamSerializers.h:181
tresult readFromStream(IBStreamer &iStreamer, ParamType &oValue) const override
Definition: ParamSerializers.h:147