Skip to content

Commit

Permalink
Review ustring::format, console::write, console, wrte_line methods an…
Browse files Browse the repository at this point in the history
…d output stream with and without formating
  • Loading branch information
gammasoft71 committed Jul 2, 2024
1 parent 4ebe3cf commit 6bc95c5
Show file tree
Hide file tree
Showing 21 changed files with 64 additions and 61 deletions.
4 changes: 2 additions & 2 deletions src/xtd.core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ add_sources(
include/xtd/format_exception
include/xtd/func.h
include/xtd/func
include/xtd/generic_stream_output.h
include/xtd/generic_stream_output
include/xtd/guid.h
include/xtd/guid
include/xtd/iasync_result.h
Expand Down Expand Up @@ -603,8 +605,6 @@ add_sources(
include/xtd/internal/__show_generic_exception_message
include/xtd/internal/__sprintf.h
include/xtd/internal/__sprintf
include/xtd/internal/__string_formatter.h
include/xtd/internal/__string_formatter
include/xtd/internal/__xtd_debugbreak.h
include/xtd/internal/__xtd_debugbreak
include/xtd/internal/__xtd_std_version.h
Expand Down
47 changes: 26 additions & 21 deletions src/xtd.core/include/xtd/enum_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,27 +177,7 @@ namespace xtd {
/// ```cpp
/// ustring shade_name = enum_object<shade>(as<shade>(1)).to_string("F");
/// ```
xtd::ustring to_string(const xtd::ustring& format, const std::locale& loc) const override {
init();
auto fmt = format;
if (fmt.empty()) fmt = "G";

switch (fmt[0]) {
case 'b':
case 'B':
case 'd':
case 'D':
case 'o':
case 'O':
case 'x':
case 'X': return __numeric_formatter(fmt, static_cast<long long int>(value_), std::locale());
case 'f':
case 'F':
case 'g':
case 'G': return __format_stringer<char>(value_);
}
throw format_exception("Invalid format"_t);
}
xtd::ustring to_string(const xtd::ustring& format, const std::locale& loc) const override;
/// @}

/// @cond
Expand Down Expand Up @@ -637,6 +617,31 @@ namespace xtd {
/// @}
};
/// @}

/// @cond
template<typename enum_t>
xtd::ustring enum_object<enum_t>::to_string(const xtd::ustring& format, const std::locale& loc) const {
init();
auto fmt = format;
if (fmt.empty()) fmt = "G";

switch (fmt[0]) {
case 'b':
case 'B':
case 'd':
case 'D':
case 'o':
case 'O':
case 'x':
case 'X': return __numeric_formatter(fmt, static_cast<long long int>(value_), std::locale());
case 'f':
case 'F':
case 'g':
case 'G': return xtd::enum_object<>::get_name(value_);
}
throw format_exception("Invalid format"_t);
}
/// @endcond
}

/// @cond
Expand Down
2 changes: 2 additions & 0 deletions src/xtd.core/include/xtd/generic_stream_output
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#pragma once
#include "generic_stream_output.h"
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@
/// @brief Contains generic stream output methods.
/// @copyright Copyright (c) 2024 Gammasoft. All rights reserved.
#pragma once
/// @cond
#if !defined(__XTD_TUNIT_INTERNAL__)
#error "Do not include this file: Internal use only"
#endif
/// @endcond

#include <xtd/any>
#include <xtd/optional>
#include <array>
Expand Down Expand Up @@ -199,7 +193,7 @@ template <typename char_t, typename char_traits_t>
inline std::basic_ostream<char_t, char_traits_t>& operator <<(std::basic_ostream<char_t, char_traits_t>& os, const std::error_code& value) {
return os << "(value = " << value.value() << "category= " << value.category().name() << ")";
}

template <typename char_t, typename char_traits_t>
inline std::basic_ostream<char_t, char_traits_t>& operator <<(std::basic_ostream<char_t, char_traits_t>& os, std::any value) {
auto it = __any_stringer__.find(std::type_index(value.type()));
Expand Down
1 change: 1 addition & 0 deletions src/xtd.core/include/xtd/iformatable.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ namespace xtd {
virtual xtd::ustring to_string(const xtd::ustring& format, const std::locale& loc) const = 0;
/// @}


/// @cond
std::string __opague_internal_formatable__(intptr, intptr, intptr, intptr) const;
/// @endcond
Expand Down
3 changes: 2 additions & 1 deletion src/xtd.core/include/xtd/internal/__character_formatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
#endif
/// @endcond

#include "__format_stringer.h"
#include "../types.h"
#include <locale>

/// @cond
template<typename char_t, typename value_t>
Expand Down
1 change: 1 addition & 0 deletions src/xtd.core/include/xtd/internal/__currency_formatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "__binary_formatter.h"

#include <cmath>
#include <iomanip>
#include <locale>
#include <sstream>

Expand Down
4 changes: 2 additions & 2 deletions src/xtd.core/include/xtd/internal/__enum_formatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ static std::string __to_string_enum(const value_t& value, const std::string& fmt

template<typename value_t>
static std::string __to_string_enum(const value_t& value, const std::string& fmt, const std::locale& loc, std::false_type) {
__format_exception("to_string specialisation not found"); return {};
__format_exception(typeid(value)); return {};
}

template<typename value_t>
static std::wstring __to_string_enum(const value_t& value, const std::wstring& fmt, const std::locale& loc, std::true_type) {return __enum_formatter(fmt, value, loc);}

template<typename value_t>
static std::wstring __to_string_enum(const value_t& value, const std::wstring& fmt, const std::locale& loc, std::false_type) {
__format_exception("to_string specialisation not found"); return {};
__format_exception(typeid(value)); return {};
}
/// @endcond
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include "__character_formatter.h"
#include "__currency_formatter.h"
#include "__format_exception.h"
#include "__format_stringer.h"
#include "__natural_formatter.h"
#include "__sprintf.h"
#include <algorithm>
Expand Down
2 changes: 2 additions & 0 deletions src/xtd.core/include/xtd/internal/__format_exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
/// @endcond

#include <string>
#include "../types.h"

/// @cond
void __format_exception(const xtd::type& type);
void __format_exception(const std::string& message);
/// @endcond
6 changes: 5 additions & 1 deletion src/xtd.core/include/xtd/internal/__iformatable_formatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@
#include "../iformatable.h"

/// @cond
namespace xtd {
class object;
}

std::string __to_string_object_to_string(const xtd::object* obj);

template<typename value_t>
static std::string __to_string_polymorphic(const value_t& value, const std::string& fmt, const std::locale& loc, std::true_type) {
if (dynamic_cast<const std::exception*>(&value)) return std::string {"exception: "} + dynamic_cast<const std::exception&>(value).what();
if (dynamic_cast<const xtd::iformatable*>(&value)) return dynamic_cast<const xtd::iformatable&>(value).__opague_internal_formatable__(reinterpret_cast<intptr_t>(&fmt), reinterpret_cast<intptr_t>(&loc), 0, INTPTR_MAX);
if (dynamic_cast<const xtd::object*>(&value)) return __to_string_object_to_string(dynamic_cast<const xtd::object*>(&value));
__format_exception("to_string specialisation not found"); return {};
__format_exception(typeid(value)); return {};
}

template<typename value_t>
Expand Down
1 change: 1 addition & 0 deletions src/xtd.core/include/xtd/internal/__sprintf.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#endif
/// @endcond

#include "../types.h"
#include <cstdarg>
#include <string>

Expand Down
2 changes: 0 additions & 2 deletions src/xtd.core/include/xtd/internal/__string_formatter

This file was deleted.

17 changes: 0 additions & 17 deletions src/xtd.core/include/xtd/internal/__string_formatter.h

This file was deleted.

12 changes: 10 additions & 2 deletions src/xtd.core/include/xtd/to_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
#include "internal/__fixed_point_formatter.h"
#include "internal/__natural_formatter.h"
#include "internal/__numeric_formatter.h"
#include "internal/__string_formatter.h"
#include "internal/__format_stringer.h"
#undef __XTD_CORE_INTERNAL__
#define __XTD_STD_INTERNAL__
#include "internal/__xtd_std_version.h"
#undef __XTD_STD_INTERNAL__
/// @endcond
#include "generic_stream_output.h"
#include "register_any_stringer.h"
#include "types.h"
#include <string>
Expand Down Expand Up @@ -691,7 +692,14 @@ namespace xtd {
/// @remarks for more information about format see @ref FormatPage "Format".
template<typename type_t, typename Period>
inline std::wstring to_string(const std::chrono::duration<type_t, Period>& value, const std::wstring& fmt, const std::locale& loc) {return __duration_formatter(fmt, value, loc);}


/// @cond
template<typename char_t, typename value_t>
inline std::basic_string<char_t> __string_formatter(const std::basic_string<char_t>& fmt, value_t value, const std::locale& loc) {
return __format_stringer<char_t>(value);
}
/// @endcond

/// @brief Convert a specified value into a string with specified format and locale.
/// @par Namespace
/// xtd
Expand Down
2 changes: 0 additions & 2 deletions src/xtd.core/include/xtd/ustring.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
/// @cond
#define __XTD_CORE_INTERNAL__
#include "internal/__format_information.h"
#include "internal/__format_stringer.h"
#include "internal/__sprintf.h"
#undef __XTD_CORE_INTERNAL__
#define __XTD_STD_INTERNAL__
Expand Down Expand Up @@ -1640,7 +1639,6 @@ void __ustring_extract_format_arg(std::string& fmt, size_t& index, std::vector<_
for (auto& format : formats) {
format.location += offset;
if (format.index == index) {
//xtd::ustring arg_str = format.format.empty() ? __format_stringer<char, arg_t>(arg) : xtd::to_string(arg, format.format);
xtd::ustring arg_str = xtd::to_string(arg, format.format);

if (!format.alignment.empty()) {
Expand Down
1 change: 1 addition & 0 deletions src/xtd.core/include/xtd/xtd.core.h
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@
#include "foreground_color.h"
#include "format_exception.h"
#include "func.h"
#include "generic_stream_output.h"
#include "get_err_rdbuf.h"
#include "get_in_rdbuf.h"
#include "get_out_rdbuf.h"
Expand Down
7 changes: 6 additions & 1 deletion src/xtd.core/src/xtd/internal/__format_exception.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
#include "../../../include/xtd/internal/__format_exception.h"
#undef __XTD_CORE_INTERNAL__
#include "../../../include/xtd/format_exception.h"
#include "../../../include/xtd/typeof.h"

void __format_exception(const xtd::type& type) {
throw xtd::format_exception {"The `" + typeof_(type).full_name() + "` object does not inherit from `xtd::iformat` or the specialisation for the `" + typeof_(type).full_name() + "` object in the `xtd::to_string` specialisation method does not exist."};
}

void __format_exception(const std::string& message) {
throw xtd::format_exception(message);
throw xtd::format_exception {message};
}
2 changes: 2 additions & 0 deletions src/xtd.core/src/xtd/object.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "../../include/xtd/as.h"
#include "../../include/xtd/iformatable.h"
#include "../../include/xtd/object.h"
#include "../../include/xtd/type_object.h"
#include "../../include/xtd/ustring.h"
Expand Down Expand Up @@ -28,6 +29,7 @@ bool object::reference_equals(const object& object_a, const object& object_b) no
}

ustring object::to_string() const noexcept {
if (dynamic_cast<const iformatable*>(this)) return dynamic_cast<const iformatable*>(this)->to_string("", std::locale {});
return get_type().full_name();
}

Expand Down
1 change: 0 additions & 1 deletion src/xtd.tunit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ add_include_directories(include)
add_references(xtd.core)
add_sources(
include/xtd/tunit/__default_insert_basic_ostream_operator.h
include/xtd/tunit/__generic_stream_output.h
include/xtd/tunit/__google_test_markers.h
include/xtd/tunit/__tunit_join__items.h
include/xtd/tunit/abort_error.h
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#endif
/// @endcond

#include "__generic_stream_output.h"
#include <array>
#include <cstdint>
#include <deque>
Expand Down

0 comments on commit 6bc95c5

Please sign in to comment.