summaryrefslogtreecommitdiffstats
path: root/libccnx-common/ccnx/common/ccnx_NameSegmentNumber.h
blob: a685c7139cea59a9a63eb8aa82c1a2f0ceb15499 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @file ccnx_NameSegmentNumber.h
 * @ingroup Naming
 * @brief Utilities for creating and parsing CCNxNameSegments that contain an integer value.
 *
 * CCNxNameSegments may optionally contain an integer value. This file contains some utilities
 * for creating and parsing CCNxNameSegments with integer values.
 * See {@link CCNxNameSegment}, {@link CCNxNameLabelType}, and {@link CCNxName} for more information.
 *
 */
#ifndef libccnx_ccnx_NameSegmentNumber_h
#define libccnx_ccnx_NameSegmentNumber_h

#include <stdbool.h>

#include <ccnx/common/ccnx_NameSegment.h>
#include <ccnx/common/ccnx_NameLabel.h>

/**
 * Create a new {@link CCNxNameSegment} consisting of a type and integer value.
 *
 * The newly created instance must eventually be released by calling {@link ccnxNameSegment_Release}().
 *
 * @param [in] type A valid {@link CCNxNameLabelType}.
 * @param [in] value The integer value to assign to the `CCNxNameSegment`.
 * @return A pointer to a new `CCNxNameSegment` instance, consisting of a type and integer value.
 *
 * Example:
 * @code
 * {
 *     uint64_t expected = 0x123456789ABCDEF0;
 *     CCNxNameSegment *segment = ccnxNameSegmentNumber_Create(CCNxNameLabelType_CHUNK, expected);
 *
 *     uint64_t actual = ccnxNameSegmentNumber_Value(segment);
 *
 *     assertTrue(expected == actual, "Expected 0x%" PRIX64 " actual 0x%" PRIX64 "", expected, actual);
 *
 *     ccnxNameSegment_Release(&segment);
 * }
 * @endcode
 *
 * @see {@link ccnxNameSegment_Release}
 * @see `CCNxNameLabelType`
 */
CCNxNameSegment *ccnxNameSegmentNumber_Create(CCNxNameLabelType type, uint64_t value);

/**
 * Decode an integer value from a {@link CCNxNameSegment}.
 *
 * Given a `CCNxNameSegment` with a numeric type, return the integer value associated with the `CCNxNameSegment`.
 *
 * @param [in] nameSegment A pointer a `CCNxNameSegment` instance containing an integer value.
 * @return The integer value of the `CCNxNameSegment`.
 *
 * Example:
 * @code
 * {
 *     uint64_t expected = 0x123456789ABCDEF0;
 *     CCNxNameSegment *segment = ccnxNameSegmentNumber_Create(CCNxNameLabelType_CHUNK, expected);
 *
 *     uint64_t actual = ccnxNameSegmentNumber_Value(segment);
 *
 *     assertTrue(expected == actual, "Expected 0x%" PRIX64 " actual 0x%" PRIX64 "", expected, actual);
 *
 *     ccnxNameSegment_Release(&segment);
 * }
 * @endcode
 */
uint64_t ccnxNameSegmentNumber_Value(const CCNxNameSegment *nameSegment);

/**
 * Determine if the given CCNxNameSegment value represents a valid encoded number.
 *
 * A valid encoded number contains at least 1 byte and nor more than 8 bytes.
 *
 * @param [in] nameSegment A pointer to a CCNxNameSegment instance.
 *
 * Example:
 * @code
 * {
 *     uint64_t expected = 0x123456789ABCDEF0;
 *     CCNxNameSegment *segment = ccnxNameSegmentNumber_Create(CCNxNameLabelType_CHUNK, expected);
 *
 *     ccnxNameSegmentNumber_AssertValid(segment);
 * }
 * @endcode
 */
bool ccnxNameSegmentNumber_IsValid(const CCNxNameSegment *nameSegment);

/**
 * Assert that the given CCNxNameSegment value represents a valid encoded number.
 *
 * @param [in] nameSegment A pointer to a CCNxNameSegment instance.
 *
 * Example:
 * @code
 * {
 *     uint64_t expected = 0x123456789ABCDEF0;
 *     CCNxNameSegment *segment = ccnxNameSegmentNumber_Create(CCNxNameLabelType_CHUNK, expected);
 *
 *     ccnxNameSegmentNumber_AssertValid(segment);
 * }
 * @endcode
 */
void ccnxNameSegmentNumber_AssertValid(const CCNxNameSegment *nameSegment);
#endif // libccnx_ccnx_NameSegmentNumber_h