Skip to content

Commit 936b239

Browse files
committed
Inline some methods
1 parent 71176e1 commit 936b239

13 files changed

+52
-268
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*.layout
44
*.cbtemp
55
*.bak
6+
*.swp
67
/bin
78
/lib
89
/obj

include/tag_array.h

+14-12
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,12 @@ class tag_array final : public detail::crtp_tag<tag_array<T>>
6262
tag_array() {}
6363

6464
///Constructs an array with the given values
65-
tag_array(std::initializer_list<T> init);
66-
tag_array(std::vector<T>&& vec) noexcept;
65+
tag_array(std::initializer_list<T> init): data(init) {}
66+
tag_array(std::vector<T>&& vec) noexcept: data(std::move(vec)) {}
6767

6868
///Returns a reference to the vector that contains the values
69-
std::vector<T>& get();
70-
const std::vector<T>& get() const;
69+
std::vector<T>& get() { return data; }
70+
const std::vector<T>& get() const { return data; }
7171

7272
/**
7373
* @brief Accesses a value by index with bounds checking
@@ -81,20 +81,20 @@ class tag_array final : public detail::crtp_tag<tag_array<T>>
8181
*
8282
* No bounds checking is performed.
8383
*/
84-
T& operator[](size_t i);
85-
T operator[](size_t i) const;
84+
T& operator[](size_t i) { return data[i]; }
85+
T operator[](size_t i) const { return data[i]; }
8686

8787
///Appends a value at the end of the array
88-
void push_back(T val);
88+
void push_back(T val) { data.push_back(val); }
8989

9090
///Removes the last element from the array
91-
void pop_back();
91+
void pop_back() { data.pop_back(); }
9292

9393
///Returns the number of values in the array
94-
size_t size() const;
94+
size_t size() const { return data.size(); }
9595

9696
///Erases all values from the array.
97-
void clear();
97+
void clear() { data.clear(); }
9898

9999
//Iterators
100100
iterator begin();
@@ -115,8 +115,10 @@ class tag_array final : public detail::crtp_tag<tag_array<T>>
115115
std::vector<T> data;
116116
};
117117

118-
template<class T> bool operator==(const tag_array<T>& lhs, const tag_array<T>& rhs);
119-
template<class T> bool operator!=(const tag_array<T>& lhs, const tag_array<T>& rhs);
118+
template<class T> bool operator==(const tag_array<T>& lhs, const tag_array<T>& rhs)
119+
{ return lhs.get() == rhs.get(); }
120+
template<class T> bool operator!=(const tag_array<T>& lhs, const tag_array<T>& rhs)
121+
{ return !(lhs == rhs); }
120122

121123
//Typedefs that should be used instead of the template tag_array.
122124
typedef tag_array<int8_t> tag_byte_array;

include/tag_compound.h

+6-4
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,10 @@ class tag_compound final : public detail::crtp_tag<tag_compound>
106106
bool has_key(const std::string& key, tag_type type) const;
107107

108108
///Returns the number of tags in the compound
109-
size_t size() const;
109+
size_t size() const { return tags.size(); }
110110

111111
///Erases all tags from the compound
112-
void clear();
112+
void clear() { tags.clear(); }
113113

114114
//Iterators
115115
iterator begin();
@@ -122,8 +122,10 @@ class tag_compound final : public detail::crtp_tag<tag_compound>
122122
void read_payload(io::stream_reader& reader) override;
123123
void write_payload(io::stream_writer& writer) const override;
124124

125-
friend bool operator==(const tag_compound& lhs, const tag_compound& rhs);
126-
friend bool operator!=(const tag_compound& lhs, const tag_compound& rhs);
125+
friend bool operator==(const tag_compound& lhs, const tag_compound& rhs)
126+
{ return lhs.tags == rhs.tags; }
127+
friend bool operator!=(const tag_compound& lhs, const tag_compound& rhs)
128+
{ return !(lhs == rhs); }
127129

128130
private:
129131
map_t_ tags;

include/tag_list.h

+8-8
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ class tag_list final : public detail::crtp_tag<tag_list>
6666
*
6767
* The content type is determined when the first tag is added.
6868
*/
69-
tag_list();
69+
tag_list(): tag_list(tag_type::Null) {}
7070

7171
///Constructs an empty list with the given content type
72-
explicit tag_list(tag_type type);
72+
explicit tag_list(tag_type type): el_type_(type) {}
7373

7474
///Constructs a list with the given contents
7575
tag_list(std::initializer_list<int8_t> init);
@@ -106,8 +106,8 @@ class tag_list final : public detail::crtp_tag<tag_list>
106106
* Returns a value to the tag at the specified index. No bounds checking
107107
* is performed.
108108
*/
109-
value& operator[](size_t i);
110-
const value& operator[](size_t i) const;
109+
value& operator[](size_t i) { return tags[i]; }
110+
const value& operator[](size_t i) const { return tags[i]; }
111111

