Jamba C++ API  6.3.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 
32 #if __clang__
33 #pragma clang diagnostic push
34 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
35 #endif
36 
39 inline std::string toUT8String(VstString16 const &iString)
40 {
41  std::wstring_convert<std::codecvt_utf8_utf16<Steinberg::char16>, Steinberg::char16> converter{};
42  return converter.to_bytes(iString);
43 
44 // Steinberg::String utf8Str(iString.c_str());
45 // utf8Str.toMultiByte(Steinberg::kCP_Utf8);
46 // return utf8Str.text8();
47 }
48 #if __clang__
49 #pragma clang diagnostic pop
50 #endif
51 
56 template<typename T>
57 std::string toUTF8String(T const &iValue, Steinberg::int32 iPrecision)
58 {
59  if constexpr(Utils::is_operator_write_to_ostream_defined<T>)
60  {
61  std::ostringstream s;
62  if(iPrecision >= 0)
63  {
64  s.precision(iPrecision);
65  s.setf(std::ios::fixed);
66  }
67  s << iValue;
68  return s.str();
69  }
70  else
71  return "";
72 }
73 
76 template<typename... Args>
77 VstString16 printf16(VstString16 const &iFormat, Args&& ...iArgs)
78 {
79  Steinberg::String s;
80  s.printf(iFormat.c_str(), std::forward<Args>(iArgs)...);
81  return s.text16();
82 }
83 
95 template<typename T, typename... Args>
96 std::shared_ptr<T> make_sfo(Args&& ...iArgs)
97 {
98  auto ptr = new T(std::forward<Args>(iArgs)...);
99  std::shared_ptr<T> sptr(ptr, [](T *p) { p->release(); });
100  return sptr;
101 }
102 
103 }
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:57
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:77
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:96
std::string toUT8String(VstString16 const &iString)
Converts a VstString16 to a regular std::string that is properly utf-8 encoded.
Definition: Utils.h:39