Values_cpp.template 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. // This file is generated by Values_cpp.template.
  2. // Copyright 2016 The Chromium Authors. All rights reserved.
  3. // Use of this source code is governed by a BSD-style license that can be
  4. // found in the LICENSE file.
  5. //#include "Values.h"
  6. #include "{{config.crdtp.dir}}/cbor.h"
  7. {% for namespace in config.protocol.namespace %}
  8. namespace {{namespace}} {
  9. {% endfor %}
  10. namespace {
  11. using {{config.crdtp.namespace}}::Status;
  12. using {{config.crdtp.namespace}}::ParserHandler;
  13. using {{config.crdtp.namespace}}::span;
  14. namespace cbor {
  15. using {{config.crdtp.namespace}}::cbor::ParseCBOR;
  16. using {{config.crdtp.namespace}}::cbor::EncodeBinary;
  17. using {{config.crdtp.namespace}}::cbor::EncodeDouble;
  18. using {{config.crdtp.namespace}}::cbor::EncodeFalse;
  19. using {{config.crdtp.namespace}}::cbor::EncodeFromLatin1;
  20. using {{config.crdtp.namespace}}::cbor::EncodeFromUTF16;
  21. using {{config.crdtp.namespace}}::cbor::EncodeIndefiniteLengthArrayStart;
  22. using {{config.crdtp.namespace}}::cbor::EncodeIndefiniteLengthMapStart;
  23. using {{config.crdtp.namespace}}::cbor::EncodeInt32;
  24. using {{config.crdtp.namespace}}::cbor::EncodeNull;
  25. using {{config.crdtp.namespace}}::cbor::EncodeStop;
  26. using {{config.crdtp.namespace}}::cbor::EncodeString8;
  27. using {{config.crdtp.namespace}}::cbor::EncodeTrue;
  28. using {{config.crdtp.namespace}}::cbor::EnvelopeEncoder;
  29. } // namespace cbor
  30. // Uses the parsing events received from driver of |ParserHandler|
  31. // (e.g. cbor::ParseCBOR) into a protocol::Value instance.
  32. class ValueParserHandler : public ParserHandler {
  33. public:
  34. // Provides the parsed protocol::Value.
  35. std::unique_ptr<Value> ReleaseRoot() { return std::move(root_); }
  36. // The first parsing error encountered; |status().ok()| is the default.
  37. Status status() const { return status_; }
  38. private:
  39. //
  40. // Implementation of ParserHandler.
  41. //
  42. void HandleMapBegin() override {
  43. if (!status_.ok()) return;
  44. std::unique_ptr<DictionaryValue> dict = DictionaryValue::create();
  45. DictionaryValue* dict_ptr = dict.get();
  46. AddValueToParent(std::move(dict));
  47. stack_.emplace_back(dict_ptr);
  48. }
  49. void HandleMapEnd() override {
  50. if (!status_.ok()) return;
  51. DCHECK(!stack_.empty());
  52. DCHECK(stack_.back().is_dict);
  53. stack_.pop_back();
  54. }
  55. void HandleArrayBegin() override {
  56. if (!status_.ok()) return;
  57. std::unique_ptr<ListValue> list = ListValue::create();
  58. ListValue* list_ptr = list.get();
  59. AddValueToParent(std::move(list));
  60. stack_.emplace_back(list_ptr);
  61. }
  62. void HandleArrayEnd() override {
  63. if (!status_.ok()) return;
  64. DCHECK(!stack_.empty());
  65. DCHECK(!stack_.back().is_dict);
  66. stack_.pop_back();
  67. }
  68. void HandleString8(span<uint8_t> chars) override {
  69. AddStringToParent(StringUtil::fromUTF8(chars.data(), chars.size()));
  70. }
  71. void HandleString16(span<uint16_t> chars) override {
  72. AddStringToParent(
  73. StringUtil::fromUTF16LE(chars.data(), chars.size()));
  74. }
  75. void HandleBinary(span<uint8_t> bytes) override {
  76. AddValueToParent(
  77. BinaryValue::create(Binary::fromSpan(bytes.data(), bytes.size())));
  78. }
  79. void HandleDouble(double value) override {
  80. AddValueToParent(FundamentalValue::create(value));
  81. }
  82. void HandleInt32(int32_t value) override {
  83. AddValueToParent(FundamentalValue::create(value));
  84. }
  85. void HandleBool(bool value) override {
  86. AddValueToParent(FundamentalValue::create(value));
  87. }
  88. void HandleNull() override {
  89. AddValueToParent(Value::null());
  90. }
  91. void HandleError(Status error) override {
  92. status_ = error;
  93. }
  94. //
  95. // Adding strings and values to the parent value.
  96. // Strings are handled separately because they can be keys for
  97. // dictionary values.
  98. //
  99. void AddStringToParent(String str) {
  100. if (!status_.ok()) return;
  101. if (!root_) {
  102. DCHECK(!key_is_pending_);
  103. root_ = StringValue::create(str);
  104. } else if (stack_.back().is_dict) {
  105. // If we already have a pending key, then this is the value of the
  106. // key/value pair. Otherwise, it's the new pending key.
  107. if (key_is_pending_) {
  108. stack_.back().dict->setString(pending_key_, str);
  109. key_is_pending_ = false;
  110. } else {
  111. pending_key_ = std::move(str);
  112. key_is_pending_ = true;
  113. }
  114. } else { // Top of the stack is a list.
  115. DCHECK(!key_is_pending_);
  116. stack_.back().list->pushValue(StringValue::create(str));
  117. }
  118. }
  119. void AddValueToParent(std::unique_ptr<Value> value) {
  120. if (!status_.ok()) return;
  121. if (!root_) {
  122. DCHECK(!key_is_pending_);
  123. root_ = std::move(value);
  124. } else if (stack_.back().is_dict) {
  125. DCHECK(key_is_pending_);
  126. stack_.back().dict->setValue(pending_key_, std::move(value));
  127. key_is_pending_ = false;
  128. } else { // Top of the stack is a list.
  129. DCHECK(!key_is_pending_);
  130. stack_.back().list->pushValue(std::move(value));
  131. }
  132. }
  133. // |status_.ok()| is the default; if we receive an error event
  134. // we keep the first one and stop modifying any other state.
  135. Status status_;
  136. // The root of the parsed protocol::Value tree.
  137. std::unique_ptr<Value> root_;
  138. // If root_ is a list or a dictionary, this stack keeps track of
  139. // the container we're currently parsing as well as its ancestors.
  140. struct ContainerState {
  141. ContainerState(DictionaryValue* dict) : is_dict(true), dict(dict) {}
  142. ContainerState(ListValue* list) : is_dict(false), list(list) {}
  143. bool is_dict;
  144. union {
  145. DictionaryValue* dict;
  146. ListValue* list;
  147. };
  148. };
  149. std::vector<ContainerState> stack_;
  150. // For maps, keys and values are alternating events, so we keep the
  151. // key around and process it when the value arrives.
  152. bool key_is_pending_ = false;
  153. String pending_key_;
  154. };
  155. } // anonymous namespace
  156. // static
  157. std::unique_ptr<Value> Value::parseBinary(const uint8_t* data, size_t size) {
  158. ValueParserHandler handler;
  159. cbor::ParseCBOR(span<uint8_t>(data, size), &handler);
  160. // TODO(johannes): We have decent error info in handler.status(); provide
  161. // a richer interface that makes this available to client code.
  162. if (handler.status().ok())
  163. return handler.ReleaseRoot();
  164. return nullptr;
  165. }
  166. bool Value::asBoolean(bool*) const
  167. {
  168. return false;
  169. }
  170. bool Value::asDouble(double*) const
  171. {
  172. return false;
  173. }
  174. bool Value::asInteger(int*) const
  175. {
  176. return false;
  177. }
  178. bool Value::asString(String*) const
  179. {
  180. return false;
  181. }
  182. bool Value::asBinary(Binary*) const
  183. {
  184. return false;
  185. }
  186. void Value::AppendSerialized(std::vector<uint8_t>* bytes) const {
  187. DCHECK(m_type == TypeNull);
  188. bytes->push_back(cbor::EncodeNull());
  189. }
  190. std::unique_ptr<Value> Value::clone() const
  191. {
  192. return Value::null();
  193. }
  194. bool FundamentalValue::asBoolean(bool* output) const
  195. {
  196. if (type() != TypeBoolean)
  197. return false;
  198. *output = m_boolValue;
  199. return true;
  200. }
  201. bool FundamentalValue::asDouble(double* output) const
  202. {
  203. if (type() == TypeDouble) {
  204. *output = m_doubleValue;
  205. return true;
  206. }
  207. if (type() == TypeInteger) {
  208. *output = m_integerValue;
  209. return true;
  210. }
  211. return false;
  212. }
  213. bool FundamentalValue::asInteger(int* output) const
  214. {
  215. if (type() != TypeInteger)
  216. return false;
  217. *output = m_integerValue;
  218. return true;
  219. }
  220. void FundamentalValue::AppendSerialized(std::vector<uint8_t>* bytes) const {
  221. switch (type()) {
  222. case TypeDouble:
  223. cbor::EncodeDouble(m_doubleValue, bytes);
  224. return;
  225. case TypeInteger:
  226. cbor::EncodeInt32(m_integerValue, bytes);
  227. return;
  228. case TypeBoolean:
  229. bytes->push_back(m_boolValue ? cbor::EncodeTrue() : cbor::EncodeFalse());
  230. return;
  231. default:
  232. DCHECK(false);
  233. }
  234. }
  235. std::unique_ptr<Value> FundamentalValue::clone() const
  236. {
  237. switch (type()) {
  238. case TypeDouble: return FundamentalValue::create(m_doubleValue);
  239. case TypeInteger: return FundamentalValue::create(m_integerValue);
  240. case TypeBoolean: return FundamentalValue::create(m_boolValue);
  241. default:
  242. DCHECK(false);
  243. }
  244. return nullptr;
  245. }
  246. bool StringValue::asString(String* output) const
  247. {
  248. *output = m_stringValue;
  249. return true;
  250. }
  251. namespace {
  252. // This routine distinguishes between the current encoding for a given
  253. // string |s|, and calls encoding routines that will
  254. // - Ensure that all ASCII strings end up being encoded as UTF8 in
  255. // the wire format - e.g., EncodeFromUTF16 will detect ASCII and
  256. // do the (trivial) transcode to STRING8 on the wire, but if it's
  257. // not ASCII it'll do STRING16.
  258. // - Select a format that's cheap to convert to. E.g., we don't
  259. // have LATIN1 on the wire, so we call EncodeFromLatin1 which
  260. // transcodes to UTF8 if needed.
  261. void EncodeString(const String& s, std::vector<uint8_t>* out) {
  262. if (StringUtil::CharacterCount(s) == 0) {
  263. cbor::EncodeString8(span<uint8_t>(nullptr, 0), out); // Empty string.
  264. } else if (StringUtil::CharactersLatin1(s)) {
  265. cbor::EncodeFromLatin1(span<uint8_t>(StringUtil::CharactersLatin1(s),
  266. StringUtil::CharacterCount(s)),
  267. out);
  268. } else if (StringUtil::CharactersUTF16(s)) {
  269. cbor::EncodeFromUTF16(span<uint16_t>(StringUtil::CharactersUTF16(s),
  270. StringUtil::CharacterCount(s)),
  271. out);
  272. } else if (StringUtil::CharactersUTF8(s)) {
  273. cbor::EncodeString8(span<uint8_t>(StringUtil::CharactersUTF8(s),
  274. StringUtil::CharacterCount(s)),
  275. out);
  276. }
  277. }
  278. } // namespace
  279. void StringValue::AppendSerialized(std::vector<uint8_t>* bytes) const {
  280. EncodeString(m_stringValue, bytes);
  281. }
  282. std::unique_ptr<Value> StringValue::clone() const
  283. {
  284. return StringValue::create(m_stringValue);
  285. }
  286. bool BinaryValue::asBinary(Binary* output) const
  287. {
  288. *output = m_binaryValue;
  289. return true;
  290. }
  291. void BinaryValue::AppendSerialized(std::vector<uint8_t>* bytes) const {
  292. cbor::EncodeBinary(span<uint8_t>(m_binaryValue.data(),
  293. m_binaryValue.size()), bytes);
  294. }
  295. std::unique_ptr<Value> BinaryValue::clone() const
  296. {
  297. return BinaryValue::create(m_binaryValue);
  298. }
  299. DictionaryValue::~DictionaryValue()
  300. {
  301. }
  302. void DictionaryValue::setBoolean(const String& name, bool value)
  303. {
  304. setValue(name, FundamentalValue::create(value));
  305. }
  306. void DictionaryValue::setInteger(const String& name, int value)
  307. {
  308. setValue(name, FundamentalValue::create(value));
  309. }
  310. void DictionaryValue::setDouble(const String& name, double value)
  311. {
  312. setValue(name, FundamentalValue::create(value));
  313. }
  314. void DictionaryValue::setString(const String& name, const String& value)
  315. {
  316. setValue(name, StringValue::create(value));
  317. }
  318. void DictionaryValue::setValue(const String& name, std::unique_ptr<Value> value)
  319. {
  320. set(name, value);
  321. }
  322. void DictionaryValue::setObject(const String& name, std::unique_ptr<DictionaryValue> value)
  323. {
  324. set(name, value);
  325. }
  326. void DictionaryValue::setArray(const String& name, std::unique_ptr<ListValue> value)
  327. {
  328. set(name, value);
  329. }
  330. bool DictionaryValue::getBoolean(const String& name, bool* output) const
  331. {
  332. protocol::Value* value = get(name);
  333. if (!value)
  334. return false;
  335. return value->asBoolean(output);
  336. }
  337. bool DictionaryValue::getInteger(const String& name, int* output) const
  338. {
  339. Value* value = get(name);
  340. if (!value)
  341. return false;
  342. return value->asInteger(output);
  343. }
  344. bool DictionaryValue::getDouble(const String& name, double* output) const
  345. {
  346. Value* value = get(name);
  347. if (!value)
  348. return false;
  349. return value->asDouble(output);
  350. }
  351. bool DictionaryValue::getString(const String& name, String* output) const
  352. {
  353. protocol::Value* value = get(name);
  354. if (!value)
  355. return false;
  356. return value->asString(output);
  357. }
  358. DictionaryValue* DictionaryValue::getObject(const String& name) const
  359. {
  360. return DictionaryValue::cast(get(name));
  361. }
  362. protocol::ListValue* DictionaryValue::getArray(const String& name) const
  363. {
  364. return ListValue::cast(get(name));
  365. }
  366. protocol::Value* DictionaryValue::get(const String& name) const
  367. {
  368. Dictionary::const_iterator it = m_data.find(name);
  369. if (it == m_data.end())
  370. return nullptr;
  371. return it->second.get();
  372. }
  373. DictionaryValue::Entry DictionaryValue::at(size_t index) const
  374. {
  375. const String key = m_order[index];
  376. return std::make_pair(key, m_data.find(key)->second.get());
  377. }
  378. bool DictionaryValue::booleanProperty(const String& name, bool defaultValue) const
  379. {
  380. bool result = defaultValue;
  381. getBoolean(name, &result);
  382. return result;
  383. }
  384. int DictionaryValue::integerProperty(const String& name, int defaultValue) const
  385. {
  386. int result = defaultValue;
  387. getInteger(name, &result);
  388. return result;
  389. }
  390. double DictionaryValue::doubleProperty(const String& name, double defaultValue) const
  391. {
  392. double result = defaultValue;
  393. getDouble(name, &result);
  394. return result;
  395. }
  396. void DictionaryValue::remove(const String& name)
  397. {
  398. m_data.erase(name);
  399. m_order.erase(std::remove(m_order.begin(), m_order.end(), name), m_order.end());
  400. }
  401. void DictionaryValue::AppendSerialized(std::vector<uint8_t>* bytes) const {
  402. cbor::EnvelopeEncoder encoder;
  403. encoder.EncodeStart(bytes);
  404. bytes->push_back(cbor::EncodeIndefiniteLengthMapStart());
  405. for (size_t i = 0; i < m_order.size(); ++i) {
  406. const String& key = m_order[i];
  407. Dictionary::const_iterator value = m_data.find(key);
  408. DCHECK(value != m_data.cend() && value->second);
  409. EncodeString(key, bytes);
  410. value->second->AppendSerialized(bytes);
  411. }
  412. bytes->push_back(cbor::EncodeStop());
  413. encoder.EncodeStop(bytes);
  414. }
  415. std::unique_ptr<Value> DictionaryValue::clone() const
  416. {
  417. std::unique_ptr<DictionaryValue> result = DictionaryValue::create();
  418. for (size_t i = 0; i < m_order.size(); ++i) {
  419. String key = m_order[i];
  420. Dictionary::const_iterator value = m_data.find(key);
  421. DCHECK(value != m_data.cend() && value->second);
  422. result->setValue(key, value->second->clone());
  423. }
  424. return result;
  425. }
  426. DictionaryValue::DictionaryValue()
  427. : Value(TypeObject)
  428. {
  429. }
  430. ListValue::~ListValue()
  431. {
  432. }
  433. void ListValue::AppendSerialized(std::vector<uint8_t>* bytes) const {
  434. cbor::EnvelopeEncoder encoder;
  435. encoder.EncodeStart(bytes);
  436. bytes->push_back(cbor::EncodeIndefiniteLengthArrayStart());
  437. for (size_t i = 0; i < m_data.size(); ++i) {
  438. m_data[i]->AppendSerialized(bytes);
  439. }
  440. bytes->push_back(cbor::EncodeStop());
  441. encoder.EncodeStop(bytes);
  442. }
  443. std::unique_ptr<Value> ListValue::clone() const
  444. {
  445. std::unique_ptr<ListValue> result = ListValue::create();
  446. for (const std::unique_ptr<protocol::Value>& value : m_data)
  447. result->pushValue(value->clone());
  448. return result;
  449. }
  450. ListValue::ListValue()
  451. : Value(TypeArray)
  452. {
  453. }
  454. void ListValue::pushValue(std::unique_ptr<protocol::Value> value)
  455. {
  456. DCHECK(value);
  457. m_data.push_back(std::move(value));
  458. }
  459. protocol::Value* ListValue::at(size_t index)
  460. {
  461. DCHECK_LT(index, m_data.size());
  462. return m_data[index].get();
  463. }
  464. {% for namespace in config.protocol.namespace %}
  465. } // namespace {{namespace}}
  466. {% endfor %}