112112
/**
113113
* @brief Assigns a value at the given index
@@ -133,16 +133,16 @@ class tag_list final : public detail::crtp_tag<tag_list>
133133
void emplace_back(Args&&... args);
134134

135135
///Removes the last element of the list
136-
void pop_back();
136+
void pop_back() { tags.pop_back(); }
137137

138138
///Returns the content type of the list, or tag_type::Null if undetermined
139-
tag_type el_type() const;
139+
tag_type el_type() const { return el_type_; }
140140

141141
///Returns the number of tags in the list
142-
size_t size() const;
142+
size_t size() const { return tags.size(); }
143143

144144
///Erases all tags from the list. Preserves the content type.
145-
void clear();
145+
void clear() { tags.clear(); }
146146

147147
/**
148148
* @brief Erases all tags from the list and changes the content type.

include/tag_string.h

+10-8
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ class tag_string final : public detail::crtp_tag<tag_string>
3535

3636
//Constructors
3737
tag_string() {}
38-
tag_string(const std::string& str);
39-
tag_string(std::string&& str) noexcept;
40-
tag_string(const char* str);
38+
tag_string(const std::string& str): value(str) {}
39+
tag_string(std::string&& str) noexcept: value(std::move(str)) {}
40+
tag_string(const char* str): value(str) {}
4141

4242
//Getters
43-
operator std::string&();
44-
operator const std::string&() const;
45-
const std::string& get() const;
43+
operator std::string&() { return value; }
44+
operator const std::string&() const { return value; }
45+
const std::string& get() const { return value; }
4646

4747
//Setters
4848
tag_string& operator=(const std::string& str);
@@ -62,8 +62,10 @@ class tag_string final : public detail::crtp_tag<tag_string>
6262
std::string value;
6363
};
6464

65-
bool operator==(const tag_string& lhs, const tag_string& rhs);
66-
bool operator!=(const tag_string& lhs, const tag_string& rhs);
65+
inline bool operator==(const tag_string& lhs, const tag_string& rhs)
66+
{ return lhs.get() == rhs.get(); }
67+
inline bool operator!=(const tag_string& lhs, const tag_string& rhs)
68+
{ return !(lhs == rhs); }
6769

6870
}
6971

include/value.h

+9-9
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class value
6262
public:
6363
//Constructors
6464
value() noexcept {}
65-
explicit value(std::unique_ptr<tag>&& t) noexcept;
65+
explicit value(std::unique_ptr<tag>&& t) noexcept: tag_(std::move(t)) {}
6666
explicit value(tag&& t);
6767

6868
//Moving
@@ -87,10 +87,10 @@ class value
8787
*
8888
* If the value is uninitialized, the behavior is undefined.
8989
*/
90-
operator tag&();
91-
operator const tag&() const;
92-
tag& get();
93-
const tag& get() const;
90+
operator tag&() { return get(); }
91+
operator const tag&() const { return get(); }
92+
tag& get() { return *tag_; }
93+
const tag& get() const { return *tag_; }
9494

