Skip to content

Commit ddca102

Browse files
committed
Move StringData to its own header
Now that it isn't just used for `Value`, it doesn't really belong in there. Rename `static-string-data.hh` to share the same prefix to keep them close together when sorting lines, also.
1 parent 351e387 commit ddca102

File tree

17 files changed

+113
-103
lines changed

17 files changed

+113
-103
lines changed

src/libexpr-tests/json.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "nix/expr/tests/libexpr.hh"
22
#include "nix/expr/value-to-json.hh"
3-
#include "nix/expr/static-string-data.hh"
3+
#include "nix/expr/string-data-static.hh"
44

55
namespace nix {
66
// Testing the conversion to JSON

src/libexpr-tests/value/print.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include "nix/expr/tests/libexpr.hh"
2-
#include "nix/expr/static-string-data.hh"
2+
#include "nix/expr/string-data-static.hh"
33

44
#include "nix/expr/value.hh"
55
#include "nix/expr/print.hh"

src/libexpr-tests/value/value.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include "nix/expr/value.hh"
2-
#include "nix/expr/static-string-data.hh"
2+
#include "nix/expr/string-data-static.hh"
33

44
#include "nix/store/tests/libstore.hh"
55
#include <gtest/gtest.h>

src/libexpr/include/nix/expr/meson.build

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ headers = [ config_pub_h ] + files(
3131
'print.hh',
3232
'repl-exit-status.hh',
3333
'search-path.hh',
34-
'static-string-data.hh',
34+
'string-data.hh',
35+
'string-data-static.hh',
3536
'symbol-table.hh',
3637
'value-to-json.hh',
3738
'value-to-xml.hh',

src/libexpr/include/nix/expr/nixexpr.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include "nix/expr/value.hh"
1313
#include "nix/expr/symbol-table.hh"
1414
#include "nix/expr/eval-error.hh"
15-
#include "nix/expr/static-string-data.hh"
15+
#include "nix/expr/string-data-static.hh"
1616
#include "nix/util/pos-idx.hh"
1717
#include "nix/expr/counter.hh"
1818
#include "nix/util/pos-table.hh"

src/libexpr/include/nix/expr/parser-state.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
#include "nix/expr/eval.hh"
77
#include "nix/expr/value.hh"
8-
#include "nix/expr/static-string-data.hh"
8+
#include "nix/expr/string-data-static.hh"
99

1010
namespace nix {
1111

src/libexpr/include/nix/expr/static-string-data.hh renamed to src/libexpr/include/nix/expr/string-data-static.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22
///@file
33

4-
#include "nix/expr/value.hh"
4+
#include "nix/expr/string-data.hh"
55

66
namespace nix {
77

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#pragma once
2+
///@file
3+
4+
#include <cstddef>
5+
#include <cstring>
6+
#include <memory_resource>
7+
#include <string_view>
8+
9+
namespace nix {
10+
11+
class StringData
12+
{
13+
public:
14+
using size_type = std::size_t;
15+
16+
size_type size_;
17+
char data_[];
18+
19+
/*
20+
* This in particular ensures that we cannot have a `StringData`
21+
* that we use by value, which is just what we want!
22+
*
23+
* Dynamically sized types aren't a thing in C++ and even flexible array
24+
* members are a language extension and beyond the realm of standard C++.
25+
* Technically, sizeof data_ member is 0 and the intended way to use flexible
26+
* array members is to allocate sizeof(StrindData) + count * sizeof(char) bytes
27+
* and the compiler will consider alignment restrictions for the FAM.
28+
*
29+
*/
30+
31+
StringData(StringData &&) = delete;
32+
StringData & operator=(StringData &&) = delete;
33+
StringData(const StringData &) = delete;
34+
StringData & operator=(const StringData &) = delete;
35+
~StringData() = default;
36+
37+
private:
38+
StringData() = delete;
39+
40+
explicit StringData(size_type size)
41+
: size_(size)
42+
{
43+
}
44+
45+
public:
46+
/**
47+
* Allocate StringData on the (possibly) GC-managed heap and copy
48+
* the contents of s to it.
49+
*/
50+
static const StringData & make(std::string_view s);
51+
52+
/**
53+
* Allocate StringData on the (possibly) GC-managed heap.
54+
* @param size Length of the string (without the NUL terminator).
55+
*/
56+
static StringData & alloc(size_t size);
57+
58+
size_t size() const
59+
{
60+
return size_;
61+
}
62+
63+
char * data() noexcept
64+
{
65+
return data_;
66+
}
67+
68+
const char * data() const noexcept
69+
{
70+
return data_;
71+
}
72+
73+
const char * c_str() const noexcept
74+
{
75+
return data_;
76+
}
77+
78+
constexpr std::string_view view() const noexcept
79+
{
80+
return std::string_view(data_, size_);
81+
}
82+
83+
template<size_t N>
84+
struct Static;
85+
86+
static StringData & make(std::pmr::memory_resource & resource, std::string_view s)
87+
{
88+
auto & res =
89+
*new (resource.allocate(sizeof(StringData) + s.size() + 1, alignof(StringData))) StringData(s.size());
90+
std::memcpy(res.data_, s.data(), s.size());
91+
res.data_[s.size()] = '\0';
92+
return res;
93+
}
94+
};
95+
96+
} // namespace nix

src/libexpr/include/nix/expr/symbol-table.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#include <memory_resource>
55
#include "nix/expr/value.hh"
6-
#include "nix/expr/static-string-data.hh"
6+
#include "nix/expr/string-data-static.hh"
77
#include "nix/util/chunked-vector.hh"
88
#include "nix/util/error.hh"
99

src/libexpr/include/nix/expr/value.hh

Lines changed: 1 addition & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@
33

44
#include <bit>
55
#include <cassert>
6-
#include <cstddef>
7-
#include <cstring>
86
#include <memory>
9-
#include <memory_resource>
107
#include <span>
118
#include <string_view>
129
#include <type_traits>
1310
#include <concepts>
1411

1512
#include "nix/expr/eval-gc.hh"
13+
#include "nix/expr/string-data.hh"
1614
#include "nix/expr/value/context.hh"
1715
#include "nix/util/source-path.hh"
1816
#include "nix/expr/print-options.hh"
@@ -193,91 +191,6 @@ public:
193191
friend struct Value;
194192
};
195193

196-
class StringData
197-
{
198-
public:
199-
using size_type = std::size_t;
200-
201-
size_type size_;
202-
char data_[];
203-
204-
/*
205-
* This in particular ensures that we cannot have a `StringData`
206-
* that we use by value, which is just what we want!
207-
*
208-
* Dynamically sized types aren't a thing in C++ and even flexible array
209-
* members are a language extension and beyond the realm of standard C++.
210-
* Technically, sizeof data_ member is 0 and the intended way to use flexible
211-
* array members is to allocate sizeof(StrindData) + count * sizeof(char) bytes
212-
* and the compiler will consider alignment restrictions for the FAM.
213-
*
214-
*/
215-
216-
StringData(StringData &&) = delete;
217-
StringData & operator=(StringData &&) = delete;
218-
StringData(const StringData &) = delete;
219-
StringData & operator=(const StringData &) = delete;
220-
~StringData() = default;
221-
222-
private:
223-
StringData() = delete;
224-
225-
explicit StringData(size_type size)
226-
: size_(size)
227-
{
228-
}
229-
230-
public:
231-
/**
232-
* Allocate StringData on the (possibly) GC-managed heap and copy
233-
* the contents of s to it.
234-
*/
235-
static const StringData & make(std::string_view s);
236-
237-
/**
238-
* Allocate StringData on the (possibly) GC-managed heap.
239-
* @param size Length of the string (without the NUL terminator).
240-
*/
241-
static StringData & alloc(size_t size);
242-
243-
size_t size() const
244-
{
245-
return size_;
246-
}
247-
248-
char * data() noexcept
249-
{
250-
return data_;
251-
}
252-
253-
const char * data() const noexcept
254-
{
255-
return data_;
256-
}
257-
258-
const char * c_str() const noexcept
259-
{
260-
return data_;
261-
}
262-
263-
constexpr std::string_view view() const noexcept
264-
{
265-
return std::string_view(data_, size_);
266-
}
267-
268-
template<size_t N>
269-
struct Static;
270-
271-
static StringData & make(std::pmr::memory_resource & resource, std::string_view s)
272-
{
273-
auto & res =
274-
*new (resource.allocate(sizeof(StringData) + s.size() + 1, alignof(StringData))) StringData(s.size());
275-
std::memcpy(res.data_, s.data(), s.size());
276-
res.data_[s.size()] = '\0';
277-
return res;
278-
}
279-
};
280-
281194
namespace detail {
282195

283196
/**

0 commit comments

Comments
 (0)