CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
string_writer.h
1
2#ifndef string_writer_h
3#define string_writer_h
4
5#include "fwd.h"
6
7#include "system/iformatprovider.h"
8#include "system/exceptions.h"
9#include "system/reflection.h"
10#include "system/globalization/culture_info.h"
11#include "system/io/text_writer.h"
12#include "system/text/string_builder.h"
13#include "system/text/unicode_encoding.h"
14
15namespace System { namespace IO {
20 class ASPOSECPP_SHARED_CLASS StringWriter : public TextWriter
21 {
22 RTTI_INFO(System::IO::StringWriter, ::System::BaseTypesInfo<System::IO::TextWriter>)
23
24 public:
28 StringWriter(const System::SharedPtr<Text::StringBuilder>& sb, const IFormatProviderPtr& formatProvider) : TextWriter(formatProvider)
29 {
30 if (sb == nullptr)
31 throw ArgumentNullException(u"StringBuilder");
32 _sb = sb;
33 }
34
38 : StringWriter(sb, Globalization::CultureInfo::get_CurrentCulture()) {}
39
42 StringWriter(const IFormatProviderPtr& formatProvider)
43 : StringWriter(MakeObject<Text::StringBuilder>(), formatProvider) {}
44
47 : StringWriter(MakeObject<Text::StringBuilder>(), Globalization::CultureInfo::get_CurrentCulture()) {}
48
51 {
52 if (m_Encoding == nullptr)
53 m_Encoding = MakeObject<Text::UnicodeEncoding>(false, false);
54 return m_Encoding;
55 };
56
59 {
60 return _sb;
61 }
62
65 virtual void Write(char_t value) override {
66 _sb->Append(value);
67 }
68
73 virtual void Write(const ArrayPtr<char_t>& buffer, int32_t index, int32_t count) override {
74 if (buffer == nullptr)
75 throw ArgumentNullException(u"buffer");
76 if (index < 0)
77 throw ArgumentOutOfRangeException(u"index");
78 if (count < 0)
79 throw ArgumentOutOfRangeException(u"count");
80 if (buffer->get_Length() - index < count)
81 throw ArgumentException(u"insufficient buffer length");
82
83 _sb->Append(buffer, index, count);
84 }
85
88 virtual void Write(const String& value) override {
89 if (!value.IsNull()) _sb->Append(value);
90 }
91
93 virtual String ToString() const override
94 {
95 return _sb->ToString();
96 }
97
98 private:
99
101 SharedPtr<Text::StringBuilder> _sb = nullptr;
103 static SharedPtr<Text::UnicodeEncoding> ASPOSECPP_SHARED_API m_Encoding;
104
105 };
106
107}} // namespace System{ namespace IO{
108
109#endif //string_writer_h
Implements a TextWriter that writes information to a string. Objects of this class should only be all...
Definition: string_writer.h:21
StringWriter(const IFormatProviderPtr &formatProvider)
Constructs a new instance of StringWriter using the specified IFormatProvider.
Definition: string_writer.h:42
virtual void Write(char_t value) override
Writes the specified character to the stream.
Definition: string_writer.h:65
StringWriter(const System::SharedPtr< Text::StringBuilder > &sb, const IFormatProviderPtr &formatProvider)
Constructs a new instance of StringWriter using the specified StringBuilder and IFormatProvider.
Definition: string_writer.h:28
virtual SharedPtr< Text::Encoding > get_Encoding() override
Returns the currently used encoding.
Definition: string_writer.h:50
virtual SharedPtr< Text::StringBuilder > GetStringBuilder()
Returns the currently used StringBuilder.
Definition: string_writer.h:58
virtual void Write(const String &value) override
Writes the specified string to the stream.
Definition: string_writer.h:88
StringWriter(const System::SharedPtr< Text::StringBuilder > &sb)
Constructs a new instance of StringWriter using the specified StringBuilder and IFormatProvider from ...
Definition: string_writer.h:37
StringWriter()
Constructs a new instance of StringWriter using IFormatProvider from the current culture.
Definition: string_writer.h:46
virtual String ToString() const override
Returns the underlying string.
Definition: string_writer.h:93
virtual void Write(const ArrayPtr< char_t > &buffer, int32_t index, int32_t count) override
Writes the specified subrange of characters from the specified character array to the stream.
Definition: string_writer.h:73
A base class for classes that represent writers that writes sequences of characters to different dest...
Definition: text_writer.h:21
Pointer class to wrap types being allocated on heap. Use it to manage memory for classes inheriting O...
Definition: smart_ptr.h:180
String class used across the library. Is a substitute for C# System.String when translating code....
Definition: string.h:122
String ToString() const
Wrapper for handling String class in contexts where ToString() is being called on value type objects.
Definition: string.h:494
bool IsNull() const
Checks if string is considered null. String is null and only if it is constructed via String() constr...
Definition: string.h:280
Definition: db_command.h:9
std::enable_if<!IsSmartPtr< T >::value, SmartPtr< T > >::type MakeObject(Args &&... args)
Creates object on heap and returns shared pointer to it.
Definition: smart_ptr.h:1506