File: function.hpp

package info (click to toggle)
camp 0.8.4-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,748 kB
  • sloc: cpp: 8,154; makefile: 17
file content (184 lines) | stat: -rw-r--r-- 6,572 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/****************************************************************************
**
** This file is part of the CAMP library.
**
** The MIT License (MIT)
**
** Copyright (C) 2009-2014 TEGESO/TEGESOFT and/or its subsidiary(-ies) and mother company.
** Contact: Tegesoft Information (contact@tegesoft.com)
**
** 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:
** 
** The above copyright notice and this permission notice shall be included in
** all copies or substantial portions of the Software.
** 
** 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.
**
****************************************************************************/

#ifndef CAMPTEST_FUNCTION_HPP
#define CAMPTEST_FUNCTION_HPP

#include <camp/camptype.hpp>
#include <camp/enum.hpp>
#include <camp/class.hpp>
#include <camp/value.hpp>
#include <boost/shared_ptr.hpp>
#include <string>

namespace FunctionTest
{
    enum MyEnum
    {
        Zero = 0,
        One  = 1,
        Two  = 2
    };

    struct MyType
    {
        MyType(int x_) : x(x_) {}
        int x;
    };

    bool operator==(const MyType& left, const MyType& right) {return left.x == right.x;}
    bool operator<(const MyType& left, const MyType& right) {return left.x < right.x;}
    std::ostream& operator<<(std::ostream& stream, const MyType& object) {return stream << object.x;}

    struct MyBase
    {
        void f6() {}
        char padding[10];
    };

    struct MyClass : MyBase
    {
        MyClass()
            : p1(true)
            , p2(2)
            , p3("3")
            , p4(MyType(4))
            , p5(MyType(5))
            , innerPtr(&inner)
            , innerSmartPtr(new Inner)
        {
        }

        bool p1;
        int p2;
        std::string p3;

        MyType p4; const MyType& f4() {return p4;}
        MyType p5; const MyType& f5() const {return p5;}
        // f6 is inherited
        camp::Value f7(camp::Value v) {return v;}

        void f8() {}
        void f9(bool) {}
        void f10(float, double) {}
        void f11(short, int, long) {}
        void f12(const std::string&, std::string, const std::string&, std::string) {}
        void f13(MyEnum, MyEnum, MyEnum, MyEnum, MyEnum) {}

        struct Inner
        {
            void f14() {}
            void f15() const {}
            int f16() {return 16;}
            void f17(int) {}
            void f18() {}
            void f19() {}
        };
        Inner inner;
        const Inner& getInner() const {return inner;}
        Inner* innerPtr;
        const Inner* getInnerPtr() const {return innerPtr;}
        boost::shared_ptr<Inner> innerSmartPtr;
        const boost::shared_ptr<Inner> getInnerSmartPtr() {return innerSmartPtr;}

        int f20(int x) {return x;}
        int f21(int x, int y) {return x + y;}
        int f22(int x, int y, int z) {return x + y + z;}
    };

    void f1(MyClass& object)
    {
        object.p1 = true;
    }

    int f2(MyClass object, int x)
    {
        return object.p2 + x;
    }

    const std::string& f3(const MyClass* object)
    {
        return object->p3;
    }

    void declare()
    {
        camp::Enum::declare<MyEnum>("FunctionTest::MyEnum")
            .value("Zero", Zero)
            .value("One",  One)
            .value("Two",  Two);

        camp::Class::declare<MyType>("FunctionTest::MyType");

        camp::Class::declare<MyBase>("FunctionTest::MyBase");

        camp::Class::declare<MyClass>("FunctionTest::MyClass")
            .base<MyBase>()

            // ***** non-member functions *****
            .function("f1", &f1) // object by reference
            .function("f2", &f2) // object by value + parameter
            .function("f3", &f3) // object by pointer

            // ***** member functions *****
            .function("f4", &MyClass::f4) // non-const
            .function("f5", &MyClass::f5) // const
            .function("f6", &MyClass::f6) // inherited
            .function("f7", &MyClass::f7) // camp::Value as return and argument types

            // ***** arguments count ******
            .function("f8",  &MyClass::f8)  // 0 argument
            .function("f9",  &MyClass::f9)  // 1 argument
            .function("f10", &MyClass::f10) // 2 arguments
            .function("f11", &MyClass::f11) // 3 arguments
            .function("f12", &MyClass::f12) // 4 arguments
            .function("f13", &MyClass::f13) // 5 arguments

            // ***** nested functions *****
            // TOFIX .function("f14", &MyClass::Inner::f14, &MyClass::inner)            // object
            .function("f15", &MyClass::Inner::f15, &MyClass::getInner)         // getter returning an object
            .function("f16", &MyClass::Inner::f16, &MyClass::innerPtr)         // raw pointer
            // TOFIX .function("f17", &MyClass::Inner::f17, &MyClass::getInnerPtr)      // getter returning a raw pointer
            .function("f18", &MyClass::Inner::f18, &MyClass::innerSmartPtr)    // smart pointer
            .function("f19", &MyClass::Inner::f19, &MyClass::getInnerSmartPtr) // getter returning a smart pointer

            // ***** boost::function *****
            .function("f20", boost::function<int (MyClass&, int)>(boost::bind(&MyClass::f20, _1, _2)))
            .function("f21", boost::function<int (MyClass&, int)>(boost::bind(&MyClass::f21, _1, _2, 20)))
            .function("f22", boost::function<int (MyClass&, int)>(boost::bind(boost::bind(&MyClass::f22, _1, _2, _3, 30), _1, _2, 20)))
            ;
    }
}

CAMP_AUTO_TYPE(FunctionTest::MyEnum,  &FunctionTest::declare)
CAMP_AUTO_TYPE(FunctionTest::MyType,  &FunctionTest::declare)
CAMP_AUTO_TYPE(FunctionTest::MyClass, &FunctionTest::declare)
CAMP_AUTO_TYPE(FunctionTest::MyBase,  &FunctionTest::declare)

#endif // CAMPTEST_FUNCTION_HPP