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

Last change on this file since 1288 was 1158, checked in by Maciej Komosinski, 3 years ago

Cosmetic/minor improvements

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