aboutsummaryrefslogtreecommitdiffstats
path: root/pages/develop/guides/csharp/eina/generic-value.md.txt
blob: 969d1d5fa87b910942afe59eb8e5388d38e0b782 (plain) (blame)
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
---
~~Title: Eina Generic Values in C#~~
---

# Eina Generic Values in C# #

The `Eina` namespace provides data types and useful tools which are not available in plain C. The C# runtime though, already provides most of `Eina`'s functionality so you will rarely need to use it. The `Eina Programming Guides` are still useful for those EFL methods which use `Eina` types as parameters or return types.

The `Eina.Value` class provides storage of and access to generic data, allowing you to store whatever you want in a single `Eina.Value` type. It is meant for simple data types, providing uniform access and release functions for the exchange of data while preserving their types. `Eina.Value` supports a number of predefined types, can be extended with additional user-provided types and can convert between differing data types including strings.

Examples of `Eina.Value` usage can be found in the [EFL examples git repository](https://git.enlightenment.org/tools/examples.git/) in the file [`reference/csharp/eina/src/eina_value.cs`](https://git.enlightenment.org/tools/examples.git/tree/reference/csharp/eina/src/eina_value.cs).

|     | WARNING |     |
| --- | ------- | --- |
| ![NOTE](/_media/note-important.png) | **The C# bindings are currently in BETA state**<br>They should only be used for experimenting and **NOT** for any product development.<br>The source code for the tutorials is subject to change in the future. | ![NOTE](/_media/note-important.png) |

## Value Types ##

`Eina.Value` can handle the following common *simple* types (found inside the `Eina.ValueType` enum):

| `Eina.ValueType`    | C# type        | Content                                  |
| ------------------- | -------------- | ---------------------------------------- |
| `SByte`             | `sbyte`        | Signed char (8-bit integer)              |
| `Byte`              | `byte`         | Unsigned char (8-bit integer)            |
| `Short`             | `short`        | Signed short (16-bit integer)            |
| `UShort`            | `ushort`       | Unsigned short (16-bit integer)          |
| `Int32`             | `int`          | Signed integer (32-bit)                  |
| `UInt32`            | `uint`         | Unsigned integer (32-bit)                |
| `Long`, `Int64`     | `long`         | Signed long (64-bit integer)             |
| `ULong`, `UInt64`   | `ulong`        | Unsigned long (64-bit integer)           |
| `Float`             | `float`        | Single precision (32-bit) floating point |
| `Double`            | `double`       | Double precision (64-bit) floating point |
| `String`            | `string`       | Unicode text string                      |

In addition, `Eina.Value` supports the following *aggregate* types:

| `Eina.ValueType` | Content                    |
| ---------------- | -------------------------- |
| `Array`          | Manages arrays of elements |
| `List`           | Manages lists of elements  |
| `Hash`           | Manages hash tables        |

## Simple Values ##

### Creating Simple Values ###

New values can be allocated with the standard `new()` operator passing in the desired type as an `Eina.ValueType`:

```csharp
var int_val = new Eina.Value(Eina.ValueType.Int32);
```

When the `int_val` variable goes out of scope any memory allocated internally by the `Eina.Value` will be freed as expected.

### Changing the Type of Simple Values ###

The type of an `Eina.Value` can be changed at any time using `Setup()` and passing in the desired new type:

```csharp
var val = new Eina.Value(Eina.ValueType.Int32);
val.Setup(Eina.ValueType.Float);
```

The content of the `Eina.Value` will be destroyed, but **not** the `Eina.Value` variable.

### Accessing the Content of Simple Values ###

The contents of a simple `Eina.Value` can be set with `Set()` and retrieved with `Get()`. Note that the types provided must match the type used when creating the `Eina.Value`.

For instance, for integers:
```csharp
var val = new Eina.Value(Eina.ValueType.Int32);
val.Set(123);

int i;
val.Get(out i);
```

Strings are also easily handled:
```csharp
var val = new Eina.Value(Eina.ValueType.String);
val.Set("My string");

string str;
val.Get(out str);
```

### Copying the Content of a Value ###

`Eina.Value`s are **referenced types**, which means that when you make a copy of an `Eina.Value` variable, the underlying value is not copied: both variables point to the same value.

```csharp
var src = new Eina.Value(Eina.ValueType.Int32);
src.Set(100);
var dst = src;
src.Set(200);
// Now both src and dst contain the integer 200
```

If you need to create a separate `Eina.Value` variable containing the same value, use `new` and initialize it:

```csharp
var src = new Eina.Value(Eina.ValueType.Int32);
src.Set(100);
var dst = new Eina.Value(src);
src.Set(200);
// Now src=200 and dst=100
```

### Comparing Two Values ###

Two `Eina.Value`s of the same type can be compared with the standard operators `<`, `==`, `!=` and `>`. The exact meaning of the comparison depends on the value type.

```csharp
var v1 = new Eina. Value(Eina.ValueType.Float);
var v2 = new Eina. Value(Eina.ValueType.Float);
v1.Set(7.0f);
v2.Set(7.5f);
if (v1 > v2) { ... }
```

The internal method `v1.Compare(v2)` is also available. The exact meaning of the comparison depends again on the value type. The method returns: a negative integer if **v1 < v2**, a positive integer if **v1 > v2** and **0** if both values are equal.

### Converting Between Values ###

Most `Eina.Value`s allow conversion from one type to another using the `ConvertTo()` function. The result of the conversion depends on the types in use. This function returns `true` if the conversion is successful. The conversion is typically very strict, meaning that conversion of negative values into unsigned types will fail and values which will not fit in the target type - i.e. conversions that would result in an overflow - will also fail.

```csharp
var v1 = new Eina.Value(Eina.ValueType.Int);
var v2 = new Eina.Value(Eina.ValueType.Float);
v1.Set(7);
v1.ConvertTo(v2);
Console.WriteLine(v2);
```

The above code should output `7.0000` on the screen because `v2` is a floating type.

### Converting to Strings ###

All `Eina.Value`s allow for conversion into a string, and, for convenience, there is a dedicated conversion method: `ToString()`.

```csharp
var v1 = new Eina.Value(Eina.ValueType.Int);
v1.Set(7);
string str = v1.ToString();
```

### Implicit Conversions ###

For convenience, implicit conversion operators also exist between an `Eina.Value` and the actual stored type, so operations like these are possible:

```csharp
Eina.Value int_val = 123;
Eina.Value str_val = "My string";
int i = int_val;
string str = str_val;
```
These conversions allow directly providing native values to EFL methods expecting an `Eina.Value`, for example.

> **NOTE:**
> As with all implicit conversions, use with caution to make sure the compiler is actually choosing the types you expect.

## Aggregate Values ##

`Eina.Value` supports handling collections of simple values through the aggregate types `Eina.ValueType.Array`, `Eina.ValueType.List` and `Eina.ValueType.Hash`.

All aggregate types allow the operations for simple types described above as well as some specific methods, typically involving construction and access to particular elements within the collection.

### Arrays ###

An array is a contiguous block of memory which holds a collection of elements of the same type. Accessing and appending new elements at the end is typically very fast, but removing elements from the beginning or the middle is not.

**Create** a new array with `new(Eina.ValueType.Array, subtype, step)`, or re-configure an existing one with `Setup(Eina.ValueType.Array, subtype, step)`. The `subtype` parameter is the type of the `Eina.Value`s that will be stored in the array. The `step` parameter indicates how many elements are added to the end of the array every time it needs to be expanded, since it is typically more efficient to enlarge the array by chunks rather than element by element. Its default value is 0 meaning that EFL will choose a value for you.

```csharp
var varray = new Eina.Value(Eina.ValueType.Array, Eina.ValueType.Int32);
```

**Retrieve the number of elements** in an array with `Count()`.

```csharp
int count = varray.Count();
```

**Append an element** at the end of the array with `Append()`.

```csharp
varray.Append(100);
varray.Append(200);
```

**Retrieve or set the contents** of the value at a given position with the `[]` operator.

```csharp
varray[1] = 100;
int old_val = varray[1];
```

**Retrieve the type of the elements** of an array (the `subtype`) with `GetValueSubType()`:

```csharp
Eina.ValueType subtype = varray.GetValueSubType();
```

> **NOTE:**
> Elements cannot be removed from an array as of now, due to limitations in the current C# bindings.

### Lists ###

A list is a linked collection of `Eina.Value`s in which each element contains a pointer to the next element. Insertions and deletions anywhere in the list are typically very fast, but accessing a given position can be slow since it requires traveling through the list.

**Create** a new list with `new(Eina.ValueType.Array, subtype)`, or configure an existing one with `Setup(subtype)`. The `subtype` parameter is the type of the `Eina.Value`s that will be stored in the list.

```csharp
var vlist = new Eina.Value(Eina.ValueType.List, Eina.ValueType.Int32);
```

**Retrieve the number of elements** in a list with `Count()`.

```csharp
int count = vlist.Count();
```

**Append an element** at the end of the list with `Append()`.

```csharp
vlist.Append(100);
vlist.Append(200);
```

> **NOTE:**
> Elements can only be added at the end as of now, due to limitations in the current C# bindings.

**Retrieve or set the contents** of the value at a given position with the `[]` operator.

```csharp
vlist[1] = 100;
int old_val = vlist[1];
```

**Retrieve the type of the elements** of a list (the `subtype`) with `GetValueSubType()`:

```csharp
Eina.ValueType subtype = vlist.GetValueSubType();
```

> **NOTE:**
> Elements cannot be removed from a list as of now, due to limitations in the current C# bindings.

### Hash Tables ###

A hash table stores `Eina.Value`s indexed by a string key rather than an integer. Insertions, deletions and searches are typically very fast, at the cost of increased memory consumption.

> **NOTE:**
> Implementation of Hash tables inside `Eina.Value`s is not complete at this moment.

**Create** a new hash table with `new(Eina.ValueType.Hash, subtype, bucket_size)`, or configure an existing one with `Setup(subtype, bucket_size)`. The `subtype` parameter is the type of the `Eina.Value`s that will be stored in the hash table. The `bucket_size` parameter indicates how the table is to be expanded as new elements are added; use 0 and a sane default will be chosen automatically.

```csharp
var vhash = new (Eina.ValueType.Hash, Eina.ValueType.Int32);
```

**Retrieve the number of elements** in a hash table with `Count()`.

```csharp
int count = vhash.Count();
```

## Further Reading ##

[Eina Value example](https://git.enlightenment.org/tools/examples.git/tree/reference/csharp/eina/src/eina_value.cs)
:    Miscellaneous usage example for the `Eina.Value` type.

[Generic Value API](/develop/legacy/api/c/start#group__Eina__Value__Group.html)
:    Reference Guide for the Generic Value API (in C).