Jamba C++ API  4.4.0
Utils.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 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>
28 
29 namespace pongasoft::VST::VstUtils {
30 
34 inline std::string toUT8String(VstString16 const &iString)
35 {
36  std::wstring_convert<std::codecvt_utf8_utf16<Steinberg::char16>, Steinberg::char16> converter{};
37  return converter.to_bytes(iString);
38 
39 // Steinberg::String utf8Str(iString.c_str());
40 // utf8Str.toMultiByte(Steinberg::kCP_Utf8);
41 // return utf8Str.text8();
42 }
43 
48 template<typename T>
49 std::string toUTF8String(T const &iValue, Steinberg::int32 iPrecision)
50 {
51  if constexpr(Utils::is_operator_write_to_ostream_defined<T>)
52  {
53  std::ostringstream s;
54  if(iPrecision >= 0)
55  {
56  s.precision(iPrecision);
57  s.setf(std::ios::fixed);
58  }
59  s << iValue;
60  return s.str();
61  }
62  else
63  return "";
64 }
65 
77 template<typename T, typename... Args>
78 std::shared_ptr<T> make_sfo(Args&& ...iArgs)
79 {
80  auto ptr = new T(std::forward<Args>(iArgs)...);
81  std::shared_ptr<T> sptr(ptr, [](T *p) { p->release(); });
82  return sptr;
83 }
84 
85 }
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:49
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
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:78
std::string toUT8String(VstString16 const &iString)
Converts a VstString16 to a regular std::string that is properly utf-8 encoded.
Definition: Utils.h:34