/legacy/TL/TLMetaConstructor.mm

https://gitlab.com/iranjith4/Telegram · Objective C++ · 1040 lines · 1017 code · 23 blank · 0 comment · 154 complexity · 02bf78be2f0331223fbc069560731051 MD5 · raw file

  1. #include "TLMetaConstructor.h"
  2. #import "NSInputStream+TL.h"
  3. #import "NSOutputStream+TL.h"
  4. #include "TLMetaClassStore.h"
  5. #import "TLObject.h"
  6. TLMetaConstructor::TLMetaConstructor(int32_t name_, int32_t signature_, std::tr1::shared_ptr<std::vector<TLMetaField> > fields_, std::tr1::shared_ptr<TLMetaType> resultType_) :
  7. name(name_), signature(signature_), fields(fields_), resultType(resultType_)
  8. {
  9. fieldNameToIndex = std::tr1::shared_ptr<std::tr1::unordered_map<int32_t, int> >(new std::tr1::unordered_map<int32_t, int>());
  10. std::vector<TLMetaField>::iterator fieldsEnd = fields->end();
  11. int index = -1;
  12. for (std::vector<TLMetaField>::iterator it = fields->begin(); it != fieldsEnd; it++)
  13. {
  14. index++;
  15. fieldNameToIndex->insert(std::pair<int32_t, int>(it->name, index));
  16. }
  17. }
  18. TLMetaConstructor::~TLMetaConstructor()
  19. {
  20. }
  21. TLConstructedValue TLMetaConstructor::construct(NSInputStream *is, id<TLSerializationEnvironment> environment, __unused TLSerializationContext *context, __autoreleasing NSError **error)
  22. {
  23. std::tr1::shared_ptr<std::vector<TLConstructedValue> > values(new std::vector<TLConstructedValue>());
  24. std::vector<TLMetaField>::iterator fieldsEnd = fields->end();
  25. for (std::vector<TLMetaField>::iterator it = fields->begin(); it != fieldsEnd; it++)
  26. {
  27. int32_t signature = 0;
  28. if (it->type.boxed)
  29. signature = [is readInt32];
  30. else
  31. signature = it->type.unboxedConstructorSignature;
  32. if (signature == TL_UNIVERSAL_VECTOR_CONSTRUCTOR && it->type.unboxedConstructorSignature != 0)
  33. signature = it->type.unboxedConstructorSignature;
  34. TLConstructedValue value = TLMetaClassStore::constructValue(is, signature, environment, nil, error);
  35. if (error != nil && *error != nil)
  36. return TLConstructedValue();
  37. values->push_back(value);
  38. }
  39. id<TLObject> objectClass = TLMetaClassStore::getObjectClass(name);
  40. if (objectClass == nil)
  41. {
  42. if (error != NULL)
  43. {
  44. NSMutableDictionary *userInfo = [[NSMutableDictionary alloc] init];
  45. [userInfo setValue:[NSString stringWithFormat:@"Object with name %.8x not found", name] forKey:NSLocalizedDescriptionKey];
  46. *error = [[NSError alloc] initWithDomain:@"TL" code:-1 userInfo:userInfo];
  47. }
  48. return TLConstructedValue();
  49. }
  50. TLConstructedValue result;
  51. result.type = TLConstructedValueTypeObject;
  52. std::tr1::shared_ptr<TLMetaObject> metaObject(new TLMetaObject(fields, fieldNameToIndex, values));
  53. result.nativeObject = [objectClass TLbuildFromMetaObject:metaObject];
  54. return result;
  55. }
  56. static void writeValueWithType(NSOutputStream *os, TLConstructedValue const &value, TLMetaTypeArgument const &type)
  57. {
  58. switch (value.type)
  59. {
  60. case TLConstructedValueTypeEmpty:
  61. {
  62. switch (type.type->getCategory())
  63. {
  64. case TLMetaTypeCategoryObject:
  65. {
  66. if (type.boxed)
  67. [os writeInt32:TL_NULL_CONSTRUCTOR];
  68. break;
  69. }
  70. case TLMetaTypeCategoryBuiltinInt32:
  71. {
  72. if (type.boxed)
  73. [os writeInt32:TL_INT32_CONSTRUCTOR];
  74. [os writeInt32:0];
  75. break;
  76. }
  77. case TLMetaTypeCategoryBuiltinInt64:
  78. {
  79. if (type.boxed)
  80. [os writeInt32:TL_INT64_CONSTRUCTOR];
  81. [os writeInt64:0];
  82. break;
  83. }
  84. case TLMetaTypeCategoryBuiltinInt128:
  85. {
  86. if (type.boxed)
  87. [os writeInt32:TL_INT128_CONSTRUCTOR];
  88. [os writeInt64:0];
  89. [os writeInt64:0];
  90. break;
  91. }
  92. case TLMetaTypeCategoryBuiltinInt256:
  93. {
  94. if (type.boxed)
  95. [os writeInt32:TL_INT256_CONSTRUCTOR];
  96. [os writeInt64:0];
  97. [os writeInt64:0];
  98. [os writeInt64:0];
  99. [os writeInt64:0];
  100. break;
  101. }
  102. case TLMetaTypeCategoryBuiltinString:
  103. {
  104. if (type.boxed)
  105. [os writeInt32:TL_STRING_CONSTRUCTOR];
  106. [os writeString:nil];
  107. break;
  108. }
  109. case TLMetaTypeCategoryBuiltinDouble:
  110. {
  111. if (type.boxed)
  112. [os writeInt32:TL_DOUBLE_CONSTRUCTOR];
  113. [os writeDouble:0.0];
  114. break;
  115. }
  116. case TLMetaTypeCategoryBuiltinBool:
  117. {
  118. if (type.boxed)
  119. [os writeInt32:TL_BOOL_FALSE_CONSTRUCTOR];
  120. break;
  121. }
  122. case TLMetaTypeCategoryBuiltinVector:
  123. {
  124. if (type.unboxedConstructorSignature != 0)
  125. {
  126. if (type.boxed)
  127. {
  128. #if TG_USE_UNIVERSAL_VECTOR
  129. [os writeInt32:TL_UNIVERSAL_VECTOR_CONSTRUCTOR];
  130. #else
  131. [os writeInt32:type.unboxedConstructorSignature];
  132. #endif
  133. }
  134. [os writeInt32:0];
  135. }
  136. else
  137. {
  138. TGLog(@"***** Unboxed constructor for 0x%.8x not found", type.type->getName());
  139. if (type.boxed)
  140. [os writeInt32:TL_NULL_CONSTRUCTOR];
  141. }
  142. break;
  143. }
  144. default:
  145. {
  146. TGLog(@"***** This should never happen 2");
  147. break;
  148. }
  149. }
  150. break;
  151. }
  152. case TLConstructedValueTypeObject:
  153. {
  154. switch (type.type->getCategory())
  155. {
  156. case TLMetaTypeCategoryObject:
  157. {
  158. TLMetaClassStore::serializeObject(os, value.nativeObject, type.boxed);
  159. break;
  160. }
  161. case TLMetaTypeCategoryBuiltinInt32:
  162. {
  163. if (type.boxed)
  164. [os writeInt32:TL_INT32_CONSTRUCTOR];
  165. [os writeInt32:0];
  166. break;
  167. }
  168. case TLMetaTypeCategoryBuiltinInt64:
  169. {
  170. if (type.boxed)
  171. [os writeInt32:TL_INT64_CONSTRUCTOR];
  172. [os writeInt64:0];
  173. break;
  174. }
  175. case TLMetaTypeCategoryBuiltinInt128:
  176. {
  177. if (type.boxed)
  178. [os writeInt32:TL_INT128_CONSTRUCTOR];
  179. [os writeInt64:0];
  180. [os writeInt64:0];
  181. break;
  182. }
  183. case TLMetaTypeCategoryBuiltinInt256:
  184. {
  185. if (type.boxed)
  186. [os writeInt32:TL_INT256_CONSTRUCTOR];
  187. [os writeInt64:0];
  188. [os writeInt64:0];
  189. [os writeInt64:0];
  190. [os writeInt64:0];
  191. break;
  192. }
  193. case TLMetaTypeCategoryBuiltinString:
  194. {
  195. if (type.boxed)
  196. [os writeInt32:TL_STRING_CONSTRUCTOR];
  197. [os writeString:nil];
  198. break;
  199. }
  200. case TLMetaTypeCategoryBuiltinDouble:
  201. {
  202. if (type.boxed)
  203. [os writeInt32:TL_DOUBLE_CONSTRUCTOR];
  204. [os writeDouble:0.0];
  205. break;
  206. }
  207. case TLMetaTypeCategoryBuiltinBool:
  208. {
  209. if (type.boxed)
  210. [os writeInt32:TL_BOOL_FALSE_CONSTRUCTOR];
  211. break;
  212. }
  213. case TLMetaTypeCategoryBuiltinVector:
  214. {
  215. if (type.unboxedConstructorSignature != 0)
  216. {
  217. if (type.boxed)
  218. [os writeInt32:type.unboxedConstructorSignature];
  219. [os writeInt32:0];
  220. }
  221. else
  222. {
  223. TGLog(@"***** Unboxed constructor for 0x%.8x not found", type.type->getName());
  224. if (type.boxed)
  225. [os writeInt32:TL_NULL_CONSTRUCTOR];
  226. }
  227. break;
  228. }
  229. default:
  230. {
  231. TGLog(@"***** This should never happen 3");
  232. break;
  233. }
  234. }
  235. break;
  236. }
  237. case TLConstructedValueTypePrimitiveInt32:
  238. {
  239. switch (type.type->getCategory())
  240. {
  241. case TLMetaTypeCategoryObject:
  242. {
  243. if (type.boxed)
  244. [os writeInt32:TL_NULL_CONSTRUCTOR];
  245. break;
  246. }
  247. case TLMetaTypeCategoryBuiltinInt32:
  248. {
  249. if (type.boxed)
  250. [os writeInt32:TL_INT32_CONSTRUCTOR];
  251. [os writeInt32:value.primitive.int32Value];
  252. break;
  253. }
  254. case TLMetaTypeCategoryBuiltinInt64:
  255. {
  256. if (type.boxed)
  257. [os writeInt32:TL_INT64_CONSTRUCTOR];
  258. [os writeInt64:(int64_t)value.primitive.int32Value];
  259. break;
  260. }
  261. case TLMetaTypeCategoryBuiltinInt128:
  262. {
  263. if (type.boxed)
  264. [os writeInt32:TL_INT128_CONSTRUCTOR];
  265. [os writeInt64:0];
  266. [os writeInt64:0];
  267. break;
  268. }
  269. case TLMetaTypeCategoryBuiltinInt256:
  270. {
  271. if (type.boxed)
  272. [os writeInt32:TL_INT256_CONSTRUCTOR];
  273. [os writeInt64:0];
  274. [os writeInt64:0];
  275. [os writeInt64:0];
  276. [os writeInt64:0];
  277. break;
  278. }
  279. case TLMetaTypeCategoryBuiltinString:
  280. {
  281. if (type.boxed)
  282. [os writeInt32:TL_STRING_CONSTRUCTOR];
  283. [os writeString:nil];
  284. break;
  285. }
  286. case TLMetaTypeCategoryBuiltinDouble:
  287. {
  288. if (type.boxed)
  289. [os writeInt32:TL_DOUBLE_CONSTRUCTOR];
  290. [os writeDouble:(double)value.primitive.int32Value];
  291. break;
  292. }
  293. case TLMetaTypeCategoryBuiltinBool:
  294. {
  295. if (type.boxed)
  296. [os writeInt32:value.primitive.int32Value != 0 ? TL_BOOL_TRUE_CONSTRUCTOR : TL_BOOL_FALSE_CONSTRUCTOR];
  297. break;
  298. }
  299. case TLMetaTypeCategoryBuiltinVector:
  300. {
  301. if (type.unboxedConstructorSignature != 0)
  302. {
  303. if (type.boxed)
  304. {
  305. #if TG_USE_UNIVERSAL_VECTOR
  306. [os writeInt32:TL_UNIVERSAL_VECTOR_CONSTRUCTOR];
  307. #else
  308. [os writeInt32:type.unboxedConstructorSignature];
  309. #endif
  310. }
  311. [os writeInt32:0];
  312. }
  313. else
  314. {
  315. TGLog(@"***** Unboxed constructor for 0x%.8x not found", type.type->getName());
  316. if (type.boxed)
  317. [os writeInt32:TL_NULL_CONSTRUCTOR];
  318. }
  319. break;
  320. }
  321. default:
  322. {
  323. TGLog(@"***** This should never happen 4");
  324. break;
  325. }
  326. }
  327. break;
  328. }
  329. case TLConstructedValueTypePrimitiveInt64:
  330. {
  331. switch (type.type->getCategory())
  332. {
  333. case TLMetaTypeCategoryObject:
  334. {
  335. if (type.boxed)
  336. [os writeInt32:TL_NULL_CONSTRUCTOR];
  337. break;
  338. }
  339. case TLMetaTypeCategoryBuiltinInt32:
  340. {
  341. if (type.boxed)
  342. [os writeInt32:TL_INT32_CONSTRUCTOR];
  343. [os writeInt32:(int32_t)value.primitive.int64Value];
  344. break;
  345. }
  346. case TLMetaTypeCategoryBuiltinInt64:
  347. {
  348. if (type.boxed)
  349. [os writeInt32:TL_INT64_CONSTRUCTOR];
  350. [os writeInt64:value.primitive.int64Value];
  351. break;
  352. }
  353. case TLMetaTypeCategoryBuiltinInt128:
  354. {
  355. if (type.boxed)
  356. [os writeInt32:TL_INT128_CONSTRUCTOR];
  357. [os writeInt64:0];
  358. [os writeInt64:0];
  359. break;
  360. }
  361. case TLMetaTypeCategoryBuiltinInt256:
  362. {
  363. if (type.boxed)
  364. [os writeInt32:TL_INT256_CONSTRUCTOR];
  365. [os writeInt64:0];
  366. [os writeInt64:0];
  367. [os writeInt64:0];
  368. [os writeInt64:0];
  369. break;
  370. }
  371. case TLMetaTypeCategoryBuiltinString:
  372. {
  373. if (type.boxed)
  374. [os writeInt32:TL_STRING_CONSTRUCTOR];
  375. [os writeString:nil];
  376. break;
  377. }
  378. case TLMetaTypeCategoryBuiltinDouble:
  379. {
  380. if (type.boxed)
  381. [os writeInt32:TL_DOUBLE_CONSTRUCTOR];
  382. [os writeDouble:(double)value.primitive.int64Value];
  383. break;
  384. }
  385. case TLMetaTypeCategoryBuiltinBool:
  386. {
  387. if (type.boxed)
  388. [os writeInt32:value.primitive.int64Value != 0 ? TL_BOOL_TRUE_CONSTRUCTOR : TL_BOOL_FALSE_CONSTRUCTOR];
  389. break;
  390. }
  391. case TLMetaTypeCategoryBuiltinVector:
  392. {
  393. if (type.unboxedConstructorSignature != 0)
  394. {
  395. if (type.boxed)
  396. {
  397. #if TG_USE_UNIVERSAL_VECTOR
  398. [os writeInt32:TL_UNIVERSAL_VECTOR_CONSTRUCTOR];
  399. #else
  400. [os writeInt32:type.unboxedConstructorSignature];
  401. #endif
  402. }
  403. [os writeInt32:0];
  404. }
  405. else
  406. {
  407. TGLog(@"***** Unboxed constructor for 0x%.8x not found", type.type->getName());
  408. if (type.boxed)
  409. [os writeInt32:TL_NULL_CONSTRUCTOR];
  410. }
  411. break;
  412. }
  413. default:
  414. {
  415. TGLog(@"***** This should never happen 5");
  416. break;
  417. }
  418. }
  419. break;
  420. }
  421. case TLConstructedValueTypePrimitiveDouble:
  422. {
  423. switch (type.type->getCategory())
  424. {
  425. case TLMetaTypeCategoryObject:
  426. {
  427. if (type.boxed)
  428. [os writeInt32:TL_NULL_CONSTRUCTOR];
  429. break;
  430. }
  431. case TLMetaTypeCategoryBuiltinInt32:
  432. {
  433. if (type.boxed)
  434. [os writeInt32:TL_INT32_CONSTRUCTOR];
  435. [os writeInt32:(int32_t)value.primitive.doubleValue];
  436. break;
  437. }
  438. case TLMetaTypeCategoryBuiltinInt64:
  439. {
  440. if (type.boxed)
  441. [os writeInt32:TL_INT64_CONSTRUCTOR];
  442. [os writeInt64:(int64_t)value.primitive.doubleValue];
  443. break;
  444. }
  445. case TLMetaTypeCategoryBuiltinInt128:
  446. {
  447. if (type.boxed)
  448. [os writeInt32:TL_INT128_CONSTRUCTOR];
  449. [os writeInt64:0];
  450. [os writeInt64:0];
  451. break;
  452. }
  453. case TLMetaTypeCategoryBuiltinInt256:
  454. {
  455. if (type.boxed)
  456. [os writeInt32:TL_INT256_CONSTRUCTOR];
  457. [os writeInt64:0];
  458. [os writeInt64:0];
  459. [os writeInt64:0];
  460. [os writeInt64:0];
  461. break;
  462. }
  463. case TLMetaTypeCategoryBuiltinString:
  464. {
  465. if (type.boxed)
  466. [os writeInt32:TL_STRING_CONSTRUCTOR];
  467. [os writeString:nil];
  468. break;
  469. }
  470. case TLMetaTypeCategoryBuiltinDouble:
  471. {
  472. if (type.boxed)
  473. [os writeInt32:TL_DOUBLE_CONSTRUCTOR];
  474. [os writeDouble:value.primitive.doubleValue];
  475. break;
  476. }
  477. case TLMetaTypeCategoryBuiltinBool:
  478. {
  479. if (type.boxed)
  480. [os writeInt32:value.primitive.doubleValue != 0.0 ? TL_BOOL_TRUE_CONSTRUCTOR : TL_BOOL_FALSE_CONSTRUCTOR];
  481. break;
  482. }
  483. case TLMetaTypeCategoryBuiltinVector:
  484. {
  485. if (type.unboxedConstructorSignature != 0)
  486. {
  487. if (type.boxed)
  488. {
  489. #if TG_USE_UNIVERSAL_VECTOR
  490. [os writeInt32:TL_UNIVERSAL_VECTOR_CONSTRUCTOR];
  491. #else
  492. [os writeInt32:type.unboxedConstructorSignature];
  493. #endif
  494. }
  495. [os writeInt32:0];
  496. }
  497. else
  498. {
  499. TGLog(@"***** Unboxed constructor for 0x%.8x not found", type.type->getName());
  500. if (type.boxed)
  501. [os writeInt32:TL_NULL_CONSTRUCTOR];
  502. }
  503. break;
  504. }
  505. default:
  506. {
  507. TGLog(@"***** This should never happen 6");
  508. break;
  509. }
  510. }
  511. break;
  512. }
  513. case TLConstructedValueTypePrimitiveBool:
  514. {
  515. switch (type.type->getCategory())
  516. {
  517. case TLMetaTypeCategoryObject:
  518. {
  519. if (type.boxed)
  520. [os writeInt32:TL_NULL_CONSTRUCTOR];
  521. break;
  522. }
  523. case TLMetaTypeCategoryBuiltinInt32:
  524. {
  525. if (type.boxed)
  526. [os writeInt32:TL_INT32_CONSTRUCTOR];
  527. [os writeInt32:(int32_t)(value.primitive.boolValue ? 1 : 0)];
  528. break;
  529. }
  530. case TLMetaTypeCategoryBuiltinInt64:
  531. {
  532. if (type.boxed)
  533. [os writeInt32:TL_INT64_CONSTRUCTOR];
  534. [os writeInt64:(int64_t)(value.primitive.boolValue ? 1 : 0)];
  535. break;
  536. }
  537. case TLMetaTypeCategoryBuiltinInt128:
  538. {
  539. if (type.boxed)
  540. [os writeInt32:TL_INT128_CONSTRUCTOR];
  541. [os writeInt64:0];
  542. [os writeInt64:0];
  543. break;
  544. }
  545. case TLMetaTypeCategoryBuiltinInt256:
  546. {
  547. if (type.boxed)
  548. [os writeInt32:TL_INT256_CONSTRUCTOR];
  549. [os writeInt64:0];
  550. [os writeInt64:0];
  551. [os writeInt64:0];
  552. [os writeInt64:0];
  553. break;
  554. }
  555. case TLMetaTypeCategoryBuiltinString:
  556. {
  557. if (type.boxed)
  558. [os writeInt32:TL_STRING_CONSTRUCTOR];
  559. [os writeString:nil];
  560. break;
  561. }
  562. case TLMetaTypeCategoryBuiltinDouble:
  563. {
  564. if (type.boxed)
  565. [os writeInt32:TL_DOUBLE_CONSTRUCTOR];
  566. [os writeDouble:(value.primitive.boolValue ? 1 : 0)];
  567. break;
  568. }
  569. case TLMetaTypeCategoryBuiltinBool:
  570. {
  571. if (type.boxed)
  572. [os writeInt32:value.primitive.boolValue ? TL_BOOL_TRUE_CONSTRUCTOR : TL_BOOL_FALSE_CONSTRUCTOR];
  573. break;
  574. }
  575. case TLMetaTypeCategoryBuiltinVector:
  576. {
  577. if (type.unboxedConstructorSignature != 0)
  578. {
  579. if (type.boxed)
  580. {
  581. #if TG_USE_UNIVERSAL_VECTOR
  582. [os writeInt32:TL_UNIVERSAL_VECTOR_CONSTRUCTOR];
  583. #else
  584. [os writeInt32:type.unboxedConstructorSignature];
  585. #endif
  586. }
  587. [os writeInt32:0];
  588. }
  589. else
  590. {
  591. TGLog(@"***** Unboxed constructor for 0x%.8x not found", type.type->getName());
  592. if (type.boxed)
  593. [os writeInt32:TL_NULL_CONSTRUCTOR];
  594. }
  595. break;
  596. }
  597. default:
  598. {
  599. TGLog(@"***** This should never happen 6");
  600. break;
  601. }
  602. }
  603. break;
  604. }
  605. case TLConstructedValueTypeString:
  606. {
  607. switch (type.type->getCategory())
  608. {
  609. case TLMetaTypeCategoryObject:
  610. {
  611. if (type.boxed)
  612. [os writeInt32:TL_NULL_CONSTRUCTOR];
  613. break;
  614. }
  615. case TLMetaTypeCategoryBuiltinInt32:
  616. {
  617. if (type.boxed)
  618. [os writeInt32:TL_INT32_CONSTRUCTOR];
  619. [os writeInt32:0];
  620. break;
  621. }
  622. case TLMetaTypeCategoryBuiltinInt64:
  623. {
  624. if (type.boxed)
  625. [os writeInt32:TL_INT64_CONSTRUCTOR];
  626. [os writeInt64:0];
  627. break;
  628. }
  629. case TLMetaTypeCategoryBuiltinInt128:
  630. {
  631. if (type.boxed)
  632. [os writeInt32:TL_INT128_CONSTRUCTOR];
  633. [os writeInt64:0];
  634. [os writeInt64:0];
  635. break;
  636. }
  637. case TLMetaTypeCategoryBuiltinInt256:
  638. {
  639. if (type.boxed)
  640. [os writeInt32:TL_INT256_CONSTRUCTOR];
  641. [os writeInt64:0];
  642. [os writeInt64:0];
  643. [os writeInt64:0];
  644. [os writeInt64:0];
  645. break;
  646. }
  647. case TLMetaTypeCategoryBuiltinString:
  648. {
  649. if (type.boxed)
  650. [os writeInt32:TL_STRING_CONSTRUCTOR];
  651. NSString *string = value.nativeObject;
  652. [os writeString:string];
  653. break;
  654. }
  655. case TLMetaTypeCategoryBuiltinBytes:
  656. {
  657. if (type.boxed)
  658. [os writeInt32:TL_BYTES_CONSTRUCTOR];
  659. NSString *string = value.nativeObject;
  660. [os writeBytes:[string dataUsingEncoding:NSUTF8StringEncoding]];
  661. break;
  662. }
  663. case TLMetaTypeCategoryBuiltinDouble:
  664. {
  665. if (type.boxed)
  666. [os writeInt32:TL_DOUBLE_CONSTRUCTOR];
  667. [os writeDouble:0.0];
  668. break;
  669. }
  670. case TLMetaTypeCategoryBuiltinBool:
  671. {
  672. if (type.boxed)
  673. [os writeInt32:TL_BOOL_FALSE_CONSTRUCTOR];
  674. break;
  675. }
  676. case TLMetaTypeCategoryBuiltinVector:
  677. {
  678. if (type.unboxedConstructorSignature != 0)
  679. {
  680. if (type.boxed)
  681. {
  682. #if TG_USE_UNIVERSAL_VECTOR
  683. [os writeInt32:TL_UNIVERSAL_VECTOR_CONSTRUCTOR];
  684. #else
  685. [os writeInt32:type.unboxedConstructorSignature];
  686. #endif
  687. }
  688. [os writeInt32:0];
  689. }
  690. else
  691. {
  692. TGLog(@"***** Unboxed constructor for 0x%.8x not found", type.type->getName());
  693. if (type.boxed)
  694. [os writeInt32:TL_NULL_CONSTRUCTOR];
  695. }
  696. break;
  697. }
  698. default:
  699. {
  700. TGLog(@"***** This should never happen 6");
  701. break;
  702. }
  703. }
  704. break;
  705. }
  706. case TLConstructedValueTypeBytes:
  707. {
  708. switch (type.type->getCategory())
  709. {
  710. case TLMetaTypeCategoryObject:
  711. {
  712. if (type.boxed)
  713. [os writeInt32:TL_NULL_CONSTRUCTOR];
  714. break;
  715. }
  716. case TLMetaTypeCategoryBuiltinInt32:
  717. {
  718. if (type.boxed)
  719. [os writeInt32:TL_INT32_CONSTRUCTOR];
  720. [os writeInt32:0];
  721. break;
  722. }
  723. case TLMetaTypeCategoryBuiltinInt64:
  724. {
  725. if (type.boxed)
  726. [os writeInt32:TL_INT64_CONSTRUCTOR];
  727. [os writeInt64:0];
  728. break;
  729. }
  730. case TLMetaTypeCategoryBuiltinInt128:
  731. {
  732. if (type.boxed)
  733. [os writeInt32:TL_INT128_CONSTRUCTOR];
  734. NSData *data = value.nativeObject;
  735. if (data.length == 16)
  736. {
  737. [os writeData:data];
  738. }
  739. else
  740. {
  741. [os writeInt64:0];
  742. [os writeInt64:0];
  743. }
  744. break;
  745. }
  746. case TLMetaTypeCategoryBuiltinInt256:
  747. {
  748. if (type.boxed)
  749. [os writeInt32:TL_INT256_CONSTRUCTOR];
  750. NSData *data = value.nativeObject;
  751. if (data.length == 32)
  752. {
  753. [os writeData:data];
  754. }
  755. else
  756. {
  757. [os writeInt64:0];
  758. [os writeInt64:0];
  759. [os writeInt64:0];
  760. [os writeInt64:0];
  761. }
  762. break;
  763. }
  764. case TLMetaTypeCategoryBuiltinString:
  765. {
  766. if (type.boxed)
  767. [os writeInt32:TL_STRING_CONSTRUCTOR];
  768. NSData *data = value.nativeObject;
  769. [os writeBytes:data];
  770. break;
  771. }
  772. case TLMetaTypeCategoryBuiltinBytes:
  773. {
  774. if (type.boxed)
  775. [os writeInt32:TL_BYTES_CONSTRUCTOR];
  776. NSData *data = value.nativeObject;
  777. [os writeBytes:data];
  778. break;
  779. }
  780. case TLMetaTypeCategoryBuiltinDouble:
  781. {
  782. if (type.boxed)
  783. [os writeInt32:TL_DOUBLE_CONSTRUCTOR];
  784. [os writeDouble:0.0];
  785. break;
  786. }
  787. case TLMetaTypeCategoryBuiltinBool:
  788. {
  789. if (type.boxed)
  790. [os writeInt32:TL_BOOL_FALSE_CONSTRUCTOR];
  791. break;
  792. }
  793. case TLMetaTypeCategoryBuiltinVector:
  794. {
  795. if (type.unboxedConstructorSignature != 0)
  796. {
  797. if (type.boxed)
  798. {
  799. #if TG_USE_UNIVERSAL_VECTOR
  800. [os writeInt32:TL_UNIVERSAL_VECTOR_CONSTRUCTOR];
  801. #else
  802. [os writeInt32:type.unboxedConstructorSignature];
  803. #endif
  804. }
  805. [os writeInt32:0];
  806. }
  807. else
  808. {
  809. TGLog(@"***** Unboxed constructor for 0x%.8x not found", type.type->getName());
  810. if (type.boxed)
  811. [os writeInt32:TL_NULL_CONSTRUCTOR];
  812. }
  813. break;
  814. }
  815. default:
  816. {
  817. TGLog(@"***** This should never happen 6");
  818. break;
  819. }
  820. }
  821. break;
  822. }
  823. case TLConstructedValueTypeVector:
  824. {
  825. switch (type.type->getCategory())
  826. {
  827. case TLMetaTypeCategoryObject:
  828. {
  829. if (type.boxed)
  830. [os writeInt32:TL_NULL_CONSTRUCTOR];
  831. break;
  832. }
  833. case TLMetaTypeCategoryBuiltinInt32:
  834. {
  835. if (type.boxed)
  836. [os writeInt32:TL_INT32_CONSTRUCTOR];
  837. [os writeInt32:0];
  838. break;
  839. }
  840. case TLMetaTypeCategoryBuiltinInt64:
  841. {
  842. if (type.boxed)
  843. [os writeInt32:TL_INT64_CONSTRUCTOR];
  844. [os writeInt64:0];
  845. break;
  846. }
  847. case TLMetaTypeCategoryBuiltinInt128:
  848. {
  849. if (type.boxed)
  850. [os writeInt32:TL_INT128_CONSTRUCTOR];
  851. NSData *data = value.nativeObject;
  852. if (data.length == 16)
  853. {
  854. [os writeData:data];
  855. }
  856. else
  857. {
  858. [os writeInt64:0];
  859. [os writeInt64:0];
  860. }
  861. break;
  862. }
  863. case TLMetaTypeCategoryBuiltinInt256:
  864. {
  865. if (type.boxed)
  866. [os writeInt32:TL_INT256_CONSTRUCTOR];
  867. NSData *data = value.nativeObject;
  868. if (data.length == 32)
  869. {
  870. [os writeData:data];
  871. }
  872. else
  873. {
  874. [os writeInt64:0];
  875. [os writeInt64:0];
  876. [os writeInt64:0];
  877. [os writeInt64:0];
  878. }
  879. break;
  880. }
  881. case TLMetaTypeCategoryBuiltinBytes:
  882. {
  883. if (type.boxed)
  884. [os writeInt32:TL_BYTES_CONSTRUCTOR];
  885. NSData *data = value.nativeObject;
  886. [os writeBytes:data];
  887. break;
  888. }
  889. case TLMetaTypeCategoryBuiltinString:
  890. {
  891. if (type.boxed)
  892. [os writeInt32:TL_BYTES_CONSTRUCTOR];
  893. NSString *string = value.nativeObject;
  894. [os writeString:string];
  895. break;
  896. }
  897. case TLMetaTypeCategoryBuiltinDouble:
  898. {
  899. if (type.boxed)
  900. [os writeInt32:TL_DOUBLE_CONSTRUCTOR];
  901. [os writeDouble:0.0];
  902. break;
  903. }
  904. case TLMetaTypeCategoryBuiltinBool:
  905. {
  906. if (type.boxed)
  907. [os writeInt32:TL_BOOL_FALSE_CONSTRUCTOR];
  908. break;
  909. }
  910. case TLMetaTypeCategoryBuiltinVector:
  911. {
  912. if (type.boxed)
  913. {
  914. if (type.unboxedConstructorSignature != 0)
  915. {
  916. #if TG_USE_UNIVERSAL_VECTOR
  917. [os writeInt32:TL_UNIVERSAL_VECTOR_CONSTRUCTOR];
  918. #else
  919. [os writeInt32:type.unboxedConstructorSignature];
  920. #endif
  921. }
  922. else
  923. {
  924. TGLog(@"***** Unboxed constructor for 0x%.8x not found", type.type->getName());
  925. }
  926. }
  927. NSArray *array = value.nativeObject;
  928. [os writeInt32:(int32_t)array.count];
  929. TLMetaTypeArgument const &typeArgument = type.type->getArguments()[0];
  930. const int convertInt32 = 1;
  931. const int convertInt64 = 2;
  932. const int convertString = 3;
  933. int conversionType = 0;
  934. switch (typeArgument.type->getCategory())
  935. {
  936. case TLMetaTypeCategoryBuiltinInt32:
  937. conversionType = convertInt32;
  938. break;
  939. case TLMetaTypeCategoryBuiltinInt64:
  940. conversionType = convertInt64;
  941. break;
  942. case TLMetaTypeCategoryObject:
  943. conversionType = 0;
  944. break;
  945. case TLMetaTypeCategoryBuiltinString:
  946. conversionType = 3;
  947. break;
  948. default:
  949. TGLog(@"***** Vector element conversion is not implemented");
  950. break;
  951. }
  952. for (id item in array)
  953. {
  954. TLConstructedValue itemValue;
  955. switch (conversionType)
  956. {
  957. case convertInt32:
  958. {
  959. itemValue.type = TLConstructedValueTypePrimitiveInt32;
  960. itemValue.primitive.int32Value = [item intValue];
  961. break;
  962. }
  963. case convertInt64:
  964. {
  965. itemValue.type = TLConstructedValueTypePrimitiveInt64;
  966. itemValue.primitive.int64Value = [item longLongValue];
  967. break;
  968. }
  969. case convertString:
  970. {
  971. itemValue.type = TLConstructedValueTypeString;
  972. itemValue.nativeObject = item;
  973. break;
  974. }
  975. default:
  976. {
  977. itemValue.type = TLConstructedValueTypeObject;
  978. itemValue.nativeObject = item;
  979. }
  980. }
  981. writeValueWithType(os, itemValue, typeArgument);
  982. }
  983. break;
  984. }
  985. default:
  986. {
  987. TGLog(@"***** This should never happen 6");
  988. break;
  989. }
  990. }
  991. break;
  992. }
  993. default:
  994. TGLog(@"***** This should never happen 1");
  995. }
  996. }
  997. void TLMetaConstructor::serialize(NSOutputStream *os, id object_)
  998. {
  999. id<TLObject> object = (id<TLObject>)object_;
  1000. std::map<int32_t, TLConstructedValue> fieldValues;
  1001. [object TLfillFieldsWithValues:&fieldValues];
  1002. std::vector<TLMetaField>::iterator fieldsEnd = fields->end();
  1003. for (std::vector<TLMetaField>::iterator it = fields->begin(); it != fieldsEnd; it++)
  1004. {
  1005. std::map<int32_t, TLConstructedValue>::iterator fieldIt = fieldValues.find(it->name);
  1006. if (fieldIt == fieldValues.end())
  1007. {
  1008. [object TLconstructorSignature];
  1009. TGLog(@"***** Field %.8x not found, writing empty value instead", it->name);
  1010. writeValueWithType(os, TLConstructedValue(), it->type);
  1011. }
  1012. else
  1013. {
  1014. writeValueWithType(os, fieldIt->second, it->type);
  1015. }
  1016. }
  1017. }