Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions API/fleece/RefCounted.hh
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,29 @@ namespace fleece {
}
return *this;
}

explicit operator bool () const FLPURE {return N==NonNull || (_ref != nullptr);}

#if 1
explicit operator bool () const noexcept FLPURE {return (_ref != nullptr);}
#else
explicit operator bool () const noexcept requires(N==MaybeNull) FLPURE {return (_ref != nullptr);}
[[deprecated("A Ref<> is non-null")]] explicit operator bool () const noexcept requires(N==NonNull) FLPURE {return (_ref != nullptr);}
#endif

/// Releases the reference. Afterwards this Ref MUST NOT be used again.
/// @warning This method should be used with caution!
/// Its purpose is to support classes with a `close` or `finalize` method that releases all
/// references before it's destructed. Those references might always be non-null until the
/// close; they can be (accurately) declared as `Ref` instead of `Retained` by having the
/// close method use `destroy` to release them.
/// @note Calling `destroy` requires an explicit `std::move`; this is to make it clear
/// that the object's lifetime is effectively ended by the call.
void destroy() && noexcept requires(N==NonNull) {
release(_ref);
_ref = nullptr;
}

/// Returns true if \ref destroy has been called on this Ref.
bool isDestroyed() const noexcept requires(N==NonNull) FLPURE {return _ref == nullptr;}

