source: mainline/uspace/lib/cpp/include/internal/locale.hpp@ 349b0f7

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 349b0f7 was c729a60, checked in by Dzejrou <dzejrou@…>, 7 years ago

cpp: added basic implementation of num_put, fixed minor errors and added undefined functions

  • Property mode set to 100644
File size: 5.5 KB
Line 
1/*
2 * Copyright (c) 2017 Jaroslav Jindrak
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#ifndef LIBCPP_INTERNAL_LOCALE
30#define LIBCPP_INTERNAL_LOCALE
31
32#include <impl/string.hpp>
33
34namespace std
35{
36 class locale;
37
38 template<class Facet>
39 const Facet& use_facet(const locale& loc);
40
41 template<class Facet>
42 bool has_facet(const locale& loc);
43
44 /**
45 * 22.3.1, class locale:
46 */
47
48 class locale
49 {
50 public:
51 class facet
52 {
53 protected:
54 explicit facet(size_t refs = 0);
55
56 virtual ~facet();
57
58 facet(const facet&) = delete;
59 void operator=(const facet&) = delete;
60 };
61
62 class id
63 {
64 public:
65 id() = default;
66
67 id(const id&) = delete;
68 void operator=(const id&) = delete;
69 };
70
71 using category = int;
72
73 static const category none = 0b000'0001;
74 static const category collate = 0b000'0010;
75 static const category ctype = 0b000'0100;
76 static const category monetary = 0b000'1000;
77 static const category numeric = 0b001'0000;
78 static const category time = 0b010'0000;
79 static const category messages = 0b100'0000;
80 static const category all = collate | ctype | monetary |
81 numeric | time | messages;
82
83 locale() noexcept;
84
85 locale(const locale& other) noexcept;
86
87 explicit locale(const char* name);
88
89 explicit locale(const string& name);
90
91 locale(const locale& other, const char* name, category);
92
93 locale(const locale& other, const string& name, category);
94
95 template<class Facet>
96 locale(const locale& other, Facet* f)
97 : name_{other.name_}
98 { /* DUMMY BODY */ }
99
100 locale(const locale& other, const locale& one, category);
101
102 ~locale() = default;
103
104 const locale& operator=(const locale& other) noexcept;
105
106 template<class Facet>
107 locale combine(const locale& other) const
108 {
109 return other;
110 }
111
112 string name() const;
113
114 bool operator==(const locale& other) const;
115 bool operator!=(const locale& other) const;
116
117 template<class Char, class Traits, class Allocator>
118 bool operator()(const basic_string<Char, Traits, Allocator>& s1,
119 const basic_string<Char, Traits, Allocator>& s2) const
120 {
121 // TODO: define outside locale
122 /* return use_facet<collate<Char>>(*this).compare( */
123 /* s1.begin(), s1.end(), s2.begin(), s2.end() */
124 /* ) < 0; */
125 return false;
126 }
127
128 static locale global(const locale&)
129 {
130 return *the_locale_;
131 }
132
133 static const locale& classic()
134 {
135 return *the_locale_;
136 }
137
138 private:
139 string name_;
140
141 // TODO: implement the_locale_
142 static constexpr locale* the_locale_{nullptr};
143
144 template<class Facet>
145 friend bool has_facet(const locale&);
146
147 template<class Facet>
148 bool has_()
149 { // Our single locale atm has all facets.
150 return true;
151 }
152
153 template<class Facet>
154 friend const Facet& use_facet(const locale&);
155
156 template<class Facet>
157 const Facet& get_() const
158 {
159 // TODO: A VERY ugly hack, when we have map/shared ptr,
160 // we should implement facets and store them in a map
161 // and return them here by their IDs.
162 return *(new Facet{0u});
163 }
164 };
165
166 template<class Facet>
167 const Facet& use_facet(const locale& loc)
168 {
169 return loc.get_<Facet>();
170 }
171
172 template<class Facet>
173 bool has_facet(const locale& loc)
174 {
175 return loc.has_<Facet>();
176 }
177}
178
179#endif
Note: See TracBrowser for help on using the repository browser.