Merge pull request #10 from gunyarakun/fix-invalid-return
[cocotron.git] / objc / Protocol.m
blob7d15ea8b4603a82026836ef032557e62a03c378f
1 /* Copyright (c) 2006-2007 Christopher J. W. Lloyd
3 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
8 #import <objc/Protocol.h>
9 #import "objc_protocol.h"
11 #import "ObjCHashTable.h"
12 #import "objc_class.h"
13 #import "objc_sel.h"
15 #define PROTOCOL_CLASS "Protocol"
17 static SEL OBJCRegisterMethodDescription(struct objc_method_description *method) {
18    return sel_registerNameNoCopy((const char *)method->name);
21 static void OBJCRegisterMethodList(OBJCMethodDescriptionList *list){
22    unsigned i;
24    for (i=0;i<list->count;i++)
25     list->list[i].name=OBJCRegisterMethodDescription(list->list+i);
28 void OBJCRegisterProtocol(OBJCProtocolTemplate *template) {
29    unsigned          i;
30    struct objc_protocol_list *subprotos;
31    Class             class=objc_lookUpClass(PROTOCOL_CLASS);
33    if(template->isa==class)
34     return; // already registered
36    template->isa=class;
38    if(template->instanceMethods!=NULL)
39     OBJCRegisterMethodList(template->instanceMethods);
41    if(template->classMethods!=NULL)
42     OBJCRegisterMethodList(template->classMethods);
43    for(subprotos=template->childProtocols;subprotos!=NULL;subprotos=subprotos->next){
44     for (i=0;i<subprotos->count;i++)
45      OBJCRegisterProtocol((OBJCProtocolTemplate *)subprotos->list[i]);
46    }
49 @implementation Protocol
51 -(const char *)name {
52     return nameCString;
55 -(struct objc_method_description *)descriptionForInstanceMethod:(SEL)selector {
56    struct objc_protocol_list *list;
57    unsigned          i;
59    if(instanceMethods!=NULL)
60     for(i=0;i<instanceMethods->count;i++)
61      if(instanceMethods->list[i].name==sel_getSelector(selector))
62       return &instanceMethods->list[i];
63     
64    list=childProtocols;
65    while(list!=NULL){
66     unsigned j;
68     for(j=0;j<list->count;j++){
69      unsigned k;
71      for(k=0;k<list->list[j]->instanceMethods->count;k++)
72       if(list->list[j]->instanceMethods->list[k].name==sel_getSelector(selector))
73        return &list->list[j]->instanceMethods->list[k];
74     }
75     list=list->next;
76    }
78    return NULL;
81 -(struct objc_method_description *)descriptionForClassMethod:(SEL)selector {
82    struct objc_protocol_list *list;
83    unsigned          i;
85    if(classMethods!=NULL)
86     for(i=0;i<classMethods->count;i++)
87      if(classMethods->list[i].name==sel_getSelector(selector))
88       return &classMethods->list[i];
90    list=childProtocols;
91    while(list!=NULL){
92     unsigned j;
94     for(j=0;j<list->count;j++) {
95      unsigned k;
97      for (k=0;k<list->list[j]->classMethods->count;k++)
98       if(list->list[j]->classMethods->list[k].name==sel_getSelector(selector))
99        return &list->list[j]->classMethods->list[k];
100     }
101     list=list->next;
102    }
104    return NULL;
107 -(BOOL)conformsTo:(Protocol *)other {
109    if(other==nil)
110     return NO;
112    if(strcmp(other->nameCString,nameCString)==0)
113     return YES;
114    else if(childProtocols==NULL)
115     return NO;
116    else {
117     int i;
119     for (i=0;i<childProtocols->count;i++) {
120      Protocol *proto=childProtocols->list[i];
122      if (strcmp(other->nameCString,proto->nameCString) == 0)
123       return YES;
125      if ([proto conformsTo:other])
126       return YES;
127     }
128     return NO;
129    }
132 @end