Jamba C++ API  6.2.0
Utils.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2023 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 
19 #pragma once
20 
21 #include "../Types.h"
22 #include <string>
23 #include <codecvt>
24 #include <locale>
25 #include <sstream>
26 #include <pluginterfaces/base/ftypes.h>
27 #include <base/source/fstring.h>
29 
30 namespace pongasoft::VST::VstUtils {
31 
35 inline std::string toUT8String(VstString16 const &iString)
36 {
37  std::wstring_convert<std::codecvt_utf8_utf16<Steinberg::char16>, Steinberg::char16> converter{};
38  return converter.to_bytes(iString);
39 
40 // Steinberg::String utf8Str(iString.c_str());
41 // utf8Str.toMultiByte(Steinberg::kCP_Utf8);
42 // return utf8Str.text8();
43 }
44 
49 template<typename T>
50 std::string toUTF8String(T const &iValue, Steinberg::int32 iPrecision)
51 {
52  if constexpr(Utils::is_operator_write_to_ostream_defined<T>)
53  {
54  std::ostringstream s;
55  if(iPrecision >= 0)
56  {
57  s.precision(iPrecision);
58  s.setf(std::ios::fixed);
59  }
60  s << iValue;
61  return s.str();
62  }
63  else
64  return "";
65 }
66 
69 template<typename... Args>
70 VstString16 printf16(VstString16 const &iFormat, Args&& ...iArgs)
71 {
72  Steinberg::String s;
73  s.printf(iFormat.c_str(), std::forward<Args>(iArgs)...);
74  return s.text16();
75 }
76 
88 template<typename T, typename... Args>
89 std::shared_ptr<T> make_sfo(Args&& ...iArgs)
90 {
91  auto ptr = new T(std::forward<Args>(iArgs)...);
92  std::shared_ptr<T> sptr(ptr, [](T *p) { p->release(); });
93  return sptr;
94 }
95 
96 }
std::string toUTF8String(T const &iValue, Steinberg::int32 iPrecision)
This generic function will determine (at compilation time) whether T can be written to an ostream and...
Definition: Utils.h:50
Definition: ExpiringDataCache.h:27
std::basic_string< Steinberg::char16 > VstString16
Strings made of char16 characters are represented by the native C++11 type std::basic_string<Steinber...
Definition: Types.h:43
VstString16 printf16(VstString16 const &iFormat, Args &&...iArgs)
Equivalent to printf but for VstString16
Definition: Utils.h:70
std::shared_ptr< T > make_sfo(Args &&...iArgs)
The VST SDK uses the concept of FObject (which are self contained reference counted objects) but requ...
Definition: Utils.h:89
std::string toUT8String(VstString16 const &iString)
Converts a VstString16 to a regular std::string that is properly utf-8 encoded.
Definition: Utils.h:35