Stroika Library 3.0d18
 
Loading...
Searching...
No Matches
WebService/Sources/Model.cpp
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#include "Stroika/Frameworks/StroikaPreComp.h"
5
6#include "Stroika/Foundation/Characters/FloatConversion.h"
9#include "Stroika/Foundation/Containers/Collection.h"
10#include "Stroika/Foundation/Containers/Set.h"
11
12#include "Model.h"
13
14using namespace std;
15
16using namespace Stroika::Foundation;
20
21using namespace StroikaSample::WebServices;
22using namespace StroikaSample::WebServices::Model;
23
24const ObjectVariantMapper StroikaSample::WebServices::Model::kMapper = [] () {
26
27 // Read/Write real/complex numbers
28 mapper.Add<Number> (
29 [] (const ObjectVariantMapper& mapper [[maybe_unused]], const Number* obj) -> VariantValue {
30 static const FloatConversion::ToStringOptions kFloat2StringOptions_{};
32 if (obj->real () != 0) {
33 if (obj->imag () == 0) {
34 return obj->real (); // return a number in this case, not a string
35 }
36 sb << FloatConversion::ToString (obj->real (), kFloat2StringOptions_);
37 }
38 if (obj->imag () != 0) {
39 if (not sb.empty ()) {
40 sb << ' ';
41 }
42 if (obj->imag () > 0 and sb.length () > 1) {
43 sb << "+ "sv;
44 }
45 if (obj->imag () == 1) {
46 // skip
47 }
48 else if (obj->imag () == -1) {
49 sb << "- "sv;
50 }
51 else {
52 sb << FloatConversion::ToString (obj->imag (), kFloat2StringOptions_);
53 }
54 sb << "i"sv;
55 }
56 if (sb.empty ()) {
57 return "0"_k;
58 }
59 return sb.str ();
60 },
61 [] (const ObjectVariantMapper& mapper [[maybe_unused]], const VariantValue& vv, Number* intoObj) -> void {
62 // Parse complex numbers of the form a + bi, handling special cases of a, and bi.
63 // Trick: parse one number, and then accumulate second number (if any)
64 // Works with a few minor rewrites
65 String remainingNumber2Parse = vv.As<String> ();
66 remainingNumber2Parse = remainingNumber2Parse.ReplaceAll (" "_k, String{}); // strip spaces - not needed and cause 2 + 4i to not parse (where 2+4i would) with simple trick we use (parse numbers and accum)
67 Number accum{};
68 for (unsigned int cnt = 0; cnt < 2 and not remainingNumber2Parse.empty (); ++cnt) {
69 // special case rewrite bare 'i' as '1i' with +/- cases
70 if (remainingNumber2Parse.StartsWith ("i"_k)) {
71 remainingNumber2Parse = "1i"_k + remainingNumber2Parse.Skip (1);
72 }
73 else if (remainingNumber2Parse.StartsWith ("+i"_k)) {
74 remainingNumber2Parse = "+1i"_k + remainingNumber2Parse.Skip (2);
75 }
76 else if (remainingNumber2Parse.StartsWith ("-i"_k)) {
77 remainingNumber2Parse = "-1i"_k + remainingNumber2Parse.Skip (2);
78 }
79 Number::value_type d = Characters::FloatConversion::ToFloat<Number::value_type> (remainingNumber2Parse, &remainingNumber2Parse);
80 if (remainingNumber2Parse.StartsWith ("i"_k)) {
81 accum += Number{0, d};
82 remainingNumber2Parse = remainingNumber2Parse.Skip (1);
83 }
84 else {
85 accum += Number{d};
86 }
87 }
88 if (not remainingNumber2Parse.empty ()) {
89 Execution::Throw (Execution::Exception{"invalid complex number: "sv + vv.As<String> ()});
90 }
91 *intoObj = accum;
92 });
93
95
96 return mapper;
97}();
Similar to String, but intended to more efficiently construct a String. Mutable type (String is large...
nonvirtual size_t length() const noexcept
number of characters, not bytes or code-points
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
nonvirtual String ReplaceAll(const RegularExpression &regEx, const String &with) const
Definition String.cpp:1155
nonvirtual bool StartsWith(const Character &c, CompareOptions co=eWithCase) const
Definition String.cpp:1059
nonvirtual String Skip(size_t n) const
Return a substring of this string, starting at 'argument' n. If n > size(), return empty string.
Definition String.inl:604
A Collection<T> is a container to manage an un-ordered collection of items, without equality defined ...
ObjectVariantMapper can be used to map C++ types to and from variant-union types, which can be transp...
nonvirtual void AddCommonType(ARGS &&... args)
Simple variant-value (case variant union) object, with (variant) basic types analogous to a value in ...
Exception<> is a replacement (subclass) for any std c++ exception class (e.g. the default 'std::excep...
Definition Exceptions.h:157
STRING_TYPE ToString(FLOAT_TYPE f, const ToStringOptions &options={})
STL namespace.