9595
/**
9696
* @brief Returns a reference to the contained tag as an instance of T
@@ -143,7 +143,7 @@ class value
143143
explicit operator const std::string&() const;
144144

145145
///Returns true if the value is not uninitialized
146-
explicit operator bool() const;
146+
explicit operator bool() const { return tag_ != nullptr; }
147147

148148
/**
149149
* @brief In case of a tag_compound, accesses a tag by key with bounds checking
@@ -189,10 +189,10 @@ class value
189189
const value& operator[](size_t i) const;
190190

191191
///Returns a reference to the underlying std::unique_ptr<tag>
192-
std::unique_ptr<tag>& get_ptr();
193-
const std::unique_ptr<tag>& get_ptr() const;
192+
std::unique_ptr<tag>& get_ptr() { return tag_; }
193+
const std::unique_ptr<tag>& get_ptr() const { return tag_; }
194194
///Resets the underlying std::unique_ptr<tag> to a different value
195-
void set_ptr(std::unique_ptr<tag>&& t);
195+
void set_ptr(std::unique_ptr<tag>&& t) { tag_ = std::move(t); }
196196

197197
///@sa tag::get_type
198198
tag_type get_type() const;

include/value_initializer.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ namespace nbt
4444
class value_initializer : public value
4545
{
4646
public:
47-
value_initializer(std::unique_ptr<tag>&& t) noexcept;
48-
value_initializer(std::nullptr_t) noexcept;
49-
value_initializer(value&& val) noexcept;
50-
value_initializer(tag&& t);
47+
value_initializer(std::unique_ptr<tag>&& t) noexcept: value(std::move(t)) {}
48+
value_initializer(std::nullptr_t) noexcept : value(nullptr) {}
49+
value_initializer(value&& val) noexcept : value(std::move(val)) {}
50+
value_initializer(tag&& t) : value(std::move(t)) {}
5151

5252
value_initializer(int8_t val);
5353
value_initializer(int16_t val);

src/tag_array.cpp

-74
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,6 @@
2525
namespace nbt
2626
{
2727

28-
template<class T>
29-
tag_array<T>::tag_array(std::initializer_list<T> init):
30-
data(init)
31-
{}
32-
33-
template<class T>
34-
tag_array<T>::tag_array(std::vector<T>&& vec) noexcept:
35-
data(std::move(vec))
36-
{}
37-
38-
template<class T>
39-
std::vector<T>& tag_array<T>::get()
40-
{
41-
return data;
42-
}
43-
44-
template<class T>
45-
const std::vector<T>& tag_array<T>::get() const
46-
{
47-
return data;
48-
}
49-
5028
template<class T>
5129
T& tag_array<T>::at(size_t i)
5230
{
@@ -59,42 +37,6 @@ T tag_array<T>::at(size_t i) const
5937
return data.at(i);
6038
}
6139

62-
template<class T>
63-
T& tag_array<T>::operator[](size_t i)
64-
{
65-
return data[i];
66-
}
67-
68-
template<class T>
69-
T tag_array<T>::operator[](size_t i) const
70-
{
71-
return data[i];
72-
}
73-
74-
template<class T>
75-
void tag_array<T>::push_back(T val)
76-
{
77-
data.push_back(val);
78-
}
79-
80-
template<class T>
81-
void tag_array<T>::pop_back()
82-
{
83-
data.pop_back();
84-
}
85-
86-
template<class T>
87-
size_t tag_array<T>::size() const
88-
{
89-
return data.size();
90-
}
91-
92-
template<class T>
93-
void tag_array<T>::clear()
94-
{
95-
data.clear();
96-
}
97-
9840
template<class T> auto tag_array<T>::begin() -> iterator { return data.begin(); }
9941
template<class T> auto tag_array<T>::end() -> iterator { return data.end(); }
10042
template<class T> auto tag_array<T>::begin() const -> const_iterator { return data.begin(); }
@@ -168,24 +110,8 @@ void tag_array<int32_t>::write_payload(io::stream_writer& writer) const
168110
writer.write_num(i);
169111
}
170112

171-
template<class T>
172-
bool operator==(const tag_array<T>& lhs, const tag_array<T>& rhs)
173-
{
174-
return lhs.get() == rhs.get();
175-
}
176-
177-
template<class T>
178-
bool operator!=(const tag_array<T>& lhs, const tag_array<T>& rhs)
179-
{
180-
return !(lhs == rhs);
181-
}
182-
183113
//Enforce template instantiations
184114
template class tag_array<int8_t>;
185115
template class tag_array<int32_t>;
186-
template bool operator==<int8_t> (const tag_array<int8_t>& , const tag_array<int8_t>&);
187-
template bool operator==<int32_t>(const tag_array<int32_t>&, const tag_array<int32_t>&);
188-
template bool operator!=<int8_t> (const tag_array<int8_t>& , const tag_array<int8_t>&);
189-
template bool operator!=<int32_t>(const tag_array<int32_t>&, const tag_array<int32_t>&);
190116

191117
}

src/tag_compound.cpp

-20
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,6 @@ bool tag_compound::has_key(const std::string& key, tag_type type) const
8282
return it != tags.end() && it->second.get_type() == type;
8383
}
8484

85-
size_t tag_compound::size() const
86-
{
87-
return tags.size();
88-
}
89-
90-
void tag_compound::clear()
91-
{
92-
tags.clear();
93-
}
94-
9585
auto tag_compound::begin() -> iterator { return tags.begin(); }
9686
auto tag_compound::end() -> iterator { return tags.end(); }
9787
auto tag_compound::begin() const -> const_iterator { return tags.begin(); }
@@ -128,14 +118,4 @@ void tag_compound::write_payload(io::stream_writer& writer) const
128118
writer.write_type(tag_type::End);
129119
}
130120

131-
bool operator==(const tag_compound& lhs, const tag_compound& rhs)
132-
{
133-
return lhs.tags == rhs.tags;
134-
}
135-
136-
bool operator!=(const tag_compound& lhs, const tag_compound& rhs)
137-
{
138-
return !(lhs == rhs);
139-
}
140-
141121
}

0 commit comments

Comments
 (0)