2  * Copyright (c) 2019 Apple Inc. All rights reserved. 
   4  * Licensed under the Apache License, Version 2.0 (the "License"); 
   5  * you may not use this file except in compliance with the License. 
   6  * You may obtain a copy of the License at 
   8  *     https://www.apache.org/licenses/LICENSE-2.0 
  10  * Unless required by applicable law or agreed to in writing, software 
  11  * distributed under the License is distributed on an "AS IS" BASIS, 
  12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  13  * See the License for the specific language governing permissions and 
  14  * limitations under the License. 
  17 #include "mdns_internal.h" 
  18 #include "mdns_object.h" 
  20 #include "mdns_objects.h" 
  22 #include <CoreUtils/CoreUtils.h> 
  24 //====================================================================================================================== 
  25 // MARK: - Object Kind Definition 
  28 _mdns_object_copy_description(mdns_object_t object
, bool debug
, bool privacy
); 
  30 const struct mdns_kind_s _mdns_object_kind 
= { 
  31         NULL
,           // No superkind. 
  33         _mdns_object_copy_description
, 
  34         NULL
,           // No equal method. 
  35         NULL            
// No finalize method. 
  39 _mdns_cf_collection_callback_retain(CFAllocatorRef allocator
, const void *object
); 
  42 _mdns_cf_collection_callback_release(CFAllocatorRef allocator
, const void *object
); 
  45 _mdns_cf_collection_callback_copy_description(const void *object
); 
  48 _mdns_cf_collection_callback_equal(const void *object1
, const void *object2
); 
  50 const CFArrayCallBacks mdns_cfarray_callbacks 
= { 
  52         _mdns_cf_collection_callback_retain
,                    // retain 
  53         _mdns_cf_collection_callback_release
,                   // release 
  54         _mdns_cf_collection_callback_copy_description
,  // copy description 
  55         _mdns_cf_collection_callback_equal                              
// equal 
  58 //====================================================================================================================== 
  59 // MARK: - Object Public Methods 
  62 mdns_retain(mdns_object_t me
) 
  67 //====================================================================================================================== 
  70 mdns_release(mdns_object_t me
) 
  75 //====================================================================================================================== 
  78 mdns_copy_description(mdns_object_t me
) 
  80         return mdns_object_copy_description(me
, false, false); 
  83 //====================================================================================================================== 
  86 mdns_equal(mdns_object_t me
, mdns_object_t other
) 
  91         if (me
->kind 
!= other
->kind
) { 
  94         if (me
->kind
->equal
) { 
  95                 return me
->kind
->equal(me
, other
); 
 100 //====================================================================================================================== 
 101 // MARK: - Object Private Methods 
 104 _mdns_object_copy_description(mdns_object_t me
, __unused 
bool debug
, __unused 
bool privacy
) 
 106         char *description 
= NULL
; 
 107         asprintf(&description
, "<%s: %p>", me
->kind
->name
, (void *)me
); 
 111 //====================================================================================================================== 
 114 mdns_object_copy_description(mdns_object_t me
, bool debug
, bool privacy
) 
 116         for (mdns_kind_t kind 
= me
->kind
; kind
; kind 
= kind
->superkind
) { 
 117                 if (kind
->copy_description
) { 
 118                         return kind
->copy_description(me
, debug
, privacy
); 
 124 //====================================================================================================================== 
 127 mdns_object_copy_description_as_cfstring_ex(mdns_object_t me
, bool debug
, bool privacy
) 
 129         CFStringRef description 
= NULL
; 
 130         char *cstring 
= mdns_object_copy_description(me
, debug
, privacy
); 
 131         require_quiet(cstring
, exit
); 
 133         description 
= CFStringCreateWithCStringNoCopy(NULL
, cstring
, kCFStringEncodingUTF8
, kCFAllocatorMalloc
); 
 134         require_quiet(description
, exit
); 
 138         FreeNullSafe(cstring
); 
 142 //====================================================================================================================== 
 145 mdns_object_finalize(mdns_object_t me
) 
 147         for (mdns_kind_t kind 
= me
->kind
; kind
; kind 
= kind
->superkind
) { 
 148                 if (kind
->finalize
) { 
 154 //====================================================================================================================== 
 157 _mdns_cf_collection_callback_retain(__unused CFAllocatorRef allocator
, const void *obj
) 
 159         mdns_retain((mdns_object_t
)obj
); 
 163 //====================================================================================================================== 
 166 _mdns_cf_collection_callback_release(__unused CFAllocatorRef allocator
, const void *obj
) 
 168         mdns_release((mdns_object_t
)obj
); 
 171 //====================================================================================================================== 
 174 _mdns_cf_collection_callback_copy_description(const void *obj
) 
 176         return mdns_object_copy_description_as_cfstring((mdns_object_t
)obj
); 
 179 //====================================================================================================================== 
 182 _mdns_cf_collection_callback_equal(const void *object1
, const void *object2
) 
 184         return mdns_equal((mdns_object_t
)object1
, (mdns_object_t
)object2
);