Commit 5f3d02e0 authored by Miro Knejp's avatar Miro Knejp Committed by Jacob Potter

Extract string translation into a predefined support lib helper class

parent 477d629a
......@@ -619,10 +619,8 @@ class ObjcppGenerator(spec: Spec) extends Generator(spec) {
case "f64" => w.wl(s"$objcType$objcIdent = ::djinni::F64$boxed::fromCpp($cppIdent);")
case "bool" => w.wl(s"$objcType$objcIdent = ::djinni::Bool$boxed::fromCpp($cppIdent);")
}
case MString => w.wl(s"$objcType$objcIdent = [[NSString alloc] initWithBytes:$cppIdent.data()").nestedN(2) {
w.wl(s"length:$cppIdent.length()")
w.wl("encoding:NSUTF8StringEncoding];")
}
case MString =>
w.wl(s"$objcType$objcIdent = ::djinni::String::fromCpp($cppIdent);")
case MDate => {
w.wl(s"$objcType$objcIdent = [NSDate dateWithTimeIntervalSince1970:").nestedN(2) {
w.wl(s"std::chrono::duration_cast<std::chrono::duration<double>>($cppIdent.time_since_epoch()).count()];")
......@@ -733,7 +731,8 @@ class ObjcppGenerator(spec: Spec) extends Generator(spec) {
case "f64" => w.wl(s"$cppType $cppIdent = ::djinni::F64$boxed::toCpp($objcIdent);")
case "bool" => w.wl(s"$cppType $cppIdent = ::djinni::Bool$boxed::toCpp($objcIdent);")
}
case MString => w.wl(s"$cppType $cppIdent([$objcIdent UTF8String], [$objcIdent lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);")
case MString =>
w.wl(s"$cppType $cppIdent = ::djinni::String::toCpp($objcIdent);")
case MDate => w.wl(s"$cppType $cppIdent = ::djinni::convert_date([$objcIdent timeIntervalSince1970]);")
case MBinary =>
w.wl(s"$cppType $cppIdent([$objcIdent length]);")
......
......@@ -9,6 +9,7 @@
#pragma once
#import <Foundation/Foundation.h>
#include <cstdint>
#include <string>
static_assert(__has_feature(objc_arc), "Djinni requires ARC to be enabled for this file");
......@@ -92,4 +93,21 @@ struct Enum {
};
};
struct String {
using CppType = std::string;
using ObjcType = NSString*;
using Boxed = String;
static CppType toCpp(ObjcType string) {
return {[string UTF8String], [string lengthOfBytesUsingEncoding:NSUTF8StringEncoding]};
}
static ObjcType fromCpp(const CppType& string) {
assert(string.size() <= std::numeric_limits<NSUInteger>::max());
// Using the pointer from .data() on an empty string is UB
return string.empty() ? @"" : [[NSString alloc] initWithBytes:string.data() length:string.size() encoding:NSUTF8StringEncoding];
}
};
} // namespace djinni
......@@ -28,9 +28,7 @@ ClientReturnedRecord ClientInterfaceObjcProxy::get_record (int64_t record_id, co
{
@autoreleasepool {
int64_t cpp_record_id = ::djinni::I64::fromCpp(record_id);
NSString *cpp_utf8string = [[NSString alloc] initWithBytes:utf8string.data()
length:utf8string.length()
encoding:NSUTF8StringEncoding];
NSString *cpp_utf8string = ::djinni::String::fromCpp(utf8string);
DBClientReturnedRecord * objcRet = [_objcRef getRecord:cpp_record_id utf8string:cpp_utf8string];
ClientReturnedRecord cppRet = std::move([objcRet cppClientReturnedRecord]);
return cppRet;
......
......@@ -34,9 +34,7 @@ static_assert(__has_feature(objc_arc), "Djinni requires ARC to be enabled for th
{
if (self = [super init]) {
_recordId = ::djinni::I64::fromCpp(clientReturnedRecord.record_id);
_content = [[NSString alloc] initWithBytes:clientReturnedRecord.content.data()
length:clientReturnedRecord.content.length()
encoding:NSUTF8StringEncoding];
_content = ::djinni::String::fromCpp(clientReturnedRecord.content);
}
return self;
}
......@@ -44,7 +42,7 @@ static_assert(__has_feature(objc_arc), "Djinni requires ARC to be enabled for th
- (ClientReturnedRecord)cppClientReturnedRecord
{
int64_t recordId = ::djinni::I64::toCpp(_recordId);
std::string content([_content UTF8String], [_content lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
std::string content = ::djinni::String::toCpp(_content);
return ClientReturnedRecord(
std::move(recordId),
std::move(content));
......
......@@ -58,9 +58,7 @@ static_assert(__has_feature(objc_arc), "Djinni requires ARC to be enabled for th
{
if (self = [super init]) {
_someInteger = ::djinni::I32::fromCpp(constants.some_integer);
_someString = [[NSString alloc] initWithBytes:constants.some_string.data()
length:constants.some_string.length()
encoding:NSUTF8StringEncoding];
_someString = ::djinni::String::fromCpp(constants.some_string);
}
return self;
}
......@@ -68,7 +66,7 @@ static_assert(__has_feature(objc_arc), "Djinni requires ARC to be enabled for th
- (Constants)cppConstants
{
int32_t someInteger = ::djinni::I32::toCpp(_someInteger);
std::string someString([_someString UTF8String], [_someString lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
std::string someString = ::djinni::String::toCpp(_someString);
return Constants(
std::move(someInteger),
std::move(someString));
......
......@@ -47,9 +47,7 @@ static_assert(__has_feature(objc_arc), "Djinni requires ARC to be enabled for th
std::vector<NSDate *> _datesByIdTempValueVector;
_datesByIdTempValueVector.reserve(mapDateRecord.dates_by_id.size());
for (const auto & cppPair_0 : mapDateRecord.dates_by_id) {
NSString *objcKey_0 = [[NSString alloc] initWithBytes:cppPair_0.first.data()
length:cppPair_0.first.length()
encoding:NSUTF8StringEncoding];
NSString *objcKey_0 = ::djinni::String::fromCpp(cppPair_0.first);
NSDate *objcValue_0 = [NSDate dateWithTimeIntervalSince1970:
std::chrono::duration_cast<std::chrono::duration<double>>(cppPair_0.second.time_since_epoch()).count()];
_datesByIdTempKeyVector.push_back(objcKey_0);
......@@ -64,7 +62,7 @@ static_assert(__has_feature(objc_arc), "Djinni requires ARC to be enabled for th
{
std::unordered_map<std::string, std::chrono::system_clock::time_point> datesById;
for (id objcKey_0 in _datesById) {
std::string cppKey_0([objcKey_0 UTF8String], [objcKey_0 lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
std::string cppKey_0 = ::djinni::String::toCpp(objcKey_0);
std::chrono::system_clock::time_point cppValue_0 = ::djinni::convert_date([[_datesById objectForKey:objcKey_0] timeIntervalSince1970]);
datesById.emplace(std::move(cppKey_0), std::move(cppValue_0));
}
......
......@@ -57,9 +57,7 @@ static_assert(__has_feature(objc_arc), "Djinni requires ARC to be enabled for th
std::vector<NSNumber *> objcValue_0TempValueVector;
objcValue_0TempValueVector.reserve(cppValue_0.size());
for (const auto & cppPair_1 : cppValue_0) {
NSString *objcKey_1 = [[NSString alloc] initWithBytes:cppPair_1.first.data()
length:cppPair_1.first.length()
encoding:NSUTF8StringEncoding];
NSString *objcKey_1 = ::djinni::String::fromCpp(cppPair_1.first);
NSNumber *objcValue_1 = ::djinni::I64::Boxed::fromCpp(cppPair_1.second);
objcValue_0TempKeyVector.push_back(objcKey_1);
objcValue_0TempValueVector.push_back(objcValue_1);
......@@ -79,7 +77,7 @@ static_assert(__has_feature(objc_arc), "Djinni requires ARC to be enabled for th
for (NSDictionary *objcValue_0 in _mapList) {
std::unordered_map<std::string, int64_t> cppValue_0;
for (id objcKey_1 in objcValue_0) {
std::string cppKey_1([objcKey_1 UTF8String], [objcKey_1 lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
std::string cppKey_1 = ::djinni::String::toCpp(objcKey_1);
int64_t cppValue_1 = ::djinni::I64::Boxed::toCpp([objcValue_0 objectForKey:objcKey_1]);
cppValue_0.emplace(std::move(cppKey_1), std::move(cppValue_1));
}
......
......@@ -47,9 +47,7 @@ static_assert(__has_feature(objc_arc), "Djinni requires ARC to be enabled for th
std::vector<NSNumber *> _mapTempValueVector;
_mapTempValueVector.reserve(mapRecord.map.size());
for (const auto & cppPair_0 : mapRecord.map) {
NSString *objcKey_0 = [[NSString alloc] initWithBytes:cppPair_0.first.data()
length:cppPair_0.first.length()
encoding:NSUTF8StringEncoding];
NSString *objcKey_0 = ::djinni::String::fromCpp(cppPair_0.first);
NSNumber *objcValue_0 = ::djinni::I64::Boxed::fromCpp(cppPair_0.second);
_mapTempKeyVector.push_back(objcKey_0);
_mapTempValueVector.push_back(objcValue_0);
......@@ -63,7 +61,7 @@ static_assert(__has_feature(objc_arc), "Djinni requires ARC to be enabled for th
{
std::unordered_map<std::string, int64_t> map;
for (id objcKey_0 in _map) {
std::string cppKey_0([objcKey_0 UTF8String], [objcKey_0 lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
std::string cppKey_0 = ::djinni::String::toCpp(objcKey_0);
int64_t cppValue_0 = ::djinni::I64::Boxed::toCpp([_map objectForKey:objcKey_0]);
map.emplace(std::move(cppKey_0), std::move(cppValue_0));
}
......
......@@ -51,9 +51,7 @@ static_assert(__has_feature(objc_arc), "Djinni requires ARC to be enabled for th
std::vector<NSString *> objcValue_0TempVector;
objcValue_0TempVector.reserve(cppValue_0.size());
for (const auto & cppValue_1 : cppValue_0) {
NSString *objcValue_1 = [[NSString alloc] initWithBytes:cppValue_1.data()
length:cppValue_1.length()
encoding:NSUTF8StringEncoding];
NSString *objcValue_1 = ::djinni::String::fromCpp(cppValue_1);
objcValue_0TempVector.push_back(objcValue_1);
}
NSSet *objcValue_0 = [NSSet setWithObjects:&objcValue_0TempVector[0] count:objcValue_0TempVector.size()];
......@@ -71,7 +69,7 @@ static_assert(__has_feature(objc_arc), "Djinni requires ARC to be enabled for th
for (NSSet *objcValue_0 in _setList) {
std::unordered_set<std::string> cppValue_0;
for (NSString *objcValue_1 in objcValue_0) {
std::string cppValue_1([objcValue_1 UTF8String], [objcValue_1 lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
std::string cppValue_1 = ::djinni::String::toCpp(objcValue_1);
cppValue_0.insert(std::move(cppValue_1));
}
setList.push_back(std::move(cppValue_0));
......
......@@ -34,9 +34,7 @@ static_assert(__has_feature(objc_arc), "Djinni requires ARC to be enabled for th
{
if (self = [super init]) {
_key1 = ::djinni::I32::fromCpp(recordWithDerivings.key1);
_key2 = [[NSString alloc] initWithBytes:recordWithDerivings.key2.data()
length:recordWithDerivings.key2.length()
encoding:NSUTF8StringEncoding];
_key2 = ::djinni::String::fromCpp(recordWithDerivings.key2);
}
return self;
}
......@@ -44,7 +42,7 @@ static_assert(__has_feature(objc_arc), "Djinni requires ARC to be enabled for th
- (RecordWithDerivings)cppRecordWithDerivings
{
int32_t key1 = ::djinni::I32::toCpp(_key1);
std::string key2([_key2 UTF8String], [_key2 lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
std::string key2 = ::djinni::String::toCpp(_key2);
return RecordWithDerivings(
std::move(key1),
std::move(key2));
......
......@@ -41,9 +41,7 @@ static_assert(__has_feature(objc_arc), "Djinni requires ARC to be enabled for th
std::vector<NSString *> _setTempVector;
_setTempVector.reserve(setRecord.set.size());
for (const auto & cppValue_0 : setRecord.set) {
NSString *objcValue_0 = [[NSString alloc] initWithBytes:cppValue_0.data()
length:cppValue_0.length()
encoding:NSUTF8StringEncoding];
NSString *objcValue_0 = ::djinni::String::fromCpp(cppValue_0);
_setTempVector.push_back(objcValue_0);
}
_set = [NSSet setWithObjects:&_setTempVector[0] count:_setTempVector.size()];
......@@ -55,7 +53,7 @@ static_assert(__has_feature(objc_arc), "Djinni requires ARC to be enabled for th
{
std::unordered_set<std::string> set;
for (NSString *objcValue_0 in _set) {
std::string cppValue_0([objcValue_0 UTF8String], [objcValue_0 lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
std::string cppValue_0 = ::djinni::String::toCpp(objcValue_0);
set.insert(std::move(cppValue_0));
}
return SetRecord(
......
......@@ -106,9 +106,7 @@ static_assert(__has_feature(objc_arc), "Djinni requires ARC to be enabled for th
std::vector<NSNumber *> objcRetTempValueVector;
objcRetTempValueVector.reserve(cppRet.size());
for (const auto & cppPair_0 : cppRet) {
NSString *objcKey_0 = [[NSString alloc] initWithBytes:cppPair_0.first.data()
length:cppPair_0.first.length()
encoding:NSUTF8StringEncoding];
NSString *objcKey_0 = ::djinni::String::fromCpp(cppPair_0.first);
NSNumber *objcValue_0 = ::djinni::I64::Boxed::fromCpp(cppPair_0.second);
objcRetTempKeyVector.push_back(objcKey_0);
objcRetTempValueVector.push_back(objcValue_0);
......@@ -122,7 +120,7 @@ static_assert(__has_feature(objc_arc), "Djinni requires ARC to be enabled for th
try {
std::unordered_map<std::string, int64_t> cppM;
for (id objcKey_0 in m) {
std::string cppKey_0([objcKey_0 UTF8String], [objcKey_0 lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
std::string cppKey_0 = ::djinni::String::toCpp(objcKey_0);
int64_t cppValue_0 = ::djinni::I64::Boxed::toCpp([m objectForKey:objcKey_0]);
cppM.emplace(std::move(cppKey_0), std::move(cppValue_0));
}
......@@ -140,9 +138,7 @@ static_assert(__has_feature(objc_arc), "Djinni requires ARC to be enabled for th
std::vector<NSNumber *> objcRetTempValueVector;
objcRetTempValueVector.reserve(cppRet.size());
for (const auto & cppPair_0 : cppRet) {
NSString *objcKey_0 = [[NSString alloc] initWithBytes:cppPair_0.first.data()
length:cppPair_0.first.length()
encoding:NSUTF8StringEncoding];
NSString *objcKey_0 = ::djinni::String::fromCpp(cppPair_0.first);
NSNumber *objcValue_0 = ::djinni::I64::Boxed::fromCpp(cppPair_0.second);
objcRetTempKeyVector.push_back(objcKey_0);
objcRetTempValueVector.push_back(objcValue_0);
......@@ -156,7 +152,7 @@ static_assert(__has_feature(objc_arc), "Djinni requires ARC to be enabled for th
try {
std::unordered_map<std::string, int64_t> cppM;
for (id objcKey_0 in m) {
std::string cppKey_0([objcKey_0 UTF8String], [objcKey_0 lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
std::string cppKey_0 = ::djinni::String::toCpp(objcKey_0);
int64_t cppValue_0 = ::djinni::I64::Boxed::toCpp([m objectForKey:objcKey_0]);
cppM.emplace(std::move(cppKey_0), std::move(cppValue_0));
}
......@@ -202,7 +198,7 @@ static_assert(__has_feature(objc_arc), "Djinni requires ARC to be enabled for th
std::unordered_map<color, std::string> cppM;
for (id objcKey_0 in m) {
color cppKey_0 = ::djinni::Enum<color, DBColor>::Boxed::toCpp(objcKey_0);
std::string cppValue_0([[m objectForKey:objcKey_0] UTF8String], [[m objectForKey:objcKey_0] lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
std::string cppValue_0 = ::djinni::String::toCpp([m objectForKey:objcKey_0]);
cppM.emplace(std::move(cppKey_0), std::move(cppValue_0));
}
TestHelpers::check_enum_map(std::move(cppM));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment