blob: 94dcc3b4e8cdff334a90810dd833057289ed1349 [file] [log] [blame]
Olli Etuaho12c03762018-01-25 12:22:33 +02001//
2// Copyright (c) 2018 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6// ImmutableStringBuilder.h: Stringstream-like utility for building pool allocated strings where the
7// maximum length is known in advance.
8//
9
10#ifndef COMPILER_TRANSLATOR_IMMUTABLESTRINGBUILDER_H_
11#define COMPILER_TRANSLATOR_IMMUTABLESTRINGBUILDER_H_
12
13#include "compiler/translator/ImmutableString.h"
14
15namespace sh
16{
17
18class ImmutableStringBuilder
19{
20 public:
21 ImmutableStringBuilder(size_t maxLength)
22 : mPos(0u), mMaxLength(maxLength), mData(AllocateEmptyPoolCharArray(maxLength))
23 {
24 }
25
26 ImmutableStringBuilder &operator<<(const ImmutableString &str);
27
28 ImmutableStringBuilder &operator<<(const char *str);
29
30 ImmutableStringBuilder &operator<<(const char &c);
31
32 // This invalidates the ImmutableStringBuilder, so it should only be called once.
33 operator ImmutableString();
34
35 private:
36 inline static char *AllocateEmptyPoolCharArray(size_t strLength)
37 {
38 size_t requiredSize = strLength + 1u;
39 return reinterpret_cast<char *>(GetGlobalPoolAllocator()->allocate(requiredSize));
40 }
41
42 size_t mPos;
43 size_t mMaxLength;
44 char *mData;
45};
46
47} // namespace sh
48
49#endif // COMPILER_TRANSLATOR_IMMUTABLESTRINGBUILDER_H_