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,13 +7,13 @@ class TdClient {
let tdlibMainLoop = DispatchQueue(label: "TDLib")
let tdlibQueryQueue = DispatchQueue(label: "TDLibQuery")
var queryF = Dictionary<Int64, (Dictionary<String,Any>)->()>()
var updateF : ((Dictionary<String,Any>)->())?
var queryId : Int64 = 0
var updateF: ((Dictionary<String,Any>)->())?
var queryId: Int64 = 0
func queryAsync(query: [String: Any], f: ((Dictionary<String,Any>)->())? = nil) {
tdlibQueryQueue.async {
var newQuery = query
if f != nil {
let nextQueryId = self.queryId + 1
newQuery["@extra"] = nextQueryId
@ -23,7 +23,7 @@ class TdClient {
td_json_client_send(self.client, to_json(newQuery))
}
}
func querySync(query: [String: Any]) -> Dictionary<String,Any> {
let semaphore = DispatchSemaphore(value:0)
var result = Dictionary<String,Any>()
@ -33,9 +33,8 @@ class TdClient {
}
semaphore.wait()
return result
}
init() {
}
@ -48,7 +47,7 @@ class TdClient {
tdlibMainLoop.async { [weak self] in
while (true) {
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)
s.queryResultAsync(event)
}
@ -58,11 +57,11 @@ class TdClient {
}
}
}
private func queryResultAsync(_ result: String) {
tdlibQueryQueue.async {
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 {
let index = self.queryF.index(forKey: extra)!
self.queryF[index].value(dictionary)
@ -75,7 +74,7 @@ class TdClient {
}
}
func to_json(_ obj: Any) -> String {
func to_json(_ obj: Any) -> String {
do {
let obj = try JSONSerialization.data(withJSONObject: obj)
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) {
case "authorizationStateWaitTdlibParameters":
client.queryAsync(query:[
@ -118,19 +117,19 @@ func updateAuthorizationState(authState : Dictionary<String, Any>) {
case "authorizationStateWaitEncryptionKey":
client.queryAsync(query: ["@type":"checkDatabaseEncryptionKey", "key":"cucumber"])
case "authorizationStateWaitPhoneNumber":
print("Enter your phone: ")
let phone = myReadLine()
client.queryAsync(query:["@type":"setAuthenticationPhoneNumber", "phone_number":phone], f:checkAuthError)
case "authorizationStateWaitCode":
var first_name : String = ""
var first_name: String = ""
var last_name: String = ""
var code : String = ""
var code: String = ""
if let is_registered = authState["is_registered"] as? Bool, is_registered {
} else {
print("Enter your first name : ")
print("Enter your first name: ")
first_name = myReadLine()
print("Enter your last name: ")
last_name = myReadLine()
@ -169,5 +168,3 @@ client.run {
while true {
sleep(1)
}

View File

@ -8,7 +8,7 @@
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) {
if (error.empty()) {

View File

@ -33,7 +33,7 @@ class TlParser {
static constexpr size_t SMALL_DATA_ARRAY_SIZE = 6;
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:
explicit TlParser(Slice slice) {