/hphp/runtime/ext/VariantController.h

https://github.com/tstarling/hiphop-php · C Header · 153 lines · 119 code · 10 blank · 24 comment · 2 complexity · d8d7d76c71d7213c86f3eb67a39e9ad5 MD5 · raw file

  1. /*
  2. +----------------------------------------------------------------------+
  3. | HipHop for PHP |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 2010-2014 Facebook, Inc. (http://www.facebook.com) |
  6. | Copyright (c) 1997-2010 The PHP Group |
  7. +----------------------------------------------------------------------+
  8. | This source file is subject to version 3.01 of the PHP license, |
  9. | that is bundled with this package in the file LICENSE, and is |
  10. | available through the world-wide-web at the following url: |
  11. | http://www.php.net/license/3_01.txt |
  12. | If you did not receive a copy of the PHP license and are unable to |
  13. | obtain it through the world-wide-web, please send a note to |
  14. | license@php.net so we can mail you a copy immediately. |
  15. +----------------------------------------------------------------------+
  16. */
  17. #ifndef VARIANTCONTROLLER_H
  18. #define VARIANTCONTROLLER_H
  19. #include "hphp/runtime/base/base-includes.h"
  20. #include <algorithm>
  21. namespace HPHP {
  22. /**
  23. * Hphp datatype conforming to datatype requirements in FBSerialize.h
  24. */
  25. struct VariantController {
  26. typedef Variant VariantType;
  27. typedef Array MapType;
  28. typedef Array VectorType;
  29. typedef String StringType;
  30. // variant accessors
  31. static HPHP::serialize::Type type(const VariantType& obj) {
  32. switch (obj.getType()) {
  33. case KindOfUninit:
  34. case KindOfNull: return HPHP::serialize::Type::NULLT;
  35. case KindOfBoolean: return HPHP::serialize::Type::BOOL;
  36. case KindOfDouble: return HPHP::serialize::Type::DOUBLE;
  37. case KindOfInt64: return HPHP::serialize::Type::INT64;
  38. case KindOfArray: return HPHP::serialize::Type::MAP;
  39. case KindOfStaticString:
  40. case KindOfString: return HPHP::serialize::Type::STRING;
  41. case KindOfObject: return HPHP::serialize::Type::OBJECT;
  42. default:
  43. throw HPHP::serialize::SerializeError(
  44. "don't know how to serialize HPHP Variant");
  45. }
  46. }
  47. static int64_t asInt64(const VariantType& obj) { return obj.toInt64(); }
  48. static bool asBool(const VariantType& obj) { return obj.toInt64() != 0; }
  49. static double asDouble(const VariantType& obj) { return obj.toDouble(); }
  50. static const String& asString(const VariantType& obj) {
  51. return obj.toCStrRef();
  52. }
  53. static const Array& asMap(const VariantType& obj) { return obj.toCArrRef(); }
  54. static const Array& asVector(const VariantType& obj) { return obj.toCArrRef(); }
  55. // variant creators
  56. static VariantType createNull() { return null_variant; }
  57. static VariantType fromInt64(int64_t val) { return val; }
  58. static VariantType fromBool(bool val) { return val; }
  59. static VariantType fromDouble(double val) { return val; }
  60. static VariantType fromString(const StringType& str) { return str; }
  61. static VariantType fromMap(const MapType& map) { return map; }
  62. static VariantType fromVector(const VectorType& vec) { return vec; }
  63. // map methods
  64. static MapType createMap() { return Array::Create(); }
  65. static MapType createMap(ArrayInit&& map) {
  66. return map.toArray();
  67. }
  68. static ArrayInit reserveMap(size_t n) {
  69. ArrayInit res(n, ArrayInit::mapInit);
  70. return res;
  71. }
  72. static MapType getStaticEmptyMap() {
  73. return HphpArray::GetStaticEmptyArray();
  74. }
  75. static HPHP::serialize::Type mapKeyType(const Variant& k) {
  76. return type(k);
  77. }
  78. static int64_t mapKeyAsInt64(const Variant& k) { return k.toInt64(); }
  79. static const String& mapKeyAsString(const Variant& k) {
  80. return k.toCStrRef();
  81. }
  82. template <typename Key>
  83. static void mapSet(MapType& map, Key&& k, VariantType&& v) {
  84. map.set(std::move(k), std::move(v));
  85. }
  86. template <typename Key>
  87. static void mapSet(ArrayInit& map, Key&& k, VariantType&& v) {
  88. map.set(std::move(k), std::move(v), /* key converted */ true);
  89. }
  90. static int64_t mapSize(const MapType& map) { return map.size(); }
  91. static ArrayIter mapIterator(const MapType& map) {
  92. return ArrayIter(map);
  93. }
  94. static bool mapNotEnd(const MapType& map, ArrayIter& it) {
  95. return !it.end();
  96. }
  97. static void mapNext(ArrayIter& it) { ++it; }
  98. static Variant mapKey(ArrayIter& it) { return it.first(); }
  99. static const VariantType& mapValue(ArrayIter& it) { return it.secondRef(); }
  100. // vector methods
  101. static VectorType createVector() { return Array::Create(); }
  102. static void vectorAppend(VectorType& vec, const VariantType& v) {
  103. vec.append(v);
  104. }
  105. static ArrayIter vectorIterator(const VectorType& vec) {
  106. return ArrayIter(vec);
  107. }
  108. static bool vectorNotEnd(const VectorType& vec, ArrayIter& it) {
  109. return !it.end();
  110. }
  111. static void vectorNext(ArrayIter& it) { ++it; }
  112. static const VariantType& vectorValue(ArrayIter& it) {
  113. return it.secondRef();
  114. }
  115. // string methods
  116. static StringType createMutableString(size_t n) {
  117. String ret(n, ReserveString);
  118. ret.setSize(n);
  119. return ret;
  120. }
  121. static StringType createStaticString(const char* str, size_t len) {
  122. String ret = String(makeStaticString(str, len));
  123. return ret;
  124. }
  125. static StringType getStaticEmptyString() {
  126. return empty_string;
  127. }
  128. static char* getMutablePtr(StringType& s) {
  129. return s.bufferSlice().ptr;
  130. }
  131. static void shrinkString(String& s, size_t length) {
  132. s.shrink(length);
  133. }
  134. static StringType stringFromData(const char* src, int n) {
  135. return StringData::Make(src, n, CopyString);
  136. }
  137. static size_t stringLen(const StringType& str) { return str.size(); }
  138. static const char* stringData(const StringType& str) {
  139. return str.data();
  140. }
  141. };
  142. }
  143. #endif