summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVitor Sousa <vitorsousasilva@gmail.com>2018-02-16 15:24:11 -0200
committerVitor Sousa <vitorsousasilva@gmail.com>2018-02-16 15:24:11 -0200
commitbc174ae1159bf024877b84bbfad2465ab7c9bf64 (patch)
tree7c00c6f1099876501642175287f02fd109674e0c
parent66dd51dc4baa3db406a751d024ec4c812b9b30d7 (diff)
eina_mono: add first reference examples for Eina C# binding
-rw-r--r--reference/csharp/eina/meson.build11
-rw-r--r--reference/csharp/eina/src/eina_array.cs75
-rw-r--r--reference/csharp/eina/src/eina_hash.cs129
-rw-r--r--reference/csharp/eina/src/eina_iterator.cs78
-rw-r--r--reference/csharp/eina/src/eina_list.cs71
-rw-r--r--reference/csharp/eina/src/eina_log.cs62
-rw-r--r--reference/csharp/eina/src/eina_value.cs93
-rw-r--r--reference/csharp/eina/src/meson.build43
8 files changed, 562 insertions, 0 deletions
diff --git a/reference/csharp/eina/meson.build b/reference/csharp/eina/meson.build
new file mode 100644
index 0000000..d637eab
--- /dev/null
+++ b/reference/csharp/eina/meson.build
@@ -0,0 +1,11 @@
1project(
2 'efl-reference-eina', 'cs',
3 version : '0.0.1',
4 meson_version : '>= 0.38.0')
5
6
7efl_mono = dependency('efl-mono', version : '>=1.20.99')
8efl_mono_libs = efl_mono.get_pkgconfig_variable('mono_libs')
9
10subdir('src')
11
diff --git a/reference/csharp/eina/src/eina_array.cs b/reference/csharp/eina/src/eina_array.cs
new file mode 100644
index 0000000..a81085f
--- /dev/null
+++ b/reference/csharp/eina/src/eina_array.cs
@@ -0,0 +1,75 @@
1/*
2 * Eina Array examples.
3 *
4 * These examples demonstrate how to work with eina_array data and methods.
5 * We use a simple array of strings to initialise our eina_array before
6 * performing various mutations and printing the results.
7 */
8
9using System;
10
11public class Example
12{
13 static eina.Array<string> CreateArray()
14 {
15 // some content to populate our array
16 string[] names =
17 {
18 "helo", "hera", "starbuck", "kat", "boomer",
19 "hotdog", "longshot", "jammer", "crashdown", "hardball",
20 "duck", "racetrack", "apolo", "husker", "freaker",
21 "skulls", "bulldog", "flat top", "hammerhead", "gonzo"
22 };
23
24 // set up an array with a growth step to give a little headroom
25 var array = new eina.Array<string>(25u);
26
27 foreach (string name in names)
28 array.Push(name);
29
30 return array;
31 }
32
33 static bool ItemRemoveCb(string name)
34 {
35 // let's keep any strings that are no more than 7 characters long
36 if (name.Length <= 7)
37 return false;
38 return true;
39 }
40
41 public static void Main()
42 {
43 efl.All.Init();
44
45 var array = CreateArray();
46
47 // show the contents of our array
48 Console.WriteLine("Array count: {0}", array.Count());
49 Console.WriteLine("Array contents:");
50 foreach(string name in array)
51 {
52 // content is strings so we simply print the data
53 Console.WriteLine(" {0}", name);
54 }
55
56 // access a specific item in the array
57 Console.WriteLine("Top gun: {0}", array[2]);
58
59 // update a single item in the array
60 array[17] = "flattop";
61 // update the array removing items that match the ItemRemoveCb criteria
62 // array.RemoveAll(ItemRemoveCb); // TODO: FIXME
63
64 // print the new contents of our array
65 Console.WriteLine("New array count: {0}", array.Length);
66 Console.WriteLine("New array contents:");
67 foreach(string name in array)
68 Console.WriteLine(" {0}", name);
69
70 array.Dispose();
71
72 efl.All.Shutdown();
73 }
74}
75
diff --git a/reference/csharp/eina/src/eina_hash.cs b/reference/csharp/eina/src/eina_hash.cs
new file mode 100644
index 0000000..b85bfaf
--- /dev/null
+++ b/reference/csharp/eina/src/eina_hash.cs
@@ -0,0 +1,129 @@
1/*
2 * Eina Hash examples.
3 *
4 * These examples demonstrate how to work with eina_hash data and methods.
5 *
6 * We have two main hash objects here, firstly an int keyed hash with some
7 * dummy content.
8 * The second example is that of a simple telephone book. The names are the
9 * string keys to the hash with phone numbers in the data field.
10 */
11
12using System;
13using System.Collections.Generic;
14
15public class Example
16{
17 static eina.Hash<Int32, string> CreateHash()
18 {
19 // let's create a simple hash with integers as keys
20 var hash = new eina.Hash<Int32, string>();
21
22 // Add initial entries to our hash
23 for (int i = 0; i < 10; ++i)
24 hash.Add(i, $"The number {i}");
25
26 return hash;
27 }
28
29 static void HashDemo()
30 {
31 var hash = CreateHash();
32
33 // get an iterator of the keys so we can print a line per entry
34 var iter = hash.Keys();
35
36 Console.WriteLine("Print contents of int hash");
37 foreach (int key in iter)
38 {
39 // look up the value for the key so we can print both
40 string value = hash.Find(key);
41 Console.WriteLine($" Item found with id {key} has value {value}");
42 }
43 iter.Dispose();
44 Console.WriteLine("");
45
46 hash.Dispose();
47 }
48
49 // here we begin the phone book example
50
51 static void PrintPhonebookEntry(string key, string data)
52 {
53 Console.WriteLine($" Name: {key}\tNumber {data}\n");
54 }
55
56 static void PrintPhonebook(eina.Hash<string, string> book)
57 {
58 int count = book.Population();
59 Console.WriteLine($"Complete phone book ({count}):");
60
61 // as an enumerator, iterate over the key and value for each entry
62 foreach (KeyValuePair<string, string> kvp in book)
63 PrintPhonebookEntry(kvp.Key, kvp.Value);
64
65 Console.WriteLine("");
66 }
67
68 static eina.Hash<string, string> CreatePhonebook()
69 {
70 string[] names =
71 {
72 "Wolfgang Amadeus Mozart", "Ludwig van Beethoven",
73 "Richard Georg Strauss", "Heitor Villa-Lobos"
74 };
75 string[] numbers =
76 {
77 "+01 23 456-78910", "+12 34 567-89101",
78 "+23 45 678-91012", "+34 56 789-10123"
79 };
80
81 // create hash of strings to strings
82 var hash = new eina.Hash<string, string>();
83
84 // Add initial entries to our hash
85 for (int i = 0; i < 4; ++i)
86 hash.Add(names[i], numbers[i]);
87
88 return hash;
89 }
90
91 static void PhonebookDemo()
92 {
93 string lookup_name = "Ludwig van Beethoven";
94
95 var phone_book = CreatePhonebook();
96 PrintPhonebook(phone_book);
97
98 string number = phone_book.Find(lookup_name);
99 Console.WriteLine("Found entry:");
100 PrintPhonebookEntry(lookup_name, number);
101 Console.WriteLine("");
102
103 // Let's first add a new entry
104 phone_book["Raul Seixas"] = "+55 01 234-56789";
105
106 // Change the phone number for an entry
107 string old_num = phone_book[lookup_name];
108 phone_book[lookup_name] = "+12 34 222-22222";
109 Console.WriteLine($"Old number for {lookup_name} was {old_num}\n");
110
111 // Change the name (key) on an entry
112 phone_book.Move("Raul Seixas", "Alceu Valenca");
113
114 Console.WriteLine("After modifications");
115 PrintPhonebook(phone_book);
116
117 phone_book.Dispose();
118 }
119
120 public static void Main()
121 {
122 efl.All.Init();
123
124 HashDemo();
125 PhonebookDemo();
126
127 efl.All.Shutdown();
128 }
129}
diff --git a/reference/csharp/eina/src/eina_iterator.cs b/reference/csharp/eina/src/eina_iterator.cs
new file mode 100644
index 0000000..7033abf
--- /dev/null
+++ b/reference/csharp/eina/src/eina_iterator.cs
@@ -0,0 +1,78 @@
1
2/*
3 * Eina Iterator examples.
4 *
5 * These examples demonstrate how to work with eina_iterator methods.
6 * Both an eina_list and an eina_array are created and an iterator obtained
7 * for both. You can see how we can use iterators irrespective of the source
8 * and also that there are different ways to work with iterating content.
9 */
10
11using System;
12
13public class Example
14{
15 static void PrintIterator(eina.Iterator<string> it)
16 {
17 Console.WriteLine("--iterator start--");
18 foreach(string s in it)
19 Console.WriteLine(s);
20 Console.WriteLine("-- iterator end --");
21 }
22
23 static eina.Array<string> CreateArray()
24 {
25 string[] strings =
26 {
27 "name strings",
28 "husker",
29 "starbuck",
30 "boomer"
31 };
32
33 var array = new eina.Array<string>(4u);
34
35 foreach (string s in strings)
36 array.Push(s);
37
38 return array;
39 }
40
41 static eina.List<string> CreateList()
42 {
43 string[] more_strings = {
44 "sentence strings",
45 "what do your hear?",
46 "nothing but the rain",
47 "then grab your gun and bring the cat in"
48 };
49
50 var list = new eina.List<string>();
51
52 foreach (string s in more_strings)
53 list.Append(s);
54
55 return list;
56 }
57
58 public static void Main()
59 {
60 efl.All.Init();
61
62 // create an eina.Array and iterate through it's contents
63 var array = CreateArray();
64 var it = array.GetIterator();
65 PrintIterator(it);
66 it.Dispose();
67 array.Dispose();
68
69 // perform the same iteration with an eina.List
70 var list = CreateList();
71 it = list.GetIterator();
72 PrintIterator(it);
73 it.Dispose();
74 list.Dispose();
75
76 efl.All.Shutdown();
77 }
78}
diff --git a/reference/csharp/eina/src/eina_list.cs b/reference/csharp/eina/src/eina_list.cs
new file mode 100644
index 0000000..b1ddfef
--- /dev/null
+++ b/reference/csharp/eina/src/eina_list.cs
@@ -0,0 +1,71 @@
1
2/*
3 * Eina List examples.
4 *
5 * These examples demonstrate how to work with eina_list data and methods.
6 * We create a simple list of names by appending strings to an empty list
7 * and then run various mutations and print each result.
8 */
9
10using System;
11using System.Linq;
12
13public class Example
14{
15 static eina.List<string> CreateList()
16 {
17 var list = new eina.List<string>();
18
19 list.Append("Adama");
20 list.Append("Baltar");
21 list.Append("Roslin");
22
23 return list;
24 }
25
26 public static void Main()
27 {
28 efl.All.Init();
29
30 var list = CreateList();
31
32 // print our list with a simple foreach
33 Console.WriteLine("List size: {0}", list.Count());
34 Console.WriteLine("List content:");
35 foreach(string item in list)
36 Console.WriteLine(" {0}", item);
37
38 // insert some more elements
39 list.Prepend("Cain");
40// list.PrependRelative("Tigh", "Baltar"); // TODO: missing
41
42 Console.WriteLine("New list content:");
43 foreach(string item in list)
44 Console.WriteLine(" {0}", item);
45
46 // promote an item to the top of the list
47 // TODO: implement ?
48// list.PromoteList(list.NthList(1));
49// list.Remove("Cain");
50//
51// Console.WriteLine("List content after promotion:");
52// foreach(string item in list)
53// Console.WriteLine(" {0}", item);
54
55 // we can sort the list with any callback
56// list.Sort((string strA, string strB) => { return strA.Compare(strB); }); // TODO: FIXME custom sort
57 list.Sort();
58 Console.WriteLine("List content sorted:");
59 foreach(string item in list)
60 Console.WriteLine(" {0}", item);
61
62 // and foreach can be in reverse too
63 Console.WriteLine("List content reverse sorted:");
64 foreach(string item in list.Reverse())
65 Console.WriteLine(" {0}", item);
66
67 list.Dispose();
68
69 efl.All.Shutdown();
70 }
71}
diff --git a/reference/csharp/eina/src/eina_log.cs b/reference/csharp/eina/src/eina_log.cs
new file mode 100644
index 0000000..ca8cce8
--- /dev/null
+++ b/reference/csharp/eina/src/eina_log.cs
@@ -0,0 +1,62 @@
1
2/*
3 * Efl Core Log examples.
4 *
5 * This demo shows how to log at various levels and to change what log is shown.
6 * You can also use a custom log printer in your app as shown in _log_custom.
7 */
8
9using System;
10
11public class Example
12{
13 static double Divide(int num, int denom)
14 {
15 if (denom == 0)
16 eina.Log.Critical("Attempt to divide by 0\n");
17 else
18 {
19 if (denom < 0)
20 eina.Log.Warning("Possible undesirable effect, divide by negative number");
21
22 double ret = ((double) num / denom);
23 eina.Log.Info($"{num} / {denom} = {ret}\n");
24 return ret;
25 }
26
27 return -1;
28 }
29
30 static void Divides()
31 {
32 Divide(5, 1);
33 Divide(5, -1);
34 Divide(5, 0);
35 }
36
37 static void LogLevels()
38 {
39 Console.WriteLine("Executing with default logging");
40 Divides();
41
42 eina.Log.GlobalLevelSet(eina.Log.Level.Warning);
43 Console.WriteLine("Executing with Warning level"); // same as EINA_LOG_LEVEL = 2
44 Divides();
45
46 eina.Log.GlobalLevelSet(eina.Log.Level.Info);
47 Console.WriteLine("Executing with Info on"); // same as EINA_LOG_LEVEL = 3
48 Divides();
49 }
50
51 public static void Main()
52 {
53 efl.All.Init();
54
55 LogLevels();
56
57 // TODO: missing
58 //LogCustom();
59
60 efl.All.Shutdown();
61 }
62}
diff --git a/reference/csharp/eina/src/eina_value.cs b/reference/csharp/eina/src/eina_value.cs
new file mode 100644
index 0000000..f4ec556
--- /dev/null
+++ b/reference/csharp/eina/src/eina_value.cs
@@ -0,0 +1,93 @@
1
2/*
3 * Eina Value examples.
4 *
5 * These examples demonstrate how to work with eina_value data and methods.
6 * Eina_Value is a way to represent and pass data of varying types and to
7 * convert efficiently between them..
8 * Eina_Value can even define structs for managing more complex requirements.
9 */
10
11using System;
12
13public class Example
14{
15 static void ValueInt()
16 {
17 int i;
18
19 // Setting up an integer value type
20 var int_val = new eina.Value(eina.ValueType.Int32);
21 int_val.Set(123);
22 int_val.Get(out i);
23 Console.WriteLine("int_val value is {0}", i);
24
25 // It can easily be converted it to a string
26 string str = int_val.ToString();
27 Console.WriteLine("int_val to string is \"{0}\"", str);
28 int_val.Flush();
29 }
30
31 static void ValueString()
32 {
33 string str;
34
35 // Setting up an string value type
36 var str_val = new eina.Value(eina.ValueType.String);
37 str_val.Set("My string");
38 str_val.Get(out str);
39 Console.WriteLine("str_val value is \"{0}\"", str);
40
41 // To string should have the same content
42 string newstr = str_val.ToString();
43 Console.WriteLine("str_val to string is \"{0}\"", newstr);
44 str_val.Flush();
45 }
46
47 static void ValueConvert()
48 {
49 // set up string and int types to convert between
50 var str_val = new eina.Value(eina.ValueType.String);
51 var int_val = new eina.Value(eina.ValueType.Int32);
52
53 // convert from int to string:
54 int i1;
55 string str1;
56 int_val.Set(123);
57 int_val.Get(out i1);
58 int_val.ConvertTo(str_val);
59 str_val.Get(out str1);
60 Console.WriteLine("int_val was {0}, converted to string is \"{1}\"", i1, str1);
61
62 // and the other way around!
63 int i2;
64 string str2;
65 str_val.Set("33");
66 str_val.Get(out str2);
67 str_val.ConvertTo(int_val);
68 int_val.Get(out i2);
69 Console.WriteLine("str_val was \"{0}\", converted to int is {1}", str2, i2);
70
71 str_val.Flush();
72 int_val.Flush();
73 }
74
75 public static void Main()
76 {
77 efl.All.Init();
78
79 ValueInt();
80 Console.WriteLine("");
81
82 ValueString();
83 Console.WriteLine("");
84
85 ValueConvert();
86 Console.WriteLine("");
87
88 // TODO: FIXME
89 // ValueStruct();
90
91 efl.All.Shutdown();
92 }
93}
diff --git a/reference/csharp/eina/src/meson.build b/reference/csharp/eina/src/meson.build
new file mode 100644
index 0000000..a0ebaf1
--- /dev/null
+++ b/reference/csharp/eina/src/meson.build
@@ -0,0 +1,43 @@
1deps = [efl_mono]
2
3executable('efl_reference_eina_array',
4 files(['eina_array.cs']),
5 dependencies : deps,
6 cs_args : efl_mono_libs,
7 install : true
8)
9
10executable('efl_reference_eina_list',
11 files(['eina_list.cs']),
12 dependencies : deps,
13 cs_args : efl_mono_libs,
14 install : true
15)
16
17executable('efl_reference_eina_iterator',
18 files(['eina_iterator.cs']),
19 dependencies : deps,
20 cs_args : efl_mono_libs,
21 install : true
22)
23
24executable('efl_reference_eina_hash',
25 files(['eina_hash.cs']),
26 dependencies : deps,
27 cs_args : efl_mono_libs,
28 install : true
29)
30
31executable('efl_reference_eina_value',
32 files(['eina_value.cs']),
33 dependencies : deps,
34 cs_args : efl_mono_libs,
35 install : true
36)
37
38executable('efl_reference_eina_log',
39 files(['eina_log.cs']),
40 dependencies : deps,
41 cs_args : efl_mono_libs,
42 install : true
43)