`fabric/core` module and `Sealable` class

Summary:
`Sealable` class represents something which can be *sealed* (imperatively marked as immutable).

Authored by shergin

Reviewed By: shergin

Differential Revision: D7174883

fbshipit-source-id: 8b26ca5b1a5154953a099895778eab86228acc46
This commit is contained in:
Kevin Gozali 2018-03-08 17:50:18 -08:00 committed by Facebook Github Bot
parent 34f8e7f848
commit 3c86701ea7
4 changed files with 139 additions and 1 deletions

View File

@ -39,6 +39,6 @@ rn_xplat_cxx_library(
"xplat//folly:memory", "xplat//folly:memory",
"xplat//folly:molly", "xplat//folly:molly",
"xplat//third-party/glog:glog", "xplat//third-party/glog:glog",
react_native_xplat_target("fabric/debug:debug"), react_native_xplat_target("fabric/core:core"),
], ],
) )

View File

@ -0,0 +1,51 @@
load("@xplat//configurations/buck/apple:flag_defs.bzl", "get_debug_preprocessor_flags")
load("//ReactNative:DEFS.bzl", "IS_OSS_BUILD", "react_native_xplat_target", "rn_xplat_cxx_library", "APPLE_INSPECTOR_FLAGS")
APPLE_COMPILER_FLAGS = []
if not IS_OSS_BUILD:
load("@xplat//configurations/buck/apple:flag_defs.bzl", "get_static_library_ios_flags", "flags")
APPLE_COMPILER_FLAGS = flags.get_flag_value(get_static_library_ios_flags(), 'compiler_flags')
rn_xplat_cxx_library(
name = "core",
srcs = glob(
[
"**/*.cpp",
],
),
headers = glob(
[
"**/*.h",
],
),
header_namespace = "",
exported_headers = subdir_glob(
[
("primitives", "*.h"),
],
prefix = "fabric/core",
),
compiler_flags = [
"-fexceptions",
"-frtti",
"-std=c++14",
"-Wall",
],
fbobjc_compiler_flags = APPLE_COMPILER_FLAGS,
fbobjc_preprocessor_flags = get_debug_preprocessor_flags() + APPLE_INSPECTOR_FLAGS,
force_static = True,
preprocessor_flags = [
"-DLOG_TAG=\"ReactNative\"",
"-DWITH_FBSYSTRACE=1",
],
visibility = ["PUBLIC"],
deps = [
"xplat//fbsystrace:fbsystrace",
"xplat//folly:headers_only",
"xplat//folly:memory",
"xplat//folly:molly",
"xplat//third-party/glog:glog",
react_native_xplat_target("fabric/debug:debug"),
],
)

View File

@ -0,0 +1,30 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "Sealable.h"
#include <stdexcept>
namespace facebook {
namespace react {
void Sealable::seal() const {
sealed_ = true;
}
bool Sealable::getSealed() const {
return sealed_;
}
void Sealable::ensureUnsealed() const {
if (sealed_) {
throw std::runtime_error("Attempt to mutate a sealed object.");
}
}
} // namespace react
} // namespace facebook

View File

@ -0,0 +1,57 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
namespace facebook {
namespace react {
/*
* Represents something which can be *sealed* (imperatively marked as immutable).
*
* Why do we need this? In Fabric, some objects are semi-immutable
* even if they explicitly marked as `const`. It means that in some special
* cases those objects can be const-cast-away and then mutated. That comes from
* the fact that we share some object's life-cycle responsibilities with React
* and the immutability is guaranteed by some logic splitted between native and
* JavaScript worlds (which makes impossible to fully use immutability
* enforcement at a language level).
* To detect possible errors as early as possible we additionally mark objects
* as *sealed* after some stage and then enforce this at run-time.
*
* How to use:
* 1. Inherit your class from `Sealable`.
* 2. Call `ensureUnsealed()` from all non-const methods.
* 3. Call `seal()` at some point from which any modifications
* must be prevented.
*/
class Sealable {
public:
/*
* Seals the object. This operation is irreversible;
* the object cannot be "unsealed" after being sealing.
*/
void seal() const;
/*
* Returns if the object already sealed or not.
*/
bool getSealed() const;
protected:
/*
* Throws an exception if the object is sealed.
* Call this from all non-`const` methods.
*/
void ensureUnsealed() const;
private:
mutable bool sealed_ {false};
};
} // namespace react
} // namespace facebook