Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_MIRROR_STRING_H_ |
| 18 | #define ART_RUNTIME_MIRROR_STRING_H_ |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 19 | |
| 20 | #include "class.h" |
| 21 | #include "gtest/gtest.h" |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 22 | #include "root_visitor.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 23 | |
| 24 | namespace art { |
| 25 | |
| 26 | struct StringClassOffsets; |
| 27 | struct StringOffsets; |
| 28 | class StringPiece; |
| 29 | |
| 30 | namespace mirror { |
| 31 | |
| 32 | // C++ mirror of java.lang.String |
| 33 | class MANAGED String : public Object { |
| 34 | public: |
| 35 | static MemberOffset CountOffset() { |
| 36 | return OFFSET_OF_OBJECT_MEMBER(String, count_); |
| 37 | } |
| 38 | |
| 39 | static MemberOffset ValueOffset() { |
| 40 | return OFFSET_OF_OBJECT_MEMBER(String, array_); |
| 41 | } |
| 42 | |
| 43 | static MemberOffset OffsetOffset() { |
| 44 | return OFFSET_OF_OBJECT_MEMBER(String, offset_); |
| 45 | } |
| 46 | |
| 47 | const CharArray* GetCharArray() const; |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 48 | CharArray* GetCharArray(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 49 | |
| 50 | int32_t GetOffset() const { |
| 51 | int32_t result = GetField32(OffsetOffset(), false); |
| 52 | DCHECK_LE(0, result); |
| 53 | return result; |
| 54 | } |
| 55 | |
| 56 | int32_t GetLength() const; |
| 57 | |
| 58 | int32_t GetHashCode() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 59 | |
| 60 | void ComputeHashCode() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 61 | |
| 62 | int32_t GetUtfLength() const; |
| 63 | |
| 64 | uint16_t CharAt(int32_t index) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 65 | |
| 66 | String* Intern() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 67 | |
| 68 | static String* AllocFromUtf16(Thread* self, |
| 69 | int32_t utf16_length, |
| 70 | const uint16_t* utf16_data_in, |
| 71 | int32_t hash_code = 0) |
| 72 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 73 | |
| 74 | static String* AllocFromModifiedUtf8(Thread* self, const char* utf) |
| 75 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 76 | |
| 77 | static String* AllocFromModifiedUtf8(Thread* self, int32_t utf16_length, |
| 78 | const char* utf8_data_in) |
| 79 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 80 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 81 | bool Equals(const char* modified_utf8) const |
| 82 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 83 | |
| 84 | // TODO: do we need this overload? give it a more intention-revealing name. |
| 85 | bool Equals(const StringPiece& modified_utf8) const |
| 86 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 87 | |
| 88 | bool Equals(const String* that) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 89 | |
| 90 | // Compare UTF-16 code point values not in a locale-sensitive manner |
| 91 | int Compare(int32_t utf16_length, const char* utf8_data_in); |
| 92 | |
| 93 | // TODO: do we need this overload? give it a more intention-revealing name. |
| 94 | bool Equals(const uint16_t* that_chars, int32_t that_offset, |
| 95 | int32_t that_length) const |
| 96 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 97 | |
| 98 | // Create a modified UTF-8 encoded std::string from a java/lang/String object. |
| 99 | std::string ToModifiedUtf8() const; |
| 100 | |
| 101 | int32_t FastIndexOf(int32_t ch, int32_t start) const; |
| 102 | |
| 103 | int32_t CompareTo(String* other) const; |
| 104 | |
| 105 | static Class* GetJavaLangString() { |
| 106 | DCHECK(java_lang_String_ != NULL); |
| 107 | return java_lang_String_; |
| 108 | } |
| 109 | |
| 110 | static void SetClass(Class* java_lang_String); |
| 111 | static void ResetClass(); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 112 | static void VisitRoots(RootVisitor* visitor, void* arg) |
| 113 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 114 | |
| 115 | private: |
| 116 | void SetHashCode(int32_t new_hash_code) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 117 | DCHECK_EQ(0u, GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false)); |
| 118 | SetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), new_hash_code, false); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | void SetCount(int32_t new_count) { |
| 122 | DCHECK_LE(0, new_count); |
| 123 | SetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), new_count, false); |
| 124 | } |
| 125 | |
| 126 | void SetOffset(int32_t new_offset) { |
| 127 | DCHECK_LE(0, new_offset); |
| 128 | DCHECK_GE(GetLength(), new_offset); |
| 129 | SetField32(OFFSET_OF_OBJECT_MEMBER(String, offset_), new_offset, false); |
| 130 | } |
| 131 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 132 | static String* Alloc(Thread* self, int32_t utf16_length) |
| 133 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 134 | |
| 135 | static String* Alloc(Thread* self, const SirtRef<CharArray>& array) |
| 136 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 137 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 138 | void SetArray(CharArray* new_array) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 139 | |
| 140 | // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses". |
| 141 | CharArray* array_; |
| 142 | |
| 143 | int32_t count_; |
| 144 | |
| 145 | uint32_t hash_code_; |
| 146 | |
| 147 | int32_t offset_; |
| 148 | |
| 149 | static Class* java_lang_String_; |
| 150 | |
| 151 | friend struct art::StringOffsets; // for verifying offset information |
| 152 | FRIEND_TEST(ObjectTest, StringLength); // for SetOffset and SetCount |
| 153 | DISALLOW_IMPLICIT_CONSTRUCTORS(String); |
| 154 | }; |
| 155 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 156 | class MANAGED StringClass : public Class { |
| 157 | private: |
| 158 | CharArray* ASCII_; |
| 159 | Object* CASE_INSENSITIVE_ORDER_; |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 160 | uint32_t REPLACEMENT_CHAR_; |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 161 | int64_t serialVersionUID_; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 162 | friend struct art::StringClassOffsets; // for verifying offset information |
| 163 | DISALLOW_IMPLICIT_CONSTRUCTORS(StringClass); |
| 164 | }; |
| 165 | |
| 166 | } // namespace mirror |
| 167 | } // namespace art |
| 168 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 169 | #endif // ART_RUNTIME_MIRROR_STRING_H_ |