Daily Archives: February 25, 2014
migration C#, Java, C++ (day 2)
Day 2. I feel some heat already 😉
managed code
public class Dog { public string Name { get; set; } public Dog() { } public Dog(string xName) { Name = xName; } } // -------------------------------------------------------------- { Dog lDog1 = new Dog(); Dog lDog2 = new Dog(); lDog1 = null; // allows the garbage collector to collect lDog1 } // leaving the scope allows lDog2 to be collected
public class Dog { public Dog() {} public Dog(String xName) { setName(xName); } private String _Name; public final String getName() { return _Name; } public final void setName(String value) { _Name = value; } } // class // -------------------------------------------------------------- { Dog lDog1 = new Dog(); Dog lDog2 = new Dog(); lDog1 = null; // allows the garbage collector to collect lDog1 } // leaving the scope allows lDog2 to be collected
// .h file #ifndef DOG_H #define DOG_H #include <string> #include <memory> using namespace std; class Dog : public enable_shared_from_this<Dog> { private: string pName; public: const string &getName() const; void setName(const string &xName); Dog(); Dog(const string &xName); }; #endif // DOG_H // .cpp file #include "Dog.h" using namespace std; Dog::Dog(){} Dog::Dog(const string &xName) { setName(xName); } const string &Dog::getName() const { return pName;} void Dog::setName(const string &value) { pName = value; } // -------------------------------------------------------------- shared_ptr<Dog> lDog1 (new Dog("Snoopy")); cout<<lDog1.use_count() << endl; // 1 shared_ptr<Dog> lDog2 = make_shared<Dog>("Lassie"); cout<<lDog2.use_count() << endl; // 1 shared_ptr<Dog> lDog3 = lDog2; cout<<lDog1.use_count() << endl; // 1 cout<<lDog2.use_count() << endl; // 2 cout<<lDog3.use_count() << endl; // 2
Dictionary
Dictionary<string, Double> lDictionary = new Dictionary<string, double>(); lDictionary.Add("-10-", 10.0); lDictionary.Add("-20-", 20.0); lDictionary.Add("-30-", 30.0); lDictionary.Add("-40-", 40.0); lDictionary.Remove("-30-"); foreach (var lPair in lDictionary) Console.WriteLine(lPair.Key + " equals " + lPair.Value); foreach (var lValue in lDictionary.Values) Console.WriteLine(lValue); Double d; if (lDictionary.TryGetValue("-10-", out d)) Console.WriteLine("found -10- which is: " + d);
java.util.HashMap<String, Double> lDictionary = new java.util.HashMap<String, Double>(); lDictionary.put("-10-", 10.0); lDictionary.put("-20-", 20.0); lDictionary.put("-30-", 30.0); lDictionary.put("-40-", 40.0); lDictionary.remove("-30-"); // C# var: There is no such as implicit typing in Java for (Entry<String, Double> lPair : lDictionary.entrySet()) System.out.println(lPair.getKey() + " equals " + lPair.getValue()); for (Double lValue : lDictionary.values()) System.out.println(lValue); double d = 0; if (lDictionary.containsKey("-10-") ? (d = lDictionary.get("-10-")) == d : false) System.out.println("found -10- which is: " + d);
unordered_map<string, double> lMap = unordered_map<string, double>(); lMap.insert(make_pair("-10-", 10.0)); lMap.insert(make_pair("-20-", 20.0)); lMap.insert(make_pair("-30-", 30.0)); lMap.insert(make_pair("-40-", 40.0)); lMap["-40-"] = 41.0; lMap.erase("-30-"); for (auto &lPair : lMap) { cout << lPair.first << " equals " << lPair.second << endl; // 10 20 41 } double d1 = lMap["-10-"]; cout << "found -10- which is: " << d1 << endl; // 10.0 double d2 = lMap["-11-"]; // returns the default value cout << "found -11- which is: " << d2 << endl; // 0.0 unordered_map<string, string> lMap2 = unordered_map<string, string>(); lMap2["hello"] = "hulla"; string s = lMap2["hola"]; if (s == "") s = "EMPTY"; cout << "lMap2[\"hola\"] == " << s << endl; // s == "EMPTY"
Queue
Queue<Dog> lDogs = new Queue<Dog>(); lDogs.Enqueue(new Dog("Boomer")); lDogs.Enqueue(new Dog("Spike")); lDogs.Enqueue(new Dog("Lassie")); Console.WriteLine(lDogs.Peek().Name); // Boomer Dog lDog = lDogs.Dequeue(); Console.WriteLine(lDogs.Peek().Name); // Spike
// Queue is not a class, just an interface java.util.Queue<Dog> lDogs = new java.util.LinkedList<Dog>(); lDogs.offer(new Dog("Boomer")); lDogs.offer(new Dog("Spike")); lDogs.offer(new Dog("Lassie")); System.out.println(lDogs.peek().getName()); // Boomer Dog lDog = lDogs.poll(); System.out.println(lDogs.peek().getName()); // Spike
#include <queue> queue<Dog*> lDogs = queue<Dog*>(); lDogs.push(new Dog("Boomer")); lDogs.push(new Dog("Spike")); lDogs.push(new Dog("Lassie")); cout << lDogs.front()->getName() << endl; // Boomer cout << lDogs.back()->getName() << endl; // Lassie lDogs.pop(); // takes Boomer from the queue cout << lDogs.front()->getName() << endl; // Spike cout << lDogs.back()->getName() << endl; // Lassie
Stack
Stack<Dog> lDogs = new Stack<Dog>(); lDogs.Push(new Dog("Boomer")); lDogs.Push(new Dog("Spike")); lDogs.Push(new Dog("Lassie")); Console.WriteLine(lDogs.Peek().Name); // Lassie Dog lDogLassie = lDogs.Pop(); Console.WriteLine(lDogs.Peek().Name); // Spike
java.util.Stack<Dog> lDogs = new java.util.Stack<Dog>(); lDogs.push(new Dog("Boomer")); lDogs.push(new Dog("Spike")); lDogs.push(new Dog("Lassie")); System.out.println(lDogs.peek().getName()); // Lassie Dog lDogLassie = lDogs.pop(); System.out.println(lDogs.peek().getName()); // Spike
stack<Dog*> lDogs = stack<Dog*>(); lDogs.push(new Dog("Boomer")); lDogs.push(new Dog("Spike")); lDogs.push(new Dog("Lassie")); cout << lDogs.top()->getName() << endl; // Lassie lDogs.pop(); // takes Lassie from the stack cout << lDogs.top()->getName() << endl; // Spike
Generics, Templates
T larger<T>(T obj1, T obj2 ) { if ((dynamic)obj1 > (dynamic)obj2) return obj1; return obj2; } // int i = larger<int>(6, 4); // i == 6 bool same<T>(T obj1, T obj2 ) where T : class { return (obj1 == obj2); } // bool b1 = same<string>("ABC", "abc"); // False bool b2 = same<string>("abc", "abc"); // True public class MyClass<T, U> where T : struct where U : class { public T MyFunc(T a, U b, T c) { return c; } } MyClass<int, string> lClass = new MyClass<int, string>(); int i = lClass.MyFunc(0, "hello", 1); // i == 1
<T extends Comparable<T>> T larger(T obj1, T obj2) { if (obj1.compareTo(obj2) > 0) return obj1; return obj2; } int i = this.<Integer>larger(6, 4); // i == 6 <T extends Comparable<T>> boolean same(T obj1, T obj2) { return (obj1 == obj2); } boolean b1 = this.<String>same("ABC", "abc"); // False boolean b2 = this.<String>same("abc", "abc"); // True class MyClass<T, U> { public final T MyFunc(T a, U b, T c) { return c; } } MyClass<Integer, String> lClass = new MyClass<Integer, String>(); int i = lClass.MyFunc(0, "hello", 1); // i == 1
template <typename T> T larger(T obj1, T obj2){ if(obj1 > obj2) return obj1; return obj2; } // int i = larger(6, 4); // i == 6 template <typename T> bool same(T obj1, T obj2){ if(obj1 == obj2) return true; return false; } // bool b1 = same("ABC", "abc"); // false bool b2 = same("abc", "abc"); // true bool b3 = same("abd", "abc"); // false template<typename T, typename U> class MyClass { U MyData; public: T MyFunc(T a, U b, T c) { return c; } void MyFunc(int a, U b) { } }; MyClass<int, string> lClass; int i = lClass.MyFunc(0, "hello", 1); // 1
simple Thread
No worries, there will be follow-ups.
private void MyThread(object x) { Console.WriteLine(x as string); } Thread lThread = new Thread(MyThread); lThread.IsBackground = true; lThread.Start("mySupaDupaaParameter");
public class MyThreadClass extends Thread { private String _Parameter; public MyThreadClass(String xParameter) { _Parameter = xParameter; } // constructor @Override public void run() { System.out.println(_Parameter); } // } // class MyThreadClass lInstance = new MyThreadClass("mySupaDupaaParameter"); Thread lThread = new Thread(lInstance); lThread.setDaemon(true); lThread.start();
#include <thread> void MyThread(string xParameter) { cout << xParameter; } thread lThread(MyThread, "mySupaDupaaParameter");
Data Binding (part 2, advanced), WPF
The new C++ posts will take a lot time. The C# posts are shorter for the next three weeks. Today I created a DataGrid and three TextBoxes. They are all linked together with pretty much no code. You can even add new items to the list. Check it out!
<Window x:Class="WpfDatabinding2.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="26*" /> <ColumnDefinition Width="192*" /> <ColumnDefinition Width="285*" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="10*" /> <RowDefinition Height="*" /> <RowDefinition Height="*" /> <RowDefinition Height="*" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <DataGrid AutoGenerateColumns="True" Grid.ColumnSpan="3" ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True" ColumnHeaderHeight="30"> </DataGrid> <TextBox Grid.Row="1" Grid.Column="1" Text="{Binding Name}" /> <TextBox Grid.Row="2" Grid.Column="1" Text="{Binding Owner}" /> <TextBox Grid.Column="1" Grid.Row="3" Text="{Binding Age}" /> </Grid> </Window>
using System.Collections.Generic; using System.Windows; using System.Windows.Documents; namespace WpfDatabinding2 { public partial class MainWindow : Window { public class Dog { public string Name { get; set; } public double Age { get; set; } public string Owner { get; set; } } // class public MainWindow() { InitializeComponent(); List<Dog> lDogs = new List<Dog>(); lDogs.Add(new Dog() { Name = "Spike", Owner = "Granny", Age = 12.6 }); lDogs.Add(new Dog() { Name = "Pluto", Owner = "Mickey Mouse", Age = 7.0 }); lDogs.Add(new Dog() { Name = "Snoopy", Owner = "Charlie Brown", Age = 5.3 }); lDogs.Add(new Dog() { Name = "Lassie", Owner = "Rudd Weatherwax", Age = 8.5 }); this.DataContext = lDogs; } // } // class } // namespace
Hard to believe, but this is it. Code does not have to be long to be powerful. The magic mostly comes from these lines:
<DataGrid AutoGenerateColumns="True" Grid.ColumnSpan="3" ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True" ColumnHeaderHeight="30"> </DataGrid> <TextBox Grid.Row="1" Grid.Column="1" Text="{Binding Name}" /> <TextBox Grid.Row="2" Grid.Column="1" Text="{Binding Owner}" /> <TextBox Grid.Column="1" Grid.Row="3" Text="{Binding Age}" />