Commit 3c1bd658 authored by Miro Knejp's avatar Miro Knejp Committed by Jacob Potter

Extract binary translation into a predefined support lib helper class

parent 5f3d02e0
...@@ -41,9 +41,7 @@ static_assert(__has_feature(objc_arc), "Djinni requires ARC to be enabled for th ...@@ -41,9 +41,7 @@ static_assert(__has_feature(objc_arc), "Djinni requires ARC to be enabled for th
std::vector<NSString *> _itemsTempVector; std::vector<NSString *> _itemsTempVector;
_itemsTempVector.reserve(itemList.items.size()); _itemsTempVector.reserve(itemList.items.size());
for (const auto & cppValue_0 : itemList.items) { for (const auto & cppValue_0 : itemList.items) {
NSString *objcValue_0 = [[NSString alloc] initWithBytes:cppValue_0.data() NSString *objcValue_0 = ::djinni::String::fromCpp(cppValue_0);
length:cppValue_0.length()
encoding:NSUTF8StringEncoding];
_itemsTempVector.push_back(objcValue_0); _itemsTempVector.push_back(objcValue_0);
} }
_items = [NSArray arrayWithObjects:&_itemsTempVector[0] count:_itemsTempVector.size()]; _items = [NSArray arrayWithObjects:&_itemsTempVector[0] count:_itemsTempVector.size()];
...@@ -56,7 +54,7 @@ static_assert(__has_feature(objc_arc), "Djinni requires ARC to be enabled for th ...@@ -56,7 +54,7 @@ static_assert(__has_feature(objc_arc), "Djinni requires ARC to be enabled for th
std::vector<std::string> items; std::vector<std::string> items;
items.reserve([_items count]); items.reserve([_items count]);
for (NSString *objcValue_0 in _items) { for (NSString *objcValue_0 in _items) {
std::string cppValue_0([objcValue_0 UTF8String], [objcValue_0 lengthOfBytesUsingEncoding:NSUTF8StringEncoding]); std::string cppValue_0 = ::djinni::String::toCpp(objcValue_0);
items.push_back(std::move(cppValue_0)); items.push_back(std::move(cppValue_0));
} }
return ::textsort::ItemList( return ::textsort::ItemList(
......
...@@ -626,7 +626,8 @@ class ObjcppGenerator(spec: Spec) extends Generator(spec) { ...@@ -626,7 +626,8 @@ class ObjcppGenerator(spec: Spec) extends Generator(spec) {
w.wl(s"std::chrono::duration_cast<std::chrono::duration<double>>($cppIdent.time_since_epoch()).count()];") w.wl(s"std::chrono::duration_cast<std::chrono::duration<double>>($cppIdent.time_since_epoch()).count()];")
} }
} }
case MBinary => w.wl(s"$objcType$objcIdent = [NSData dataWithBytes:(&$cppIdent[0]) length:($cppIdent.size())];") case MBinary =>
w.wl(s"$objcType$objcIdent = ::djinni::Binary::fromCpp($cppIdent);")
case MOptional => throw new AssertionError("optional should have been special cased") case MOptional => throw new AssertionError("optional should have been special cased")
case MList => case MList =>
val objcName = "objcValue_" + valueLevel val objcName = "objcValue_" + valueLevel
...@@ -735,8 +736,7 @@ class ObjcppGenerator(spec: Spec) extends Generator(spec) { ...@@ -735,8 +736,7 @@ class ObjcppGenerator(spec: Spec) extends Generator(spec) {
w.wl(s"$cppType $cppIdent = ::djinni::String::toCpp($objcIdent);") w.wl(s"$cppType $cppIdent = ::djinni::String::toCpp($objcIdent);")
case MDate => w.wl(s"$cppType $cppIdent = ::djinni::convert_date([$objcIdent timeIntervalSince1970]);") case MDate => w.wl(s"$cppType $cppIdent = ::djinni::convert_date([$objcIdent timeIntervalSince1970]);")
case MBinary => case MBinary =>
w.wl(s"$cppType $cppIdent([$objcIdent length]);") w.wl(s"$cppType $cppIdent = ::djinni::Binary::toCpp($objcIdent);")
w.wl(s"[$objcIdent getBytes:(static_cast<void *>($cppIdent.data())) length:[$objcIdent length]];")
case MOptional => throw new AssertionError("optional should have been special cased") case MOptional => throw new AssertionError("optional should have been special cased")
case MList => case MList =>
val cppName = "cppValue_" + valueLevel val cppName = "cppValue_" + valueLevel
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
#include <cstdint> #include <cstdint>
#include <string> #include <string>
#include <vector>
static_assert(__has_feature(objc_arc), "Djinni requires ARC to be enabled for this file"); static_assert(__has_feature(objc_arc), "Djinni requires ARC to be enabled for this file");
...@@ -110,4 +111,22 @@ struct String { ...@@ -110,4 +111,22 @@ struct String {
} }
}; };
struct Binary {
using CppType = std::vector<uint8_t>;
using ObjcType = NSData*;
using Boxed = Binary;
static CppType toCpp(ObjcType data) {
auto bytes = reinterpret_cast<const uint8_t*>(data.bytes);
return data.length > 0 ? CppType{bytes, bytes + data.length} : CppType{};
}
static ObjcType fromCpp(const CppType& bytes) {
assert(bytes.size() <= std::numeric_limits<NSUInteger>::max());
// Using the pointer from .data() on an empty vector is UB
return bytes.empty() ? [NSData data] : [NSData dataWithBytes:bytes.data() length:bytes.size()];
}
};
} // namespace djinni } // namespace djinni
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