serializer.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*************************************************************************
  2. *
  3. * Copyright 2016 Realm Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. **************************************************************************/
  18. #ifndef REALM_UTIL_SERIALIZER_HPP
  19. #define REALM_UTIL_SERIALIZER_HPP
  20. #include <realm/table_ref.hpp>
  21. #include <realm/util/optional.hpp>
  22. #include <string>
  23. #include <sstream>
  24. #include <vector>
  25. namespace realm {
  26. class BinaryData;
  27. struct ColKey;
  28. struct null;
  29. class ObjectId;
  30. struct ObjKey;
  31. struct ObjLink;
  32. class StringData;
  33. class Timestamp;
  34. class LinkMap;
  35. class UUID;
  36. class TypeOfValue;
  37. enum class ExpressionComparisonType : unsigned char;
  38. namespace util {
  39. namespace serializer {
  40. // Definitions
  41. template <typename T>
  42. std::string print_value(T value);
  43. template <typename T>
  44. std::string print_value(Optional<T> value);
  45. const static std::string value_separator = ".";
  46. // Specializations declared here to be defined in the cpp file
  47. template <> std::string print_value<>(BinaryData);
  48. template <> std::string print_value<>(bool);
  49. template <>
  50. std::string print_value<>(float);
  51. template <>
  52. std::string print_value<>(double);
  53. template <> std::string print_value<>(realm::null);
  54. template <> std::string print_value<>(StringData);
  55. template <> std::string print_value<>(realm::Timestamp);
  56. template <>
  57. std::string print_value<>(realm::ObjectId);
  58. template <>
  59. std::string print_value<>(realm::ObjKey);
  60. template <>
  61. std::string print_value<>(realm::ObjLink);
  62. template <>
  63. std::string print_value<>(realm::UUID);
  64. template <>
  65. std::string print_value<>(realm::TypeOfValue);
  66. // General implementation for most types
  67. template <typename T>
  68. std::string print_value(T value)
  69. {
  70. std::stringstream ss;
  71. ss << value;
  72. return ss.str();
  73. }
  74. template <typename T>
  75. std::string print_value(Optional<T> value)
  76. {
  77. if (bool(value)) {
  78. return print_value(*value);
  79. } else {
  80. return "NULL";
  81. }
  82. }
  83. StringData get_printable_table_name(StringData name, const std::string& prefix);
  84. struct SerialisationState {
  85. SerialisationState(const std::string& prefix)
  86. : class_prefix(prefix)
  87. {
  88. }
  89. std::string describe_column(ConstTableRef table, ColKey col_key);
  90. std::string describe_columns(const LinkMap& link_map, ColKey target_col_key);
  91. std::string describe_expression_type(ExpressionComparisonType type);
  92. std::string get_column_name(ConstTableRef table, ColKey col_key);
  93. std::string get_backlink_column_name(ConstTableRef from, ColKey col_key);
  94. std::string get_variable_name(ConstTableRef table);
  95. std::vector<std::string> subquery_prefix_list;
  96. std::string class_prefix;
  97. };
  98. } // namespace serializer
  99. } // namespace util
  100. } // namespace realm
  101. #endif // REALM_UTIL_SERIALIZER_HPP