1 /*
2  * Copyright (c) 2009-2021, Google LLC
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 are met:
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of Google LLC nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef UPB_REFLECTION_MESSAGE_H_
29 #define UPB_REFLECTION_MESSAGE_H_
30 
31 #include "upb/collections/map.h"
32 #include "upb/reflection/common.h"
33 
34 // Must be last.
35 #include "upb/port/def.inc"
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 
41 // Returns a mutable pointer to a map, array, or submessage value. If the given
42 // arena is non-NULL this will construct a new object if it was not previously
43 // present. May not be called for primitive fields.
44 upb_MutableMessageValue upb_Message_Mutable(upb_Message* msg,
45                                             const upb_FieldDef* f,
46                                             upb_Arena* a);
47 
48 // Returns the field that is set in the oneof, or NULL if none are set.
49 const upb_FieldDef* upb_Message_WhichOneof(const upb_Message* msg,
50                                            const upb_OneofDef* o);
51 
52 // Clear all data and unknown fields.
53 void upb_Message_ClearByDef(upb_Message* msg, const upb_MessageDef* m);
54 
55 // Clears any field presence and sets the value back to its default.
56 void upb_Message_ClearFieldByDef(upb_Message* msg, const upb_FieldDef* f);
57 
58 // May only be called for fields where upb_FieldDef_HasPresence(f) == true.
59 bool upb_Message_HasFieldByDef(const upb_Message* msg, const upb_FieldDef* f);
60 
61 // Returns the value in the message associated with this field def.
62 upb_MessageValue upb_Message_GetFieldByDef(const upb_Message* msg,
63                                            const upb_FieldDef* f);
64 
65 // Sets the given field to the given value. For a msg/array/map/string, the
66 // caller must ensure that the target data outlives |msg| (by living either in
67 // the same arena or a different arena that outlives it).
68 //
69 // Returns false if allocation fails.
70 bool upb_Message_SetFieldByDef(upb_Message* msg, const upb_FieldDef* f,
71                                upb_MessageValue val, upb_Arena* a);
72 
73 // Iterate over present fields.
74 //
75 // size_t iter = kUpb_Message_Begin;
76 // const upb_FieldDef *f;
77 // upb_MessageValue val;
78 // while (upb_Message_Next(msg, m, ext_pool, &f, &val, &iter)) {
79 //   process_field(f, val);
80 // }
81 //
82 // If ext_pool is NULL, no extensions will be returned.  If the given symtab
83 // returns extensions that don't match what is in this message, those extensions
84 // will be skipped.
85 
86 #define kUpb_Message_Begin -1
87 
88 bool upb_Message_Next(const upb_Message* msg, const upb_MessageDef* m,
89                       const upb_DefPool* ext_pool, const upb_FieldDef** f,
90                       upb_MessageValue* val, size_t* iter);
91 
92 // Clears all unknown field data from this message and all submessages.
93 bool upb_Message_DiscardUnknown(upb_Message* msg, const upb_MessageDef* m,
94                                 int maxdepth);
95 
96 #ifdef __cplusplus
97 } /* extern "C" */
98 #endif
99 
100 #include "upb/port/undef.inc"
101 
102 #endif /* UPB_REFLECTION_MESSAGE_H_ */
103