// typical dereference operations:
operator T_ptr () const & noexcept LIFETIMEBOUND FLPURE STEPOVER {return _ref;}
Expand Down
8 changes: 4 additions & 4 deletions Fleece/Core/Builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ namespace fleece::impl::builder {
RetainedConst<Value> buildValue() {
switch (peekToken()) {
case '[': {
Retained<MutableArray> array = MutableArray::newArray();
Ref<MutableArray> array = MutableArray::newArray();
_buildInto(array);
finished();
return array.get();
}
case '{': {
Retained<MutableDict> dict = MutableDict::newDict();
Ref<MutableDict> dict = MutableDict::newDict();
_buildInto(dict);
finished();
return dict.get();
Expand Down Expand Up @@ -82,13 +82,13 @@ namespace fleece::impl::builder {
bool _buildValue(ValueSlot &inSlot) {
switch (peekValue()) {
case ValueType::array: {
Retained<MutableArray> array = MutableArray::newArray();
Ref<MutableArray> array = MutableArray::newArray();
_buildInto(array);
inSlot.set(array);
break;
}
case ValueType::dict: {
Retained<MutableDict> dict = MutableDict::newDict();
Ref<MutableDict> dict = MutableDict::newDict();
_buildInto(dict);
inSlot.set(dict);
break;
Expand Down
4 changes: 2 additions & 2 deletions Fleece/Core/Doc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -350,11 +350,11 @@ namespace fleece { namespace impl {
}


Retained<Doc> Doc::fromFleece(const alloc_slice &fleece, Trust trust) {
Ref<Doc> Doc::fromFleece(const alloc_slice &fleece, Trust trust) {
return new Doc(fleece, trust);
}

Retained<Doc> Doc::fromJSON(slice json, SharedKeys *sk) {
Ref<Doc> Doc::fromJSON(slice json, SharedKeys *sk) {
return new Doc(JSONConverter::convertJSON(json, sk), kTrusted, sk);
}

Expand Down
4 changes: 2 additions & 2 deletions Fleece/Core/Doc.hh
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ namespace fleece { namespace impl {
slice subData,
Trust =kUntrusted) noexcept;

static Retained<Doc> fromFleece(const alloc_slice &fleece, Trust =kUntrusted);
static Retained<Doc> fromJSON(slice json, SharedKeys* =nullptr);
static Ref<Doc> fromFleece(const alloc_slice &fleece, Trust =kUntrusted);
static Ref<Doc> fromJSON(slice json, SharedKeys* =nullptr);

static RetainedConst<Doc> containing(const Value* NONNULL) noexcept;

Expand Down
11 changes: 5 additions & 6 deletions Fleece/Core/Encoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,11 @@ namespace fleece { namespace impl {
return out;
}

Retained<Doc> Encoder::finishDoc() {
Retained<Doc> doc = new Doc(finish(),
Doc::kTrusted,
_sharedKeys,
(_markExternPtrs ? _base : slice()));
return doc;
Ref<Doc> Encoder::finishDoc() {
return new Doc(finish(),
Doc::kTrusted,
_sharedKeys,
(_markExternPtrs ? _base : slice()));
}

// Returns position in the stream of the next write. Pads stream to even pos if necessary.
Expand Down
2 changes: 1 addition & 1 deletion Fleece/Core/Encoder.hh
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ namespace fleece { namespace impl {
alloc_slice finish();

/** Returns the encoded data as a Doc. This implicitly calls end(). */
Retained<Doc> finishDoc();
Ref<Doc> finishDoc();

/** Resets the encoder so it can be used again. */
void reset();
Expand Down
6 changes: 3 additions & 3 deletions Fleece/Mutable/MutableArray.hh
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@ namespace fleece { namespace impl {
public:

/** Creates a new array of size `initialCount` filled with null Values. */
static Retained<MutableArray> newArray(uint32_t initialCount =0) {
static Ref<MutableArray> newArray(uint32_t initialCount =0) {
return (new internal::HeapArray(initialCount))->asMutableArray();
}

/** Creates a copy of `a`, or an empty array if `a` is null.
If `deepCopy` is true, nested mutable collections will be recursively copied too. */
static Retained<MutableArray> newArray(const Array *a, CopyFlags flags =kDefaultCopy) {
static Ref<MutableArray> newArray(const Array *a, CopyFlags flags =kDefaultCopy) {
auto ha = retained(new internal::HeapArray(a));
if (flags)
ha->copyChildren(flags);
return ha->asMutableArray();
}

Retained<MutableArray> copy(CopyFlags f = kDefaultCopy) {return newArray(this, f);}
Ref<MutableArray> copy(CopyFlags f = kDefaultCopy) {return newArray(this, f);}

const Array* source() const {return heapArray()->_source;}
bool isChanged() const {return heapArray()->isChanged();}
Expand Down
4 changes: 2 additions & 2 deletions Fleece/Mutable/MutableDict.hh
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ namespace fleece { namespace impl {
class MutableDict : public Dict {
public:

static Retained<MutableDict> newDict(const Dict *d =nullptr, CopyFlags flags =kDefaultCopy) {
static Ref<MutableDict> newDict(const Dict *d =nullptr, CopyFlags flags =kDefaultCopy) {
return retained(new internal::HeapDict(d, flags))->asMutableDict();
}

Retained<MutableDict> copy(CopyFlags f =kDefaultCopy) {return newDict(this, f);}
Ref<MutableDict> copy(CopyFlags f =kDefaultCopy) {return newDict(this, f);}

const Dict* source() const {return heapDict()->_source;}
bool isChanged() const {return heapDict()->isChanged();}
Expand Down
5 changes: 2 additions & 3 deletions Fleece/Mutable/ValueSlot.hh
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,8 @@ namespace fleece { namespace impl {

// These methods allow set(Value*) to be called, without allowing any other pointer type
// to be used; without them, set(Foo*) would incorrectly call set(bool).
template <class T> void set(const T* t) {setValue(t);}
template <class T> void set(const Retained<T> &t) {setValue(t);}
template <class T> void set(const RetainedConst<T> &t) {setValue(t);}
template <class T> void set(const T* t) {setValue(t);}
template <class T, Nullability N> void set(const Retained<T,N> &t) {setValue(t);}

/** Replaces an external value with a copy of itself. */
void copyValue(CopyFlags);
Expand Down
4 changes: 3 additions & 1 deletion Tests/EncoderTests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,9 @@ class EncoderTests {

#if FL_HAVE_TEST_FILES
TEST_CASE_METHOD(EncoderTests, "Encode To File", "[Encoder]") {
auto doc = readTestFile("1000people.fleece");
if (!sCreated1000PeopleFile)
create100PeopleFleeceFile();
auto doc = readTestFile("1000people.fleece");
auto root = Value::fromTrustedData(doc)->asArray();

{
Expand Down