BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bdljsn_location.h
Go to the documentation of this file.
1/// @file bdljsn_location.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdljsn_location.h -*-C++-*-
8#ifndef INCLUDED_BDLJSN_LOCATION
9#define INCLUDED_BDLJSN_LOCATION
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdljsn_location bdljsn_location
15/// @brief Provide a value-semantic type for location in a JSON document.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdljsn
19/// @{
20/// @addtogroup bdljsn_location
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdljsn_location-purpose"> Purpose</a>
25/// * <a href="#bdljsn_location-classes"> Classes </a>
26/// * <a href="#bdljsn_location-description"> Description </a>
27/// * <a href="#bdljsn_location-attributes"> Attributes </a>
28/// * <a href="#bdljsn_location-usage"> Usage </a>
29/// * <a href="#bdljsn_location-example-1-basic-syntax"> Example 1: Basic Syntax </a>
30///
31/// # Purpose {#bdljsn_location-purpose}
32/// Provide a value-semantic type for location in a JSON document.
33///
34/// # Classes {#bdljsn_location-classes}
35///
36/// - bdljsn::Location: position in a JSON document
37///
38/// @see bdljsn_jsonutil, bdljsn_json
39///
40/// # Description {#bdljsn_location-description}
41/// This component provides a single, unconstrained
42/// (value-semantic) attribute class, `bdljsn::Location`, that is used to
43/// describe a location in a (JSON) document. Location is expressed by the
44/// `offset` (attrbute) in bytes from the start of the document. See
45/// @ref bdljson_jsonutil for utilities that may provide `bdljsn::Location` values
46/// when reporting error states.
47///
48/// ## Attributes {#bdljsn_location-attributes}
49///
50///
51/// @code
52/// Name Type Default
53/// ------------------ ------------- -------
54/// offset bsl::uint64_t 0
55/// @endcode
56/// * `offset`: the offset into the JSON document
57///
58/// ## Usage {#bdljsn_location-usage}
59///
60///
61/// This section illustrates intended use of this component.
62///
63/// ### Example 1: Basic Syntax {#bdljsn_location-example-1-basic-syntax}
64///
65///
66/// This example exercises each of the methods of the `bdljsn::Location` class.
67///
68/// First, create a `bdljsn::Location` object (having the default value):
69/// @code
70/// bdljsn::Location locationA;
71/// assert(0 == locationA.offset());
72/// @endcode
73/// Then, set `locationA` to some other offset:
74/// @code
75/// locationA.setOffset(1);
76/// assert(1 == locationA.offset());
77/// @endcode
78/// Next, use the value constructor to create a second location having the same
79/// offset as the first:
80/// @code
81/// bdljsn::Location locationB(1);
82/// assert(1 == locationB.offset());
83/// assert(locationA == locationB);
84/// @endcode
85/// Then, set the second location to the maximum offset:
86/// @code
87/// const bsl::uint64_t maxOffset = bsl::numeric_limits<bsl::uint64_t>::max();
88///
89/// locationB.setOffset(maxOffset);
90/// assert(maxOffset == locationB.offset());
91/// @endcode
92/// Next, create another `Location` that is a copy of the one at `maxOffset':
93/// @code
94/// bdljsn::Location locationC(locationB);
95/// assert(locationB == locationC);
96/// @endcode
97/// Then, set the first location back to the default state:
98/// @code
99/// locationA.reset();
100/// assert(0 == locationA.offset());
101/// assert(bdljsn::Location() == locationA);
102/// @endcode
103/// Next, print the value of each:
104/// @code
105/// bsl::cout << locationA << "\n"
106/// << locationB << bsl::endl;
107///
108/// bsl::cout << "\n";
109///
110/// locationC.print(bsl::cout, 2, 3);
111/// @endcode
112/// and observe:
113/// @code
114/// 0
115/// 18446744073709551615
116///
117/// [
118/// offset = 18446744073709551615
119/// ]
120/// @endcode
121/// Finally, set each location equal to the first:
122/// @code
123/// locationC = locationB = locationA;
124/// assert(0 == locationA.offset());
125/// assert(0 == locationB.offset());
126/// assert(0 == locationC.offset());
127/// @endcode
128/// @}
129/** @} */
130/** @} */
131
132/** @addtogroup bdl
133 * @{
134 */
135/** @addtogroup bdljsn
136 * @{
137 */
138/** @addtogroup bdljsn_location
139 * @{
140 */
141
142#include <bdlscm_version.h>
143
144#include <bslalg_swaputil.h>
145
148
149#include <bslh_hash.h>
150
151#include <bsl_cstdint.h> // 'bsl::uint64_t'
152#include <bsl_iosfwd.h>
153
154
155namespace bdljsn {
156
157 // ==============
158 // class Location
159 // ==============
160
161/// This unconstrained (value-semantic) attribute class specifies a location in
162/// a (JSON) document. See the @ref bdljsn_location-attributes for information on the class attributes.
163///
164/// \note Note that the class invariants are identically the constraints
165/// on the individual attributes.
166///
167/// See @ref bdljsn_location
168class Location {
169
170 // DATA
171 bsl::uint64_t d_offset; // offset in bytes into a document
172
173 // FRIENDS
174 template <class HASHALG>
175 friend void hashAppend(HASHALG& hashAlg , const Location& );
176 friend void swap(Location& , Location& );
177
178 public:
180
181 // CREATORS
182
183 /// Create a `Location` object having the default value, 0.
184 Location();
185
186 /// Create a `Location` object having the specified `offset`.
187 explicit Location(bsl::uint64_t offset);
188
189 /// Create a `Location` object having the same value as the specified
190 /// `original` object.
191 Location(const Location& original);
192
193 ~Location() = default;
194 // Destroy this object.
195
196 // MANIPULATORS
197
198 /// Assign to this object the value of the specified `rhs` object.
199 Location& operator=(const Location& rhs);
200
201 /// Reset this object to the default value (i.e., its value upon default
202 /// construction).
203 Location& reset();
204
205 /// Set the "offset" attribute of this object to the specified `value`.
206 Location& setOffset(bsl::uint64_t value);
207
208 // Aspects
209
210 /// Efficiently exchange the value of this object with the value of the
211 /// specified `other` object. This method provides the no-throw
212 /// exception-safety guarantee.
213 void swap(Location& other);
214
215 // ACCESSORS
216
217 /// Return the `offset` attribute of this object.
218 bsl::uint64_t offset() const;
219
220 // Aspects
221
222 /// Format this object to the specified output `stream` at the optionally
223 /// specified indentation `level` and return a reference to the modifiable
224 /// `stream`. If `level` is specified, optionally specify
225 /// `spacesPerLevel`, the number of spaces per indentation level for this
226 /// and all of its nested objects. Each line is indented by the absolute
227 /// value of `level * spacesPerLevel`. If `level` is negative, suppress
228 /// indentation of the first line. If `spacesPerLevel` is negative,
229 /// suppress line breaks and format the entire output on one line. If
230 /// `stream` is initially invalid, this operation has no effect.
231 ///
232 /// \note Note that a trailing newline is provided in multiline mode only.
233 bsl::ostream& print(bsl::ostream& stream,
234 int level = 0,
235 int spacesPerLevel = 4) const;
236};
237
238// FREE OPERATORS
239
240/// Return `true` if the specified `lhs` and `rhs` attribute objects have the
241/// same value, and `false` otherwise. Two attribute objects have the same
242/// value if each respective attribute has the same value.
243inline
244bool operator==(const Location& lhs, const Location& rhs);
245
246/// Return `true` if the specified `lhs` and `rhs` attribute objects do not
247/// have the same value, and `false` otherwise. Two attribute objects do not
248/// have the same value if one or more respective attributes differ in values.
249inline
250bool operator!=(const Location& lhs, const Location& rhs);
251
252/// Write the value of the specified `object` to the specified output `stream`
253/// in a single-line format, and return a non-`const` reference to `stream`.
254/// If `stream` is not valid on entry, this operation has no effect.
255///
256/// \note Note that this human-readable format is not fully specified and can change without
257/// notice. Also note that this method has the same behavior as
258/// `object.print(stream, 0, -1)`, but with the attribute names elided.
259bsl::ostream& operator<<(bsl::ostream& stream, const Location& object);
260
261// FREE FUNCTIONS
262
263/// Pass the specified `object` to the specified `hashAlg`. This function
264/// integrates with the `bslh` modular hashing system and effectively provides
265/// a `bsl::hash` specialization for `Location`.
266template <class HASHALG>
267void hashAppend(HASHALG& hashAlg, const Location& object);
268
269/// Exchange the values of the specified `a` and `b` objects. This function
270/// provides the no-throw exception-safety guarantee.
271void swap(Location& a, Location& b);
272
273// ============================================================================
274// INLINE DEFINITIONS
275// ============================================================================
276
277 // --------------
278 // class Location
279 // --------------
280
281// CREATORS
282inline
284: d_offset(0)
285{
286}
287
288inline
289Location::Location(bsl::uint64_t offset)
290: d_offset(offset)
291{
292}
293
294inline
296: d_offset(original.d_offset)
297{
298}
299
300// MANIPULATORS
301inline
303{
304 d_offset = rhs.d_offset;
305
306 return *this;
307}
308
309inline
311{
312 d_offset = 0;
313 return *this;
314}
315
316inline
317Location& Location::setOffset(bsl::uint64_t value)
318{
319 d_offset = value;
320 return *this;
321}
322
323 // Aspects
324
325inline
327{
328 bslalg::SwapUtil::swap(&d_offset,&other.d_offset);
329}
330
331// ACCESSORS
332inline
333bsl::uint64_t Location::offset() const
334{
335 return d_offset;
336}
337
338} // close package namespace
339
340// FREE OPERATORS
341inline
343 const bdljsn::Location& rhs)
344{
345 return lhs.offset() == rhs.offset();
346}
347
348inline
350 const bdljsn::Location& rhs)
351{
352 return lhs.offset() != rhs.offset();
353}
354
355// FREE FUNCTIONS
356template <class HASHALG>
357inline
358void bdljsn::hashAppend(HASHALG& hashAlg, const Location& object)
359{
360 using ::BloombergLP::bslh::hashAppend;
361 hashAppend(hashAlg, object.d_offset);
362}
363
364inline
365void bdljsn::swap(Location& a, Location& b)
366{
367 bslalg::SwapUtil::swap(&a.d_offset, &b.d_offset);
368}
369
370
371
372#endif
373
374// ----------------------------------------------------------------------------
375// Copyright 2022 Bloomberg Finance L.P.
376//
377// Licensed under the Apache License, Version 2.0 (the "License");
378// you may not use this file except in compliance with the License.
379// You may obtain a copy of the License at
380//
381// http://www.apache.org/licenses/LICENSE-2.0
382//
383// Unless required by applicable law or agreed to in writing, software
384// distributed under the License is distributed on an "AS IS" BASIS,
385// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
386// See the License for the specific language governing permissions and
387// limitations under the License.
388// ----------------------------- END-OF-FILE ----------------------------------
389
390/** @} */
391/** @} */
392/** @} */
Definition bdljsn_location.h:168
friend void swap(Location &, Location &)
bsl::ostream & print(bsl::ostream &stream, int level=0, int spacesPerLevel=4) const
friend void hashAppend(HASHALG &hashAlg, const Location &)
bsl::uint64_t offset() const
Return the offset attribute of this object.
Definition bdljsn_location.h:333
~Location()=default
Location & setOffset(bsl::uint64_t value)
Set the "offset" attribute of this object to the specified value.
Definition bdljsn_location.h:317
Location & reset()
Definition bdljsn_location.h:310
BSLMF_NESTED_TRAIT_DECLARATION(Location, bslmf::IsBitwiseMoveable)
Location()
Create a Location object having the default value, 0.
Definition bdljsn_location.h:283
Location & operator=(const Location &rhs)
Assign to this object the value of the specified rhs object.
Definition bdljsn_location.h:302
static void swap(T *a, T *b)
Definition bslalg_swaputil.h:182
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
void hashAppend(HASH_ALGORITHM &hashAlgorithm, const BigEndianInt16 &object)
Definition bdljsn_error.h:142
bool operator!=(const Error &lhs, const Error &rhs)
void swap(Error &a, Error &b)
bool operator==(const Error &lhs, const Error &rhs)
bsl::ostream & operator<<(bsl::ostream &stream, const Error &object)
void hashAppend(HASHALG &hashAlgorithm, const Error &object)
ALLOCATOR const STRING_VIEW_LIKE_TYPE & rhs
Definition bslstl_string.h:3918
ALLOCATOR & lhs
Definition bslstl_string.h:3917
Definition bslmf_isbitwisemoveable.h:718