Better alignas and swift example formatting fixes.

GitOrigin-RevId: f13cb9754b0f91975710662d30c87563de4d7641
This commit is contained in:
levlam 2018-02-12 20:21:29 +03:00
parent 35179d5b15
commit 72df55fb2d
3 changed files with 18 additions and 21 deletions

View File

@ -7,8 +7,8 @@ class TdClient {
let tdlibMainLoop = DispatchQueue(label: "TDLib") let tdlibMainLoop = DispatchQueue(label: "TDLib")
let tdlibQueryQueue = DispatchQueue(label: "TDLibQuery") let tdlibQueryQueue = DispatchQueue(label: "TDLibQuery")
var queryF = Dictionary<Int64, (Dictionary<String,Any>)->()>() var queryF = Dictionary<Int64, (Dictionary<String,Any>)->()>()
var updateF : ((Dictionary<String,Any>)->())? var updateF: ((Dictionary<String,Any>)->())?
var queryId : Int64 = 0 var queryId: Int64 = 0
func queryAsync(query: [String: Any], f: ((Dictionary<String,Any>)->())? = nil) { func queryAsync(query: [String: Any], f: ((Dictionary<String,Any>)->())? = nil) {
tdlibQueryQueue.async { tdlibQueryQueue.async {
@ -33,7 +33,6 @@ class TdClient {
} }
semaphore.wait() semaphore.wait()
return result return result
} }
init() { init() {
@ -48,7 +47,7 @@ class TdClient {
tdlibMainLoop.async { [weak self] in tdlibMainLoop.async { [weak self] in
while (true) { while (true) {
if let s = self { if let s = self {
if let res = td_json_client_receive(s.client,10) { if let res = td_json_client_receive(s.client, 10) {
let event = String(cString: res) let event = String(cString: res)
s.queryResultAsync(event) s.queryResultAsync(event)
} }
@ -62,7 +61,7 @@ class TdClient {
private func queryResultAsync(_ result: String) { private func queryResultAsync(_ result: String) {
tdlibQueryQueue.async { tdlibQueryQueue.async {
let json = try? JSONSerialization.jsonObject(with: result.data(using: .utf8)!, options:[]) let json = try? JSONSerialization.jsonObject(with: result.data(using: .utf8)!, options:[])
if let dictionary = json as? [String:Any] { if let dictionary = json as? [String:Any] {
if let extra = dictionary["@extra"] as? Int64 { if let extra = dictionary["@extra"] as? Int64 {
let index = self.queryF.index(forKey: extra)! let index = self.queryF.index(forKey: extra)!
self.queryF[index].value(dictionary) self.queryF[index].value(dictionary)
@ -75,7 +74,7 @@ class TdClient {
} }
} }
func to_json(_ obj: Any) -> String { func to_json(_ obj: Any) -> String {
do { do {
let obj = try JSONSerialization.data(withJSONObject: obj) let obj = try JSONSerialization.data(withJSONObject: obj)
return String(data: obj, encoding: .utf8)! return String(data: obj, encoding: .utf8)!
@ -98,7 +97,7 @@ func myReadLine() -> String {
} }
} }
func updateAuthorizationState(authState : Dictionary<String, Any>) { func updateAuthorizationState(authState: Dictionary<String, Any>) {
switch(authState["@type"] as! String) { switch(authState["@type"] as! String) {
case "authorizationStateWaitTdlibParameters": case "authorizationStateWaitTdlibParameters":
client.queryAsync(query:[ client.queryAsync(query:[
@ -125,12 +124,12 @@ func updateAuthorizationState(authState : Dictionary<String, Any>) {
client.queryAsync(query:["@type":"setAuthenticationPhoneNumber", "phone_number":phone], f:checkAuthError) client.queryAsync(query:["@type":"setAuthenticationPhoneNumber", "phone_number":phone], f:checkAuthError)
case "authorizationStateWaitCode": case "authorizationStateWaitCode":
var first_name : String = "" var first_name: String = ""
var last_name: String = "" var last_name: String = ""
var code : String = "" var code: String = ""
if let is_registered = authState["is_registered"] as? Bool, is_registered { if let is_registered = authState["is_registered"] as? Bool, is_registered {
} else { } else {
print("Enter your first name : ") print("Enter your first name: ")
first_name = myReadLine() first_name = myReadLine()
print("Enter your last name: ") print("Enter your last name: ")
last_name = myReadLine() last_name = myReadLine()
@ -169,5 +168,3 @@ client.run {
while true { while true {
sleep(1) sleep(1)
} }

View File

@ -8,7 +8,7 @@
namespace td { namespace td {
alignas(128) const unsigned char TlParser::empty_data[sizeof(UInt256)] = {}; // static zero-initialized alignas(4) const unsigned char TlParser::empty_data[sizeof(UInt256)] = {}; // static zero-initialized
void TlParser::set_error(const string &error_message) { void TlParser::set_error(const string &error_message) {
if (error.empty()) { if (error.empty()) {

View File

@ -33,7 +33,7 @@ class TlParser {
static constexpr size_t SMALL_DATA_ARRAY_SIZE = 6; static constexpr size_t SMALL_DATA_ARRAY_SIZE = 6;
std::array<int32, SMALL_DATA_ARRAY_SIZE> small_data_array; std::array<int32, SMALL_DATA_ARRAY_SIZE> small_data_array;
static const unsigned char empty_data[sizeof(UInt256)]; alignas(4) static const unsigned char empty_data[sizeof(UInt256)];
public: public:
explicit TlParser(Slice slice) { explicit TlParser(Slice slice) {