vitorsousa pushed a commit to branch master.

http://git.enlightenment.org/tools/examples.git/commit/?id=bc174ae1159bf024877b84bbfad2465ab7c9bf64

commit bc174ae1159bf024877b84bbfad2465ab7c9bf64
Author: Vitor Sousa <vitorsousasi...@gmail.com>
Date:   Fri Feb 16 15:24:11 2018 -0200

    eina_mono: add first reference examples for Eina C# binding
---
 reference/csharp/eina/meson.build          |  11 +++
 reference/csharp/eina/src/eina_array.cs    |  75 +++++++++++++++++
 reference/csharp/eina/src/eina_hash.cs     | 129 +++++++++++++++++++++++++++++
 reference/csharp/eina/src/eina_iterator.cs |  78 +++++++++++++++++
 reference/csharp/eina/src/eina_list.cs     |  71 ++++++++++++++++
 reference/csharp/eina/src/eina_log.cs      |  62 ++++++++++++++
 reference/csharp/eina/src/eina_value.cs    |  93 +++++++++++++++++++++
 reference/csharp/eina/src/meson.build      |  43 ++++++++++
 8 files changed, 562 insertions(+)

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 @@
+project(
+  'efl-reference-eina', 'cs',
+  version : '0.0.1',
+  meson_version : '>= 0.38.0')
+
+
+efl_mono = dependency('efl-mono', version : '>=1.20.99')
+efl_mono_libs = efl_mono.get_pkgconfig_variable('mono_libs')
+
+subdir('src')
+
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 @@
+/*
+ * Eina Array examples.
+ *
+ * These examples demonstrate how to work with eina_array data and methods.
+ * We use a simple array of strings to initialise our eina_array before
+ * performing various mutations and printing the results.
+ */
+
+using System;
+
+public class Example
+{
+    static eina.Array<string> CreateArray()
+    {
+        // some content to populate our array
+        string[] names =
+        {
+            "helo", "hera", "starbuck", "kat", "boomer",
+            "hotdog", "longshot", "jammer", "crashdown", "hardball",
+            "duck", "racetrack", "apolo", "husker", "freaker",
+            "skulls", "bulldog", "flat top", "hammerhead", "gonzo"
+        };
+
+        // set up an array with a growth step to give a little headroom
+        var array = new eina.Array<string>(25u);
+
+        foreach (string name in names)
+            array.Push(name);
+
+        return array;
+    }
+
+    static bool ItemRemoveCb(string name)
+    {
+        // let's keep any strings that are no more than 7 characters long
+        if (name.Length <= 7)
+            return false;
+        return true;
+    }
+
+    public static void Main()
+    {
+        efl.All.Init();
+
+        var array = CreateArray();
+
+        // show the contents of our array
+        Console.WriteLine("Array count: {0}", array.Count());
+        Console.WriteLine("Array contents:");
+        foreach(string name in array)
+        {
+            // content is strings so we simply print the data
+            Console.WriteLine("  {0}", name);
+        }
+
+        // access a specific item in the array
+        Console.WriteLine("Top gun: {0}", array[2]);
+
+        // update a single item in the array
+        array[17] = "flattop";
+        // update the array removing items that match the ItemRemoveCb criteria
+        // array.RemoveAll(ItemRemoveCb); // TODO: FIXME
+
+        // print the new contents of our array
+        Console.WriteLine("New array count: {0}", array.Length);
+        Console.WriteLine("New array contents:");
+        foreach(string name in array)
+            Console.WriteLine("  {0}", name);
+
+        array.Dispose();
+
+        efl.All.Shutdown();
+    }
+}
+
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 @@
+/*
+ * Eina Hash examples.
+ *
+ * These examples demonstrate how to work with eina_hash data and methods.
+ *
+ * We have two main hash objects here, firstly an int keyed hash with some
+ * dummy content.
+ * The second example is that of a simple telephone book. The names are the
+ * string keys to the hash with phone numbers in the data field.
+ */
+
+using System;
+using System.Collections.Generic;
+
+public class Example
+{
+    static eina.Hash<Int32, string> CreateHash()
+    {
+        // let's create a simple hash with integers as keys
+        var hash = new eina.Hash<Int32, string>();
+
+        // Add initial entries to our hash
+        for (int i = 0; i < 10; ++i)
+            hash.Add(i, $"The number {i}");
+
+        return hash;
+    }
+
+    static void HashDemo()
+    {
+        var hash = CreateHash();
+
+        // get an iterator of the keys so we can print a line per entry
+        var iter = hash.Keys();
+
+        Console.WriteLine("Print contents of int hash");
+        foreach (int key in iter)
+        {
+            // look up the value for the key so we can print both
+            string value = hash.Find(key);
+            Console.WriteLine($"  Item found with id {key} has value {value}");
+        }
+        iter.Dispose();
+        Console.WriteLine("");
+
+        hash.Dispose();
+    }
+
+    // here we begin the phone book example
+
+    static void PrintPhonebookEntry(string key, string data)
+    {
+        Console.WriteLine($"  Name: {key}\tNumber {data}\n");
+    }
+
+    static void PrintPhonebook(eina.Hash<string, string> book)
+    {
+        int count = book.Population();
+        Console.WriteLine($"Complete phone book ({count}):");
+
+        // as an enumerator, iterate over the key and value for each entry
+        foreach (KeyValuePair<string, string> kvp in book)
+            PrintPhonebookEntry(kvp.Key, kvp.Value);
+
+        Console.WriteLine("");
+    }
+
+    static eina.Hash<string, string> CreatePhonebook()
+    {
+        string[] names =
+        {
+            "Wolfgang Amadeus Mozart", "Ludwig van Beethoven",
+            "Richard Georg Strauss", "Heitor Villa-Lobos"
+        };
+        string[] numbers =
+        {
+            "+01 23 456-78910", "+12 34 567-89101",
+            "+23 45 678-91012", "+34 56 789-10123"
+        };
+
+        // create hash of strings to strings
+        var hash = new eina.Hash<string, string>();
+
+        // Add initial entries to our hash
+        for (int i = 0; i < 4; ++i)
+            hash.Add(names[i], numbers[i]);
+
+        return hash;
+    }
+
+    static void PhonebookDemo()
+    {
+        string lookup_name = "Ludwig van Beethoven";
+
+        var phone_book = CreatePhonebook();
+        PrintPhonebook(phone_book);
+
+        string number = phone_book.Find(lookup_name);
+        Console.WriteLine("Found entry:");
+        PrintPhonebookEntry(lookup_name, number);
+        Console.WriteLine("");
+
+        // Let's first add a new entry
+        phone_book["Raul Seixas"] = "+55 01 234-56789";
+
+        // Change the phone number for an entry
+        string old_num = phone_book[lookup_name];
+        phone_book[lookup_name] = "+12 34 222-22222";
+        Console.WriteLine($"Old number for {lookup_name} was {old_num}\n");
+
+        // Change the name (key) on an entry
+        phone_book.Move("Raul Seixas", "Alceu Valenca");
+
+        Console.WriteLine("After modifications");
+        PrintPhonebook(phone_book);
+
+        phone_book.Dispose();
+    }
+
+    public static void Main()
+    {
+        efl.All.Init();
+
+        HashDemo();
+        PhonebookDemo();
+
+        efl.All.Shutdown();
+    }
+}
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 @@
+
+/*
+ * Eina Iterator examples.
+ *
+ * These examples demonstrate how to work with eina_iterator methods.
+ * Both an eina_list and an eina_array are created and an iterator obtained
+ * for both. You can see how we can use iterators irrespective of the source
+ * and also that there are different ways to work with iterating content.
+ */
+
+using System;
+
+public class Example
+{
+    static void PrintIterator(eina.Iterator<string> it)
+    {
+        Console.WriteLine("--iterator start--");
+        foreach(string s in it)
+            Console.WriteLine(s);
+        Console.WriteLine("-- iterator end --");
+    }
+
+    static eina.Array<string> CreateArray()
+    {
+        string[] strings =
+        {
+            "name strings",
+            "husker",
+            "starbuck",
+            "boomer"
+        };
+
+        var array = new eina.Array<string>(4u);
+
+        foreach (string s in strings)
+            array.Push(s);
+
+        return array;
+    }
+
+    static eina.List<string> CreateList()
+    {
+        string[] more_strings = {
+            "sentence strings",
+            "what do your hear?",
+            "nothing but the rain",
+            "then grab your gun and bring the cat in"
+        };
+
+        var list = new eina.List<string>();
+
+        foreach (string s in more_strings)
+            list.Append(s);
+
+        return list;
+    }
+
+    public static void Main()
+    {
+        efl.All.Init();
+
+        // create an eina.Array and iterate through it's contents
+        var array = CreateArray();
+        var it = array.GetIterator();
+        PrintIterator(it);
+        it.Dispose();
+        array.Dispose();
+
+        // perform the same iteration with an eina.List
+        var list = CreateList();
+        it = list.GetIterator();
+        PrintIterator(it);
+        it.Dispose();
+        list.Dispose();
+
+        efl.All.Shutdown();
+    }
+}
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 @@
+
+/*
+ * Eina List examples.
+ *
+ * These examples demonstrate how to work with eina_list data and methods.
+ * We create a simple list of names by appending strings to an empty list
+ * and then run various mutations and print each result.
+ */
+
+using System;
+using System.Linq;
+
+public class Example
+{
+    static eina.List<string> CreateList()
+    {
+        var list = new eina.List<string>();
+
+        list.Append("Adama");
+        list.Append("Baltar");
+        list.Append("Roslin");
+
+        return list;
+    }
+
+    public static void Main()
+    {
+        efl.All.Init();
+
+        var list = CreateList();
+
+        // print our list with a simple foreach
+        Console.WriteLine("List size: {0}", list.Count());
+        Console.WriteLine("List content:");
+        foreach(string item in list)
+            Console.WriteLine("  {0}", item);
+
+        // insert some more elements
+        list.Prepend("Cain");
+//         list.PrependRelative("Tigh", "Baltar"); // TODO: missing
+
+        Console.WriteLine("New list content:");
+        foreach(string item in list)
+            Console.WriteLine("  {0}", item);
+
+        // promote an item to the top of the list
+        // TODO: implement ?
+//         list.PromoteList(list.NthList(1));
+//         list.Remove("Cain");
+//
+//         Console.WriteLine("List content after promotion:");
+//         foreach(string item in list)
+//             Console.WriteLine("  {0}", item);
+
+        // we can sort the list with any callback
+//         list.Sort((string strA, string strB) => { return 
strA.Compare(strB); }); // TODO: FIXME custom sort
+        list.Sort();
+        Console.WriteLine("List content sorted:");
+        foreach(string item in list)
+            Console.WriteLine("  {0}", item);
+
+        // and foreach can be in reverse too
+        Console.WriteLine("List content reverse sorted:");
+        foreach(string item in list.Reverse())
+            Console.WriteLine("  {0}", item);
+
+        list.Dispose();
+
+        efl.All.Shutdown();
+    }
+}
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 @@
+
+/*
+ * Efl Core Log examples.
+ *
+ * This demo shows how to log at various levels and to change what log is 
shown.
+ * You can also use a custom log printer in your app as shown in _log_custom.
+ */
+
+using System;
+
+public class Example
+{
+    static double Divide(int num, int denom)
+    {
+        if (denom == 0)
+            eina.Log.Critical("Attempt to divide by 0\n");
+        else
+        {
+            if (denom < 0)
+                eina.Log.Warning("Possible undesirable effect, divide by 
negative number");
+
+            double ret = ((double) num / denom);
+            eina.Log.Info($"{num} / {denom} = {ret}\n");
+            return ret;
+        }
+
+        return -1;
+    }
+
+    static void Divides()
+    {
+        Divide(5, 1);
+        Divide(5, -1);
+        Divide(5, 0);
+    }
+
+    static void LogLevels()
+    {
+        Console.WriteLine("Executing with default logging");
+        Divides();
+
+        eina.Log.GlobalLevelSet(eina.Log.Level.Warning);
+        Console.WriteLine("Executing with Warning level"); // same as 
EINA_LOG_LEVEL = 2
+        Divides();
+
+        eina.Log.GlobalLevelSet(eina.Log.Level.Info);
+        Console.WriteLine("Executing with Info on"); // same as EINA_LOG_LEVEL 
= 3
+        Divides();
+    }
+
+    public static void Main()
+    {
+        efl.All.Init();
+
+        LogLevels();
+
+        // TODO: missing
+        //LogCustom();
+
+        efl.All.Shutdown();
+    }
+}
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 @@
+
+/*
+ * Eina Value examples.
+ *
+ * These examples demonstrate how to work with eina_value data and methods.
+ * Eina_Value is a way to represent and pass data of varying types and to
+ * convert efficiently between them..
+ * Eina_Value can even define structs for managing more complex requirements.
+ */
+
+using System;
+
+public class Example
+{
+    static void ValueInt()
+    {
+        int i;
+
+        // Setting up an integer value type
+        var int_val = new eina.Value(eina.ValueType.Int32);
+        int_val.Set(123);
+        int_val.Get(out i);
+        Console.WriteLine("int_val value is {0}", i);
+
+        // It can easily be converted it to a string
+        string str = int_val.ToString();
+        Console.WriteLine("int_val to string is \"{0}\"", str);
+        int_val.Flush();
+    }
+
+    static void ValueString()
+    {
+        string str;
+
+        // Setting up an string value type
+        var str_val = new eina.Value(eina.ValueType.String);
+        str_val.Set("My string");
+        str_val.Get(out str);
+        Console.WriteLine("str_val value is \"{0}\"", str);
+
+        // To string should have the same content
+        string newstr = str_val.ToString();
+        Console.WriteLine("str_val to string is \"{0}\"", newstr);
+        str_val.Flush();
+    }
+
+    static void ValueConvert()
+    {
+        // set up string and int types to convert between
+        var str_val = new eina.Value(eina.ValueType.String);
+        var int_val = new eina.Value(eina.ValueType.Int32);
+
+        // convert from int to string:
+        int i1;
+        string str1;
+        int_val.Set(123);
+        int_val.Get(out i1);
+        int_val.ConvertTo(str_val);
+        str_val.Get(out str1);
+        Console.WriteLine("int_val was {0}, converted to string is \"{1}\"", 
i1, str1);
+
+        // and the other way around!
+        int i2;
+        string str2;
+        str_val.Set("33");
+        str_val.Get(out str2);
+        str_val.ConvertTo(int_val);
+        int_val.Get(out i2);
+        Console.WriteLine("str_val was \"{0}\", converted to int is {1}", 
str2, i2);
+
+        str_val.Flush();
+        int_val.Flush();
+    }
+
+    public static void Main()
+    {
+        efl.All.Init();
+
+        ValueInt();
+        Console.WriteLine("");
+
+        ValueString();
+        Console.WriteLine("");
+
+        ValueConvert();
+        Console.WriteLine("");
+
+        // TODO: FIXME
+        // ValueStruct();
+
+        efl.All.Shutdown();
+    }
+}
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 @@
+deps = [efl_mono]
+
+executable('efl_reference_eina_array',
+  files(['eina_array.cs']),
+  dependencies : deps,
+  cs_args : efl_mono_libs,
+  install : true
+)
+
+executable('efl_reference_eina_list',
+  files(['eina_list.cs']),
+  dependencies : deps,
+  cs_args : efl_mono_libs,
+  install : true
+)
+
+executable('efl_reference_eina_iterator',
+  files(['eina_iterator.cs']),
+  dependencies : deps,
+  cs_args : efl_mono_libs,
+  install : true
+)
+
+executable('efl_reference_eina_hash',
+  files(['eina_hash.cs']),
+  dependencies : deps,
+  cs_args : efl_mono_libs,
+  install : true
+)
+
+executable('efl_reference_eina_value',
+  files(['eina_value.cs']),
+  dependencies : deps,
+  cs_args : efl_mono_libs,
+  install : true
+)
+
+executable('efl_reference_eina_log',
+  files(['eina_log.cs']),
+  dependencies : deps,
+  cs_args : efl_mono_libs,
+  install : true
+)

-- 


Reply via email to