BDE 4.14.0 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/// {`jsonutil`} for utilities that may provide `bdljsn::Location` values when
46/// 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
162/// in a (JSON) document. See the {Attributes} section under {DESCRIPTION}
163/// in the component-level documentation for information on the class
164/// attributes. Note that the class invariants are identically the
165/// constraints 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
223 /// optionally specified indentation `level` and return a reference to
224 /// the modifiable `stream`. If `level` is specified, optionally
225 /// specify `spacesPerLevel`, the number of spaces per indentation level
226 /// for this and all of its nested objects. Each line is indented by
227 /// the absolute value of `level * spacesPerLevel`. If `level` is
228 /// negative, suppress indentation of the first line. If
229 /// `spacesPerLevel` is negative, suppress line breaks and format the
230 /// entire output on one line. If `stream` is initially invalid, this
231 /// operation has no effect. Note that a trailing newline is provided
232 /// 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
241/// the same value, and `false` otherwise. Two attribute objects have the
242/// same 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
248/// not have the same value if one or more respective attributes differ in
249/// values.
250inline
251bool operator!=(const Location& lhs, const Location& rhs);
252
253/// Write the value of the specified `object` to the specified output
254/// `stream` in a single-line format, and return a non-`const` reference to
255/// `stream`. If `stream` is not valid on entry, this operation has no
256/// effect. Note that this human-readable format is not fully specified and
257/// can change without notice. Also note that this method has the same
258/// behavior as `object.print(stream, 0, -1)`, but with the attribute names
259/// elided.
260bsl::ostream& operator<<(bsl::ostream& stream, const Location& object);
261
262// FREE FUNCTIONS
263
264/// Pass the specified `object` to the specified `hashAlg`. This function
265/// integrates with the `bslh` modular hashing system and effectively
266/// provides a `bsl::hash` specialization for `Location`.
267template <class HASHALG>
268void hashAppend(HASHALG& hashAlg, const Location& object);
269
270/// Exchange the values of the specified `a` and `b` objects. This function
271/// provides the no-throw exception-safety guarantee.
272void swap(Location& a, Location& b);
273
274// ============================================================================
275// INLINE DEFINITIONS
276// ============================================================================
277
278 // --------------
279 // class Location
280 // --------------
281
282// CREATORS
283inline
285: d_offset(0)
286{
287}
288
289inline
290Location::Location(bsl::uint64_t offset)
291: d_offset(offset)
292{
293}
294
295inline
297: d_offset(original.d_offset)
298{
299}
300
301// MANIPULATORS
302inline
304{
305 d_offset = rhs.d_offset;
306
307 return *this;
308}
309
310inline
312{
313 d_offset = 0;
314 return *this;
315}
316
317inline
318Location& Location::setOffset(bsl::uint64_t value)
319{
320 d_offset = value;
321 return *this;
322}
323
324 // Aspects
325
326inline
328{
329 bslalg::SwapUtil::swap(&d_offset,&other.d_offset);
330}
331
332// ACCESSORS
333inline
334bsl::uint64_t Location::offset() const
335{
336 return d_offset;
337}
338
339} // close package namespace
340
341// FREE OPERATORS
342inline
344 const bdljsn::Location& rhs)
345{
346 return lhs.offset() == rhs.offset();
347}
348
349inline
351 const bdljsn::Location& rhs)
352{
353 return lhs.offset() != rhs.offset();
354}
355
356// FREE FUNCTIONS
357template <class HASHALG>
358inline
359void bdljsn::hashAppend(HASHALG& hashAlg, const Location& object)
360{
361 using ::BloombergLP::bslh::hashAppend;
362 hashAppend(hashAlg, object.d_offset);
363}
364
365inline
366void bdljsn::swap(Location& a, Location& b)
367{
368 bslalg::SwapUtil::swap(&a.d_offset, &b.d_offset);
369}
370
371
372
373#endif
374
375// ----------------------------------------------------------------------------
376// Copyright 2022 Bloomberg Finance L.P.
377//
378// Licensed under the Apache License, Version 2.0 (the "License");
379// you may not use this file except in compliance with the License.
380// You may obtain a copy of the License at
381//
382// http://www.apache.org/licenses/LICENSE-2.0
383//
384// Unless required by applicable law or agreed to in writing, software
385// distributed under the License is distributed on an "AS IS" BASIS,
386// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
387// See the License for the specific language governing permissions and
388// limitations under the License.
389// ----------------------------- END-OF-FILE ----------------------------------
390
391/** @} */
392/** @} */
393/** @} */
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:334
~Location()=default
Location & setOffset(bsl::uint64_t value)
Set the "offset" attribute of this object to the specified value.
Definition bdljsn_location.h:318
Location & reset()
Definition bdljsn_location.h:311
BSLMF_NESTED_TRAIT_DECLARATION(Location, bslmf::IsBitwiseMoveable)
Location()
Create a Location object having the default value, 0.
Definition bdljsn_location.h:284
Location & operator=(const Location &rhs)
Assign to this object the value of the specified rhs object.
Definition bdljsn_location.h:303
static void swap(T *a, T *b)
Definition bslalg_swaputil.h:194
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
void hashAppend(HASH_ALGORITHM &hashAlg, const baljsn::EncoderTestAddress &object)
Definition baljsn_encoder_testtypes.h:9236
Definition bdljsn_error.h:143
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)
Definition bslmf_isbitwisemoveable.h:718