source: cpp/frams/vm/classes/collectionobj.h @ 906

Last change on this file since 906 was 868, checked in by Maciej Komosinski, 6 years ago

Added two methods for dictionary iteration: for(x in dict) and for(x in dict.keys)

  • Property svn:eol-style set to native
File size: 3.8 KB
Line 
1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
2// Copyright (C) 1999-2019  Maciej Komosinski and Szymon Ulatowski.
3// See LICENSE.txt for details.
4
5#ifndef _COLLECTIONOBJ_H_
6#define _COLLECTIONOBJ_H_
7
8#include <frams/param/param.h>
9#include <frams/util/extvalue.h>
10#include <frams/util/hashtable.h>
11#include <frams/util/3d.h>
12
13/** object collection, indexed by int */
14class VectorObject : public DestrBase
15{
16public:
17        SList data;
18        unsigned int readonly : 1, owndata : 1;
19        void clear();
20        ExtValue *get(int i) { return (ExtValue*)data.get(i); }
21        void set_or_insert(int i, const ExtValue& val, bool insert);
22
23        static Param par;
24        VectorObject(Pt3D& pt);
25        VectorObject() :readonly(0), owndata(1) {}
26        ~VectorObject() { clear(); }
27        static Param& getStaticParam() { return par; }
28#define STATRICKCLASS VectorObject
29        PARAMPROCDEF(p_clear) { if (readonly) return; clear(); }
30        PARAMGETDEF(size) { arg1->setInt(data.size()); }
31        PARAMGETDEF(avg);
32        PARAMGETDEF(stdev);
33        PARAMGETDEF(iterator);
34        PARAMPROCDEF(p_remove);
35        PARAMPROCDEF(p_get);
36        PARAMPROCDEF(p_find);
37        PARAMPROCDEF(p_set) { if (!readonly) set_or_insert(arg1[1].getInt(), arg1[0], false); }
38        PARAMPROCDEF(p_add) { if (readonly) return; /*ExtValue tmp; get_toString(&tmp); printf("%s += %s",(const char*)tmp.getString(),(const char*)arg1[0].getString());*/ data += new ExtValue(arg1[0]); /*get_toString(&tmp); printf(" -> %s\n",(const char*)tmp.getString());*/ arg2->setInt(data.size() - 1); }
39        PARAMPROCDEF(p_insert) { if (!readonly) set_or_insert(arg1[1].getInt(), arg1[0], true); }
40        PARAMGETDEF(toString);
41        PARAMPROCDEF(p_sort);
42        PARAMPROCDEF(p_clone);
43#undef STATRICKCLASS
44        static void p_new(void*, ExtValue*args, ExtValue*ret)
45        {
46                ret->setObject(ExtObject(&par, new VectorObject));
47        }
48        SString serialize(SerializationFormat format) const;
49        ExtObject makeObject() { return ExtObject(&par, this); }
50
51        static VectorObject* fromObject(const ExtObject& o, bool warn = true);
52};
53
54/** object collection, indexed by name */
55class DictionaryObject : public DestrBase
56{
57public:
58        HashTable hash;
59        HashEntryIterator it;
60        int it_index;
61
62        void clear();
63        HashEntryIterator* getIndexIterator(int i);
64
65        static Param par;
66        DictionaryObject() :it(hash), it_index(-1) {}
67        ~DictionaryObject() { clear(); }
68        static Param& getStaticParam() { return par; }
69#define STATRICKCLASS DictionaryObject
70        PARAMPROCDEF(p_clear) { clear(); }
71        PARAMGETDEF(size) { arg1->setInt(hash.getSize()); }
72        PARAMPROCDEF(p_remove);
73        PARAMPROCDEF(p_get);
74        PARAMPROCDEF(p_getKey);
75        PARAMPROCDEF(p_hasKey);
76        PARAMPROCDEF(p_set);
77        PARAMPROCDEF(p_find);
78        PARAMGETDEF(toString);
79        PARAMPROCDEF(p_clone);
80        PARAMPROCDEF(p_assign);
81        PARAMGETDEF(iterator);
82        PARAMGETDEF(keys);
83#undef STATRICKCLASS
84        ExtValue get(SString key);
85        ExtValue get(int index);
86        ExtValue set(SString key, ExtValue new_value);
87        void copyFrom(DictionaryObject *other);
88        SString serialize(SerializationFormat format) const;
89        static void p_new(void*, ExtValue*args, ExtValue*ret)
90        {
91                ret->setObject(ExtObject(&par, new DictionaryObject));
92        }
93        static DictionaryObject* fromObject(const ExtObject& v, bool warn = true);
94        ExtObject makeObject() { return ExtObject(&par, this); }
95};
96
97class VectorIterator : public DestrBase
98{
99public:
100        VectorObject *vec;
101        int pos;
102        VectorIterator(VectorObject* v);
103        ~VectorIterator();
104#define STATRICKCLASS VectorIterator
105        PARAMGETDEF(next);
106        PARAMGETDEF(value);
107#undef STATRICKCLASS
108        static ExtObject makeFrom(VectorObject *v);
109};
110
111class DictionaryIterator : public DestrBase
112{
113public:
114        DictionaryObject *dic;
115        HashEntryIterator it;
116        bool initial, keys;
117        DictionaryIterator(DictionaryObject* d, bool _keys);
118        ~DictionaryIterator();
119#define STATRICKCLASS DictionaryIterator
120        PARAMGETDEF(next);
121        PARAMGETDEF(value);
122        PARAMGETDEF(iterator);
123#undef STATRICKCLASS
124        static ExtObject makeFrom(DictionaryObject *v, bool _keys = false);
125};
126
127#endif
Note: See TracBrowser for help on using the repository browser.