Browse Source

Add user-defined literal suffixes for integer and float types.

tags/v2.0.0
Andrew Belt 3 years ago
parent
commit
c0275f0cbb
2 changed files with 51 additions and 31 deletions
  1. +41
    -20
      include/common.hpp
  2. +10
    -11
      src/common.cpp

+ 41
- 20
include/common.hpp View File

@@ -18,9 +18,6 @@
#include <logger.hpp>


namespace rack {


/** Attribute for deprecated functions and symbols.
E.g.

@@ -120,6 +117,47 @@ to get its size in bytes.
#endif


/** Helpful user-defined literals for specifying exact integer and float types.
Usage examples:
123_i8
-1234_u16
0x1000_i32
0b10000000_u64
12.3_f32
1e4_f64
*/
inline int8_t operator"" _i8(unsigned long long x) {return x;}
inline int16_t operator"" _i16(unsigned long long x) {return x;}
inline int32_t operator"" _i32(unsigned long long x) {return x;}
inline int64_t operator"" _i64(unsigned long long x) {return x;}
inline uint8_t operator"" _u8(unsigned long long x) {return x;}
inline uint16_t operator"" _u16(unsigned long long x) {return x;}
inline uint32_t operator"" _u32(unsigned long long x) {return x;}
inline uint64_t operator"" _u64(unsigned long long x) {return x;}
inline float operator"" _f32(long double x) {return x;}
inline double operator"" _f64(long double x) {return x;}


#if defined ARCH_WIN
// wchar_t on Windows should be 2 bytes
static_assert(sizeof(wchar_t) == 2);

// Windows C standard functions are ASCII-8 instead of UTF-8, so redirect these functions to wrappers which convert to UTF-8
#define fopen fopen_u8

extern "C" {
FILE* fopen_u8(const char* filename, const char* mode);
}

namespace std {
using ::fopen_u8;
}
#endif


namespace rack {


/** C#-style property constructor
Example:

@@ -213,20 +251,3 @@ extern const std::string API_VERSION;


} // namespace rack


#if defined ARCH_WIN
// wchar_t on Windows should be 2 bytes
static_assert(sizeof(wchar_t) == 2);

// Windows C standard functions are ASCII-8 instead of UTF-8, so redirect these functions to wrappers which convert to UTF-8
#define fopen fopen_u8

extern "C" {
FILE* fopen_u8(const char* filename, const char* mode);
}

namespace std {
using ::fopen_u8;
}
#endif

+ 10
- 11
src/common.cpp View File

@@ -2,6 +2,16 @@
#include <string.hpp>


#if defined ARCH_WIN
#include <windows.h>

FILE* fopen_u8(const char* filename, const char* mode) {
return _wfopen(rack::string::U8toU16(filename).c_str(), rack::string::U8toU16(mode).c_str());
}

#endif


namespace rack {


@@ -30,14 +40,3 @@ Exception::Exception(const char* format, ...) {


} // namespace rack


#if defined ARCH_WIN
#include <windows.h>

FILE* fopen_u8(const char* filename, const char* mode) {
return _wfopen(rack::string::U8toU16(filename).c_str(), rack::string::U8toU16(mode).c_str());
}


#endif

Loading…
Cancel
Save