1 /*
2  * Pair-inl.h -
3  *
4  *   Copyright (c) 2008  Higepon(Taro Minowa)  <higepon@users.sourceforge.jp>
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  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
23  *   TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  *   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  *   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  *   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  *  $Id: Pair-inl.h 261 2008-07-25 06:16:44Z higepon $
30  */
31 
32 #ifndef SCHEME_PAIR_INL_
33 #define SCHEME_PAIR_INL_
34 
35 #include "scheme.h"
36 
37 namespace scheme {
38 
car()39 inline Object& Object::car() const
40 {
41     return toPair()->car;
42 }
43 
cons(Object car,Object cdr)44 inline Object Object::cons(Object car, Object cdr)
45 {
46     return Object(reinterpret_cast<intptr_t>(new(GC) Pair(car, cdr)));
47 }
48 
makeAnnoatedPair(Object car,Object cdr,Object annotation)49 inline Object Object::makeAnnoatedPair(Object car, Object cdr, Object annotation)
50 {
51     return Object(reinterpret_cast<intptr_t>(new(GC) AnnotatedPair(car, cdr, annotation)));
52 }
53 
cdr()54 inline Object& Object::cdr() const
55 {
56     return toPair()->cdr;
57 }
58 
isPair()59 inline bool Object::isPair() const
60 {
61     return isRawPointer() && ((toPair()->car.val & 0x03) != 0x03);
62 }
63 
isAnnotatedPair()64 inline bool Object::isAnnotatedPair() const
65 {
66     // if (isPair()) {
67     //     printf("GC_size((void*)val) =%d  sizeof(AnnotatedPair) =%d %d:%d", GC_size((void*)val), sizeof(AnnotatedPair), GC_size(GC_MALLOC(sizeof(struct Pair))), GC_size(new(GC) AnnotatedPair(Object::False, Object::False, Object::False)));
68     // }
69     return isPair() && GC_base((void*)val) && GC_size((void*)val) > sizeof(AnnotatedPair);
70 }
71 
first()72 inline Object& Object::first() const
73 {
74     return car();
75 }
76 
second()77 inline Object& Object::second() const
78 {
79     return cdr().car();
80 }
81 
third()82 inline Object& Object::third() const
83 {
84     return cdr().cdr().car();
85 }
86 
fourth()87 inline Object& Object::fourth() const
88 {
89     return cdr().cdr().cdr().car();
90 }
91 
fifth()92 inline Object& Object::fifth() const
93 {
94     return cdr().cdr().cdr().cdr().car();
95 }
96 
97 } // namespace scheme
98 
99 #endif // SCHEME_PAIR_INL_
100