ptypes

ptypes Commit Details


Date:2013-05-10 23:40:00 (11 years 7 months ago)
Author:Natalie Adams
Branch:default
Commit:a5343ffaee93
Parents: 6e47101f975d
Message:Adding tparray also changing tpodlist object to be DLL exportable

Changes:
Asrc/ptparray.cpp (full)
Minclude/ptypes.h (4 diffs)
Msrc/pvariant.cxx (1 diff)
Mwin32/PTypes_Lib.vcxproj (1 diff)
Mwin32/PTypes_Lib.vcxproj.filters (1 diff)

File differences

include/ptypes.h
461461
462462
463463
464
464
465465
466466
467467
......
984984
985985
986986
987
988987
989988
990989
......
10531052
10541053
10551054
1055
1056
1057
1058
1059
10561060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
10571123
10581124
10591125
......
11421208
11431209
11441210
1211
11451212
11461213
11471214
// tpodlist is a fully-inlined template based on _podlist
template <class X, bool initzero = false> class tpodlist: public _podlist
template <class X, bool initzero = false> class ptpublic tpodlist: public _podlist
{
protected:
X& dozero(X& t) { if (initzero) memset(&t, 0, sizeof(X)); return t; }
ptpublic friend void ptdecl apack(variant&);
ptpublic friend bool ptdecl anext(const variant& a, int&, variant& item);
ptpublic friend bool ptdecl anext(const variant& a, int&, variant& item, string& key);
ptpublic friend bool ptdecl anext(const variant& a, int&, variant& item, variant& key);
ptpublic friend int ptdecl aadd(variant&, const variant& item);
ptpublic friend void ptdecl aput(variant&, int index, const variant& item);
ptpublic friend void ptdecl ains(variant&, int index, const variant& item);
virtual ~evariant();
};
enum {
ARR_ASSOCIATIVE,
ARR_LIST,
ARR_NULL
};
/*
The reason for a tparray is that while a variant can be an
associative array - it stores numeric keys as hex strings
this can be very problematic if you want to loop through
all the elements and know if a given key was a number.
(For example for JSON encoding)
A tparray uses a variant for both the keys/values so
this really opens it up to a wide range of uses. Also,
it performs no conversions on the data so when you loop
through all the keys/vals they are what you stored them as.
This object's complexity is O(n) and it could in theory be better
by sectioning off the arrays such that
-------------------------------------------------------
| string | string | string | number | number | number |
-------------------------------------------------------
This would increase the complexity of adding items, and
if you don't have a very diverse number of items them you
may not benefit much from the algorithm.
*/
class ptpublic tparray : public component
{
protected:
int tag;
tpodlist<variant> _keys;
tpodlist<variant> _vals;
public:
tparray() { tag = ARR_NULL; }
//anext function acts similar for variants and loops through elements
// This is allowed for both types
ptpublic friend bool ptdecl anext(const tparray& a, int&, variant& val);
// This is only allowed for Associative arrays as a list doesn't have keys
ptpublic friend bool ptdecl anext(const tparray& a, int&, variant& val, variant & key);
/*
Initialy the object will be NULL - but calling one of these functions
will change the type (permanently)
*/
ptpublic friend void ptdecl add(tparray & t, const variant & val);
ptpublic friend void ptdecl add(tparray & t, const variant & key, const variant & val);
// get will perform a O(n) search for the matching variant in the keys array
ptpublic friend variant ptdecl get(tparray & t, const variant & val);
// This will return the value at the given index
ptpublic friend variant ptdecl at(tparray & t, int index);
// This will return the key at the given index
ptpublic friend variant ptdecl keyat(tparray & t, int index);
friend bool isassoc(const tparray& t);
friend bool islist(const tparray& t);
friend bool isnull(const tparray& t);
};
inline bool isassoc(const tparray& t) { return t.tag == ARR_ASSOCIATIVE; }
inline bool islist(const tparray& t) { return t.tag == ARR_LIST; }
inline bool isnull(const tparray& t) { return t.tag == ARR_NULL; }
// -------------------------------------------------------------------- //
// --- pre-2.0 compatibility declarations ----------------------------- //
inline void del(textmap& m, const string& k) { m.del(k); }
#endif // PTYPES19_COMPAT
src/ptparray.cpp
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
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include "ptypes.h"
PTYPES_BEGIN
bool ptdecl anext(const tparray& a, int& i, variant& val)
{
variant t;
return anext(a, i, val, t);
}
bool ptdecl anext(const tparray& a, int& i, variant& val, variant & key)
{
if (a._keys.get_count() == i)
{
return false;
}
else
{
val = a._vals[i];
key = a._keys[i];
i++;
return true;
}
}
void ptdecl add(tparray & t, const variant & val)
{
if (t.tag == ARR_NULL || t.tag == ARR_LIST)
{
t.tag = ARR_LIST;
t._vals.add(val);
}
//else drop it
}
void ptdecl add(tparray & t, const variant & key, const variant & val)
{
if (t.tag == ARR_NULL || t.tag == ARR_ASSOCIATIVE)
{
t.tag = ARR_ASSOCIATIVE;
t._vals.add(val);
t._keys.add(key);
}
}
variant ptdecl get(tparray & t, const variant & val)
{
for(int i = 0; i < t._keys.get_count(); i++)
{
if (t._keys[i] == val)
{
return t._vals[i];
}
}
return NULL;
}
variant ptdecl at(tparray & t, int index)
{
return t._vals[index];
}
variant ptdecl keyat(tparray & t, int index)
{
return t._keys[index];
}
PTYPES_END
src/pvariant.cxx
588588
589589
590590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609591
610592
611593
return true;
}
bool ptdecl anext(const variant& array, int& index, variant& item, variant & key)
{
if (array.tag != VAR_ARRAY)
{
clear(item);
return false;
}
if (index < 0 || index >= array.value.a->get_count())
{
clear(item);
return false;
}
item = array.value.a->doget(index)->var;
key = array.value.a->doget(index)->key;
index++;
return true;
}
void ptdecl add(variant & v, const variant & var)
{
put(v, alength(v), var);
win32/PTypes_Lib.vcxproj
216216
217217
218218
219
219220
220221
221222
<AdditionalIncludeDirectories Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<ClCompile Include="..\src\ptparray.cpp" />
<ClCompile Include="..\src\punit.cxx">
<AdditionalIncludeDirectories Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">%(PreprocessorDefinitions)</PreprocessorDefinitions>
win32/PTypes_Lib.vcxproj.filters
182182
183183
184184
185
186
187
185188
186189
187190
<ClCompile Include="..\src\pipsvbase.cxx">
<Filter>Inet</Filter>
</ClCompile>
<ClCompile Include="..\src\ptparray.cpp">
<Filter>Types</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\include\pasync.h">

Archive Download the corresponding diff file

Branches

Tags

Page rendered in 0.65179s using 14 queries.