summaryrefslogtreecommitdiff
path: root/AK
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-02-25 16:04:08 +0100
committerAndreas Kling <awesomekling@gmail.com>2019-02-25 16:04:08 +0100
commit15fb917f28505539b05b9b21de0aae7055a5ab2e (patch)
tree3a64297ee2ee684aa03ce095bede85dadb953b31 /AK
parent2cfcbdc735e7578a22ed2f9ae25492a72abe3f3f (diff)
downloadserenity-15fb917f28505539b05b9b21de0aae7055a5ab2e.zip
Convert more RetainPtr use to Retained.
Diffstat (limited to 'AK')
-rw-r--r--AK/AKString.h5
-rw-r--r--AK/Retained.h1
-rw-r--r--AK/StringImpl.cpp28
-rw-r--r--AK/StringImpl.h6
4 files changed, 15 insertions, 25 deletions
diff --git a/AK/AKString.h b/AK/AKString.h
index 29438e2369..1dfc924915 100644
--- a/AK/AKString.h
+++ b/AK/AKString.h
@@ -44,6 +44,11 @@ public:
{
}
+ String(Retained<StringImpl>&& impl)
+ : m_impl(move(impl))
+ {
+ }
+
unsigned to_uint(bool& ok) const;
String to_lowercase() const
diff --git a/AK/Retained.h b/AK/Retained.h
index aed6308ae7..849e96b2b5 100644
--- a/AK/Retained.h
+++ b/AK/Retained.h
@@ -36,6 +36,7 @@ public:
enum AdoptTag { Adopt };
RETURN_TYPESTATE(unconsumed) Retained(T& object) : m_ptr(&object) { m_ptr->retain(); }
+ template<typename U> RETURN_TYPESTATE(unconsumed) Retained(U& object) : m_ptr(&static_cast<T&>(object)) { m_ptr->retain(); }
RETURN_TYPESTATE(unconsumed) Retained(AdoptTag, T& object) : m_ptr(&object) { }
RETURN_TYPESTATE(unconsumed) Retained(Retained& other) : m_ptr(&other.copy_ref().leak_ref()) { }
RETURN_TYPESTATE(unconsumed) Retained(Retained&& other) : m_ptr(&other.leak_ref()) { }
diff --git a/AK/StringImpl.cpp b/AK/StringImpl.cpp
index f36ffe8ce1..dedf4ac909 100644
--- a/AK/StringImpl.cpp
+++ b/AK/StringImpl.cpp
@@ -55,13 +55,11 @@ static inline size_t allocation_size_for_stringimpl(size_t length)
return sizeof(StringImpl) + (sizeof(char) * length) + sizeof(char);
}
-RetainPtr<StringImpl> StringImpl::create_uninitialized(size_t length, char*& buffer)
+Retained<StringImpl> StringImpl::create_uninitialized(size_t length, char*& buffer)
{
ASSERT(length);
void* slot = kmalloc(allocation_size_for_stringimpl(length));
- if (!slot)
- return nullptr;
-
+ ASSERT(slot);
auto new_stringimpl = adopt(*new (slot) StringImpl(ConstructWithInlineBuffer, length));
buffer = const_cast<char*>(new_stringimpl->m_characters);
buffer[length] = '\0';
@@ -81,8 +79,6 @@ RetainPtr<StringImpl> StringImpl::create(const char* cstring, size_t length, Sho
char* buffer;
auto new_stringimpl = create_uninitialized(length, buffer);
- if (!new_stringimpl)
- return nullptr;
memcpy(buffer, cstring, length * sizeof(char));
if (shouldChomp && buffer[length - 1] == '\n') {
@@ -125,47 +121,35 @@ static inline char to_ascii_uppercase(char c)
return c;
}
-RetainPtr<StringImpl> StringImpl::to_lowercase() const
+Retained<StringImpl> StringImpl::to_lowercase() const
{
- if (!m_length)
- return const_cast<StringImpl*>(this);
-
for (size_t i = 0; i < m_length; ++i) {
if (!is_ascii_lowercase(m_characters[i]))
goto slow_path;
}
- return const_cast<StringImpl*>(this);
+ return const_cast<StringImpl&>(*this);
slow_path:
char* buffer;
auto lowercased = create_uninitialized(m_length, buffer);
- if (!lowercased)
- return nullptr;
for (size_t i = 0; i < m_length; ++i)
buffer[i] = to_ascii_lowercase(m_characters[i]);
-
return lowercased;
}
-RetainPtr<StringImpl> StringImpl::to_uppercase() const
+Retained<StringImpl> StringImpl::to_uppercase() const
{
- if (!m_length)
- return const_cast<StringImpl*>(this);
-
for (size_t i = 0; i < m_length; ++i) {
if (!is_ascii_uppercase(m_characters[i]))
goto slow_path;
}
- return const_cast<StringImpl*>(this);
+ return const_cast<StringImpl&>(*this);
slow_path:
char* buffer;
auto uppercased = create_uninitialized(m_length, buffer);
- if (!uppercased)
- return nullptr;
for (size_t i = 0; i < m_length; ++i)
buffer[i] = to_ascii_uppercase(m_characters[i]);
-
return uppercased;
}
diff --git a/AK/StringImpl.h b/AK/StringImpl.h
index aba8ea8d4b..c2a1fdf78f 100644
--- a/AK/StringImpl.h
+++ b/AK/StringImpl.h
@@ -10,11 +10,11 @@ enum ShouldChomp { NoChomp, Chomp };
class StringImpl : public Retainable<StringImpl> {
public:
- static RetainPtr<StringImpl> create_uninitialized(size_t length, char*& buffer);
+ static Retained<StringImpl> create_uninitialized(size_t length, char*& buffer);
static RetainPtr<StringImpl> create(const char* cstring, ShouldChomp = NoChomp);
static RetainPtr<StringImpl> create(const char* cstring, size_t length, ShouldChomp = NoChomp);
- RetainPtr<StringImpl> to_lowercase() const;
- RetainPtr<StringImpl> to_uppercase() const;
+ Retained<StringImpl> to_lowercase() const;
+ Retained<StringImpl> to_uppercase() const;
static StringImpl& the_empty_stringimpl();