void Variant::Clear()

in app/src/variant.cc [286:411]


void Variant::Clear(Type new_type) {
  switch (type_) {
    case kInternalTypeNull: {
      break;
    }
    case kInternalTypeInt64: {
      value_.int64_value = 0;
      break;
    }
    case kInternalTypeDouble: {
      value_.double_value = 0;
      break;
    }
    case kInternalTypeBool: {
      value_.bool_value = false;
      break;
    }
    case kInternalTypeStaticString: {
      value_.static_string_value = nullptr;
      break;
    }
    case kInternalTypeMutableString: {
      if (new_type != kTypeMutableString ||
          value_.mutable_string_value == nullptr) {
        delete value_.mutable_string_value;
        value_.mutable_string_value = nullptr;
      } else {
        value_.mutable_string_value->clear();
      }
      break;
    }
    case kInternalTypeSmallString: {
      value_.small_string[0] = '\0';
      break;
    }
    case kInternalTypeVector: {
      if (new_type != kTypeVector || value_.vector_value == nullptr) {
        delete value_.vector_value;
        value_.vector_value = nullptr;
      } else {
        value_.vector_value->clear();
      }
      break;
    }
    case kInternalTypeMap: {
      if (new_type != kTypeMap || value_.map_value == nullptr) {
        delete value_.map_value;
        value_.map_value = nullptr;
      } else {
        value_.map_value->clear();
      }
      break;
    }
    case kInternalTypeStaticBlob: {
      set_blob_pointer(nullptr, 0);
      break;
    }
    case kInternalTypeMutableBlob: {
      uint8_t* prev_data = const_cast<uint8_t*>(value_.blob_value.ptr);
      set_blob_pointer(nullptr, 0);
      delete[] prev_data;
      break;
    }
    case kMaxTypeValue:
      FIREBASE_ASSERT(false);  // Should never hit this
      break;
  }

  InternalType old_type = type_;
  type_ = static_cast<InternalType>(new_type);
  switch (type_) {
    case kInternalTypeNull: {
      break;
    }
    case kInternalTypeInt64: {
      value_.int64_value = 0;
      break;
    }
    case kInternalTypeDouble: {
      value_.double_value = 0;
      break;
    }
    case kInternalTypeBool: {
      value_.bool_value = false;
      break;
    }
    case kInternalTypeStaticString: {
      value_.static_string_value = "";
      break;
    }
    case kInternalTypeMutableString: {
      if (old_type != kInternalTypeMutableString ||
          value_.mutable_string_value == nullptr) {
        value_.mutable_string_value = new std::string();
      }
      break;
    }
    case kInternalTypeSmallString: {
      value_.small_string[0] = '\0';
      break;
    }
    case kInternalTypeVector: {
      if (old_type != kInternalTypeVector || value_.vector_value == nullptr) {
        value_.vector_value = new std::vector<Variant>(0);
      }
      break;
    }
    case kInternalTypeMap: {
      if (old_type != kInternalTypeMap || value_.map_value == nullptr) {
        value_.map_value = new std::map<Variant, Variant>();
      }
      break;
    }
    case kInternalTypeStaticBlob: {
      set_blob_pointer(nullptr, 0);
      break;
    }
    case kInternalTypeMutableBlob: {
      set_blob_pointer(nullptr, 0);
      break;
    }
    case kMaxTypeValue:
      FIREBASE_ASSERT(false);  // Should never hit this
      break;
  }
}