1 /*
2  * collection.c - common stuff for collection & other aggregate types.
3  *
4  *   Copyright (c) 2007-2020  Shiro Kawai  <shiro@acm.org>
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *   1. Redistributions of source code must retain the above copyright
11  *      notice, this list of conditions and the following disclaimer.
12  *
13  *   2. Redistributions in binary form must reproduce the above copyright
14  *      notice, this list of conditions and the following disclaimer in the
15  *      documentation and/or other materials provided with the distribution.
16  *
17  *   3. Neither the name of the authors nor the names of its contributors
18  *      may be used to endorse or promote products derived from this
19  *      software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27  *   TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28  *   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29  *   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31  *   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #define LIBGAUCHE_BODY
35 #include "gauche.h"
36 #include "gauche/class.h"
37 
38 /*
39  * Class stuff
40  */
41 
42 SCM_DEFINE_ABSTRACT_CLASS(Scm_CollectionClass, SCM_CLASS_DEFAULT_CPL);
43 SCM_DEFINE_ABSTRACT_CLASS(Scm_SequenceClass, SCM_CLASS_COLLECTION_CPL);
44 SCM_DEFINE_ABSTRACT_CLASS(Scm_DictionaryClass, SCM_CLASS_COLLECTION_CPL);
45 SCM_DEFINE_ABSTRACT_CLASS(Scm_OrderedDictionaryClass, Scm__OrderedDictionaryCPL+1);
46 
47 ScmClass *Scm__OrderedDictionaryCPL[] = {
48     SCM_CLASS_STATIC_PTR(Scm_OrderedDictionaryClass),
49     SCM_CLASS_STATIC_PTR(Scm_SequenceClass),
50     SCM_CLASS_STATIC_PTR(Scm_DictionaryClass),
51     SCM_CLASS_STATIC_PTR(Scm_CollectionClass),
52     SCM_CLASS_STATIC_PTR(Scm_TopClass),
53     NULL
54 };
55 
56 ScmClass *Scm__SequenceCPL[] = {
57     SCM_CLASS_STATIC_PTR(Scm_SequenceClass),
58     SCM_CLASS_STATIC_PTR(Scm_CollectionClass),
59     SCM_CLASS_STATIC_PTR(Scm_TopClass),
60     NULL
61 };
62 
63 /*
64  * Some internal utilities
65  */
66 
Scm__CheckDictValue(ScmObj val,const char * file,int line)67 intptr_t Scm__CheckDictValue(ScmObj val, const char *file, int line)
68 {
69     if (val == NULL || SCM_UNBOUNDP(val)) {
70         Scm_Panic("[internal] attempted to set an invalid ScmObj value (%p) as a value of a dictionary, at %s:%d", val, file, line);
71     }
72     return (intptr_t)val;
73 }
74 
75 /*
76  * Initialization
77  */
Scm__InitCollection(void)78 void Scm__InitCollection(void)
79 {
80     ScmModule *mod = Scm_GaucheModule();
81 
82     Scm_InitStaticClass(&Scm_CollectionClass, "<collection>",
83                         mod, NULL, 0);
84     Scm_InitStaticClass(&Scm_SequenceClass, "<sequence>",
85                         mod, NULL, 0);
86     Scm_InitStaticClass(&Scm_DictionaryClass, "<dictionary>",
87                         mod, NULL, 0);
88     Scm_InitStaticClass(&Scm_OrderedDictionaryClass, "<ordered-dictionary>",
89                         mod, NULL, 0);
90 }
91