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

Last change on this file since 453 was 453, checked in by Maciej Komosinski, 8 years ago

Built-in shallow clone() for Vector and Dictionary

  • Property svn:eol-style set to native
File size: 2.8 KB
Line 
1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
2// Copyright (C) 1999-2015  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{
16  public:
17SList data;
18unsigned int readonly:1, owndata:1;
19void clear();
20ExtValue *get(int i) {return (ExtValue*)data.get(i);}
21void set(int i,const ExtValue& val);
22
23static Param par;
24VectorObject(Pt3D& pt);
25VectorObject():readonly(0),owndata(1) {}
26~VectorObject() {clear();}
27#define STATRICKCLASS VectorObject
28PARAMPROCDEF(p_clear) {if (readonly) return; clear();}
29PARAMGETDEF(size) {arg1->setInt(data.size());}
30PARAMGETDEF(avg);
31PARAMGETDEF(stdev);
32PARAMGETDEF(iterator);
33PARAMPROCDEF(p_remove);
34PARAMPROCDEF(p_get);
35PARAMPROCDEF(p_find);
36PARAMPROCDEF(p_set) {if (!readonly) set(arg1[1].getInt(),arg1[0]);}
37PARAMPROCDEF(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);}
38PARAMGETDEF(toString);
39PARAMPROCDEF(p_sort);
40PARAMPROCDEF(p_clone);
41#undef STATRICKCLASS
42static void p_new(void*,ExtValue*args,ExtValue*ret)
43        {ret->setObject(ExtObject(&par,new VectorObject));}
44SString serialize() const;
45ExtObject makeObject() {return ExtObject(&par,this);}
46
47static VectorObject* fromObject(const ExtObject& o, bool warn=true);
48};
49
50/** object collection, indexed by name */
51class DictionaryObject: public DestrBase
52{
53  public:
54HashTable hash;
55HashEntryIterator it;
56int it_index;
57
58void clear();
59HashEntryIterator* getIndexIterator(int i);
60
61static Param par;
62DictionaryObject():it(hash),it_index(-1) {}
63~DictionaryObject() {clear();}
64#define STATRICKCLASS DictionaryObject
65PARAMPROCDEF(p_clear) {clear();}
66PARAMGETDEF(size) {arg1->setInt(hash.getSize());}
67PARAMPROCDEF(p_remove);
68PARAMPROCDEF(p_get);
69PARAMPROCDEF(p_getKey);
70PARAMPROCDEF(p_set);
71PARAMPROCDEF(p_find);
72PARAMGETDEF(toString);
73PARAMPROCDEF(p_clone);
74#undef STATRICKCLASS
75SString serialize() const;
76static void p_new(void*,ExtValue*args,ExtValue*ret)
77        {ret->setObject(ExtObject(&par,new DictionaryObject));}
78static DictionaryObject* fromObject(const ExtObject& v, bool warn=true);
79ExtObject makeObject() {return ExtObject(&par,this);}
80};
81
82class VectorIterator: public DestrBase
83{
84  public:
85VectorObject *vec;
86int pos;
87VectorIterator(VectorObject* v);
88~VectorIterator();
89#define STATRICKCLASS VectorIterator
90PARAMGETDEF(next);
91PARAMGETDEF(value);
92#undef STATRICKCLASS
93static ExtObject makeFrom(VectorObject *v);
94};
95
96#endif
Note: See TracBrowser for help on using the repository browser.