RTI Connext Micro  Version 2.4.1.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
dds_c_string.h
Go to the documentation of this file.
1 /*
2  * FILE: dds_c_string.h - DDS string definitions
3  *
4  * (c) Copyright, Real-Time Innovations, 2012-2014.
5  *
6  * All rights reserved.
7  *
8  * No duplications, whole or partial, manual or electronic, may be made
9  * without express written permission. Any such copies, or
10  * revisions thereof, must display this notice unaltered.
11  * This code contains trade secrets of Real-Time Innovations, Inc.
12  *
13  * Modification History
14  * --------------------
15  * 15may2014,as MICRO-317 (Verocel PR#1443) Added DDS_String_ncmp
16  * 08jun2012,tk Written
17  */
18 /*ce
19  * \file
20  * \brief DDS string definitions
21  */
22 
23 /* ================================================================== */
24 /* Class: String */
25 /* ================================================================== */
26 /*e @addtogroup DDSStringClass String Support
27  @ingroup DDSInfrastructureModule
28  @brief String creation, cloning, assignment, and deletion.
29 
30  The \methods in this class ensure consistent cross-platform
31  implementations for string creation (DDS_String_alloc()), deletion
32  (DDS_String_free()), and cloning (DDS_String_dup()) that preserve the
33  mutable value type semantics. These are to be viewed as \methods that
34  define a \p string class whose data is represented by a \c 'char*'.
35 
36  @section DDSStringClass_conventions Conventions
37 
38  The following conventions govern the memory management of strings in \ndds.
39 
40  <ul>
41  <li>The \dds implementation ensures that when value types containing
42  strings are passed back and forth to the DDS APIs, the strings
43  are created/deleted/assigned/cloned using the \p string class
44  \methods.
45  <ul>
46  <li>Value types containing strings have ownership of the
47  contained string. Thus, when a value type is deleted, the
48  contained string field is also deleted.</li>
49  </ul></li>
50  <li>The user must ensure that when value types containing
51  strings are passed back and forth to the DDS APIs, the strings
52  are created/deleted/assigned/cloned using the String class \methods.
53  </li>
54  </ul>
55 
56  The representation of a string in C/C++ unfortunately does not allow
57  programs to detect how much memory has been allocated for a string. \ndds
58  must therefore make some assumptions when a user requests that a string be
59  copied into. The following rules apply when \ndds is copying into a string
60  or string sequence:
61 
62  \li If the 'char*' is NULL, \ndds will log a warning and allocate a new
63  string on behalf of the user. <i>To avoid leaking memory, you must
64  ensure that the string will be freed (see \ref DDSStringClass_usage
65  below)</i>.
66  \li If the 'char*' is not NULL, \ndds will assume that you are managing the
67  string's memory yourself and have allocated enough memory to store the
68  string to be copied. <i>\ndds will copy into your memory; to avoid
69  memory corruption, be sure to allocate enough of it.</i> Also, <i>do
70  not pass structures containing junk pointers into \ndds</i>; you are
71  likely to crash.
72 
73  @section DDSStringClass_usage Usage
74 
75  This requirement can generally be assured by adhering
76  to the following \em idiom for manipulating strings.
77 
78  \code
79  Always use
80  DDS_String_alloc() to create,
81  DDS_String_dup() to clone,
82  DDS_String_free() to delete
83  a string 'char*' that is passed back and forth between
84  user code and the DDS C/C++ APIs.
85  \endcode
86 
87  Not adhering to this idiom can result in bad pointers, and incorrect
88  memory being freed.
89 
90  In addition, the user code should be vigilant to avoid memory leaks.
91  It is good practice to:
92 
93  \li Balance occurrences of DDS_String_alloc(), DDS_String_dup(),
94  with matching occurrences of DDS_String_free() in the code.
95  \li Finalize value types containing strings. In C++ the destructor
96  accomplishes this automatically. in C, explicit "destructor" functions
97  are provided; these functions are typically called "finalize."
98 
99 */
100 
101 /* ================================================================= */
102 /*i @file
103  @ingroup String
104  @brief Defines the String facilities.
105 */
106 #ifndef dds_c_string_h
107 #define dds_c_string_h
108 
109 
110 
111 #ifndef dds_c_dll_h
112 #include "dds_c/dds_c_dll.h"
113 #endif
114 #ifndef dds_c_common_h
115 #include "dds_c/dds_c_common.h"
116 #endif
117 #ifndef dds_c_sequence_h
118 #include "dds_c/dds_c_sequence.h"
119 #endif
120 
121 #ifdef __cplusplus
122 extern "C"
123 {
124 #endif
125 
126 #define DDS_StringSeq REDA_StringSeq
127 #define DDS_StringSeq_initialize REDA_StringSeq_initialize
128 #define DDS_StringSeq_finalize REDA_StringSeq_finalize
129 #define DDS_StringSeq_get_maximum REDA_StringSeq_get_maximum
130 #define DDS_StringSeq_set_maximum REDA_StringSeq_set_maximum
131 #define DDS_StringSeq_set_length REDA_StringSeq_set_length
132 #define DDS_StringSeq_get_length REDA_StringSeq_get_length
133 #define DDS_StringSeq_get_reference REDA_StringSeq_get_reference
134 #define DDS_StringSeq_copy REDA_StringSeq_copy
135 #define DDS_StringSeq_is_equal REDA_StringSeq_is_equal
136 #define DDS_StringSeq_loan_contiguous REDA_StringSeq_loan_contiguous
137 #define DDS_StringSeq_loan_discontiguous REDA_StringSeq_loan_discontiguous
138 #define DDS_StringSeq_unloan REDA_StringSeq_unloan
139 #define DDS_StringSeq_has_ownership REDA_StringSeq_has_ownership
140 #define DDS_StringSeq_get_buffer REDA_StringSeq_get_buffer
141 #define DDS_StringSeq_set_buffer REDA_StringSeq_set_buffer
142 #define DDS_StringSeq_has_discontiguous_buffer REDA_StringSeq_has_discontiougous_buffer
143 
144 /* ----------------------------------------------------------------- */
145 /*e @ingroup DDSStringClass
146  *
147  * @brief Create a new empty string that can hold up to \p length
148  * characters.
149  *
150  * A string created by this \method must be deleted using
151  * DDS_String_free().
152  *
153  * This function will allocate enough memory to hold a string of
154  * \p length characters, \b plus one additional byte to hold the NULL
155  * terminating character.
156  *
157  * @param length \st_in Capacity of the string.
158  *
159  * @return A newly created non-NULL string upon success or
160  * NULL upon failure.
161  */
162 DDSCDllExport char*
164 
165 #define DDS_String_alloc(l_) REDA_String_alloc(l_)
166 
167 /* ----------------------------------------------------------------- */
168 /*e @ingroup DDSStringClass
169  *
170  * @brief Clone a string. Creates a new string that duplicates
171  * the value of \p string.
172  *
173  * A string created by this \method must be deleted using
174  * DDS_String_free()
175  *
176  * @param str \st_in The string to duplicate.
177  *
178  * @return If \p string == NULL, this \method always returns NULL.
179  * Otherwise, upon success it returns a newly created string
180  * whose value is \p string; upon failure it returns NULL.
181  */
182 DDSCDllExport char*
183 DDS_String_dup(const char *str);
184 
185 #define DDS_String_dup(str_) REDA_String_dup(str_)
186 
187 /* ----------------------------------------------------------------- */
188 /*i @ingroup DDSStringClass
189  *
190  * @brief Assign a string to a new value. Replaces the string
191  * pointed to by \p string_ptr, with a string whose value
192  * is \p new_value.
193  *
194  * A string created by this \method must be deleted using
195  * DDS_String_free().
196  *
197  * @pre \p string_ptr be a non-NULL pointer. *string_ptr must be
198  * either NULL, or a string created using DDS_String_alloc()
199  * or DDS_String_dup(), or DDS_String_replace().
200  *
201  * @param string_ptr \st_inout Pointer to the string to replace.
202  *
203  * @param new_value \st_in The value of the replacement string.
204  *
205  * @return If \p new_value = NULL, this \method always returns NULL.
206  * Otherwise, upon success it returns *string_ptr whose value
207  * is \p new_value; upon failure it returns NULL.
208  *
209  * @post If \p new_value = NULL, *string_ptr == NULL.
210  * Otherwise, upon success \p string_ptr contains a pointer
211  * to a string whose value is \p new_value; upon failure,
212  * \p string_ptr is left unchanged.
213  */
214 DDSCDllExport char*
215 DDS_String_replace(char **string_ptr, const char *new_value);
216 
217 #define DDS_String_replace(str_,val_) REDA_String_replace(str_,val_)
218 
219 /* ----------------------------------------------------------------- */
220 /*e @ingroup DDSStringClass
221  *
222  * @brief Delete a string.
223  *
224  * @pre \p string must be either NULL, or must have been created
225  * using DDS_String_alloc(), DDS_String_dup()
226  *
227  * @param str \st_in The string to delete.
228  * @internal String could also have been created with DDS_String_replace()
229  */
230 DDSCDllExport void
231 DDS_String_free(char *str);
232 
233 #define DDS_String_free(str_) REDA_String_free(str_)
234 
235 /* ----------------------------------------------------------------- */
236 /*e @ingroup DDSStringClass
237  *
238  * @brief Compare two strings.
239  *
240  * @pre \p s1 and s2 can be NULL or non-NULL.
241  *
242  * @param s1 \st_in String 1 to compare.
243  * @param s2 \st_in String 2 to compare.
244  *
245  * @return 0 if s1 == s2, > 0 if s1 > s2 and < 0 if s1 < s2
246  */
247 DDSCDllExport int
248 DDS_String_cmp(const char *s1, const char *s2);
249 
250 DDSCDllExport int
251 DDS_String_ncmp(const char *s1, const char *s2, DDS_UnsignedLong num);
252 
253 #define DDS_String_cmp(str1_,str2_) REDA_String_compare(str1_,str2_)
254 
255 #define DDS_String_ncmp(str1_,str2_,num_) REDA_String_ncompare(str1_,str2_,num_)
256 
257 /* ----------------------------------------------------------------- */
258 /*e @ingroup DDSStringClass
259  *
260  * @brief Return the length of a string
261  *
262  * @pre \p string must be non-NULL
263  *
264  * @param string \st_in String to return length of
265  *
266  * @return length of string. Does not include the null-termination character.
267  * If a NULL value is passed as argument, RTI_SIZE_INVALID will be returned.
268  */
269 DDSCDllExport int
270 DDS_String_length(const char *string);
271 
272 #define DDS_String_length(str1_) REDA_String_length(str1_)
273 
274 #ifdef __cplusplus
275 } /* extern "C" */
276 #endif
277 
278 
279 #endif /* dds_c_string_h */

RTI Connext Micro Version 2.4.1.0 Copyright © Thu Nov 20 2014 Real-Time Innovations, Inc