From db717bf6704b444b093d46f53935402c83441854 Mon Sep 17 00:00:00 2001 From: zxu Date: Thu, 5 Apr 2018 11:52:07 -0400 Subject: Port transform operations to C++ (#1020) * port FieldMask to C++ * address changes * address changes * fix test * address change * Port transform operations (FSTTransformOperation, FSTServerTimestampTransform) to C++ * address changes * address changes * address changes * address change --- .../src/firebase/firestore/model/CMakeLists.txt | 1 + .../firestore/model/transform_operations.h | 97 ++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 Firestore/core/src/firebase/firestore/model/transform_operations.h (limited to 'Firestore/core/src/firebase/firestore/model') diff --git a/Firestore/core/src/firebase/firestore/model/CMakeLists.txt b/Firestore/core/src/firebase/firestore/model/CMakeLists.txt index f6c5efe..d83c0aa 100644 --- a/Firestore/core/src/firebase/firestore/model/CMakeLists.txt +++ b/Firestore/core/src/firebase/firestore/model/CMakeLists.txt @@ -35,6 +35,7 @@ cc_library( resource_path.h snapshot_version.cc snapshot_version.h + transform_operations.h types.h DEPENDS absl_strings diff --git a/Firestore/core/src/firebase/firestore/model/transform_operations.h b/Firestore/core/src/firebase/firestore/model/transform_operations.h new file mode 100644 index 0000000..6ff4dbc --- /dev/null +++ b/Firestore/core/src/firebase/firestore/model/transform_operations.h @@ -0,0 +1,97 @@ +/* + * Copyright 2018 Google + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_MODEL_TRANSFORM_OPERATIONS_H_ +#define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_MODEL_TRANSFORM_OPERATIONS_H_ + +namespace firebase { +namespace firestore { +namespace model { + +// TODO(zxu123): We might want to refactor transform_operations.h into several +// files when the number of different types of operations grows gigantically. +// For now, put the interface and the only operation here. + +/** Represents a transform within a TransformMutation. */ +class TransformOperation { + public: + /** All the different kinds to TransformOperation. */ + enum class Type { + ServerTimestamp, + Test, // Purely for test purpose. + }; + + virtual ~TransformOperation() { + } + + /** Returns the actual type. */ + virtual Type type() const = 0; + + /** Returns whether the two are equal. */ + virtual bool operator==(const TransformOperation& other) const = 0; + + /** Returns whether the two are not equal. */ + bool operator!=(const TransformOperation& other) const { + return !operator==(other); + } + +#if defined(__OBJC__) + // For Objective-C++ hash; to be removed after migration. + // Do NOT use in C++ code. + virtual NSUInteger Hash() const = 0; +#endif // defined(__OBJC__) +}; + +/** Transforms a value into a server-generated timestamp. */ +class ServerTimestampTransform : public TransformOperation { + public: + ~ServerTimestampTransform() override { + } + + Type type() const override { + return Type::ServerTimestamp; + } + + bool operator==(const TransformOperation& other) const override { + // All ServerTimestampTransform objects are equal. + return other.type() == Type::ServerTimestamp; + } + + static const ServerTimestampTransform& Get() { + static ServerTimestampTransform shared_instance; + return shared_instance; + } + +#if defined(__OBJC__) + // For Objective-C++ hash; to be removed after migration. + // Do NOT use in C++ code. + NSUInteger Hash() const override { + // arbitrary number, the same as used in ObjC implementation, since all + // instances are equal. + return 37; + } +#endif // defined(__OBJC__) + + private: + ServerTimestampTransform() { + } +}; + +} // namespace model +} // namespace firestore +} // namespace firebase + +#endif // FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_MODEL_TRANSFORM_OPERATIONS_H_ -- cgit v1.2.3