/* ----------------------------------------------------------------------------- * See the LICENSE file for information on copyright, usage and redistribution * of SWIG, and the README file for authors - http://www.swig.org/release.html. * * std_map.i * * SWIG typemaps for std::map * C# implementation by Yuval Baror (http://yuval.bar-or.org) * * The C# wrapper is made to look and feel like a C# System.Collections.Generic.IDictionary<>. * * Using this wrapper is fairly simple. For example, to create a map from integers to doubles use: * * %include "std_map.i" * %template(Int_Vector) std::vector * %template(Int_Double_Mector) std::map * * Very often the C# generated code will not compile as the C++ template type is not the same as the C# * proxy type, so use the SWIG_STD_MAP_SPECIALIZE or SWIG_STD_MAP_SPECIALIZE_SIMPLE macros. For example: * * SWIG_STD_MAP_SPECIALIZE(MyCppKeyClass, MyCppValueClass, MyCsKeyClass, MyCsValueClass) * %template(MyCppKeyClass_Vector) std::vector; * %template(MyKeyClass_MyValueClass_Map) std::map; * * Or if the C# class names are the same as the C++ class names, you can use: * * SWIG_STD_MAP_SPECIALIZE_SIMPLE(MyKeyClass, MyValueClass) * %template(MyKeyClass_Vector) std::vector; * %template(MyKeyClass_MyValueClass_Map) std::map; * * Notes: * 1) For .NET 1 compatibility, define SWIG_DOTNET_1 when compiling the C# code. In this case * the C# wrapper has only basic functionality. * 2) This wrapper makes use of the std::vector wrapper for the map keys, so you must define * a template for the std::vector class (where MyKeyClass is the key class of * your map). * 3) IEnumerable<> is implemented in the proxy class which is useful for using LINQ with * C++ std::map wrappers. * * Warning: heavy macro usage in this file. Use swig -E to get a sane view on the real file contents! * ----------------------------------------------------------------------------- */ %include %include // ------------------------------------------------------------------------ // std::map // ------------------------------------------------------------------------ %{ #include #include #include #include %} // A minimal implementation to be used when no specialization exists. %define SWIG_STD_MAP_MINIMAL_INTERNAL(K, T) // add typemaps here public: map(); map(const map &); unsigned int size() const; bool empty() const; void clear(); %extend { T get(const K& key) throw (std::out_of_range) { std::map::iterator i = self->find(key); if (i != self->end()) return i->second; else throw std::out_of_range("key not found"); } void set(const K& key, const T& x) { (*self)[key] = x; } void del(const K& key) throw (std::out_of_range) { std::map::iterator i = self->find(key); if (i != self->end()) self->erase(i); else throw std::out_of_range("key not found"); } bool has_key(const K& key) { std::map::iterator i = self->find(key); return i != self->end(); } std::vector keys(void) { std::vector k = std::vector(); for ( std::map::iterator iter = self->begin(); iter != self->end(); iter++ ) { k.push_back(iter->first); } return k; } } %enddef /* The specialized std::map implementation * K is the C++ key type * T is the C++ value type * CSKEYTYPE is the C# key type * CSVALUETYPE is the C# value type */ %define SWIG_STD_MAP_SPECIALIZED_INTERNAL(K, T, CSKEYTYPE, CSVALUETYPE) // add typemaps here %typemap(csinterfaces) std::map "IDisposable \n#if !SWIG_DOTNET_1\n , System.Collections.Generic.IDictionary\n#endif\n"; %typemap(cscode) std::map %{ public CSVALUETYPE this[CSKEYTYPE key] { get { return getitem(key); } set { setitem(key, value); } } public bool TryGetValue(CSKEYTYPE key, out CSVALUETYPE value) { if (this.ContainsKey(key)) { value = this[key]; return true; } value = default(CSVALUETYPE); return false; } public int Count { get { return (int)size(); } } public bool IsReadOnly { get { return false; } } #if !SWIG_DOTNET_1 public System.Collections.Generic.ICollection Keys { get { return new System.Collections.Generic.List(getKeys()); } } public System.Collections.Generic.ICollection Values { get { System.Collections.Generic.ICollection vals = new System.Collections.Generic.List(); foreach (System.Collections.Generic.KeyValuePair pair in this) { vals.Add(pair.Value); } return vals; } } public void Add(System.Collections.Generic.KeyValuePair item) { Add(item.Key, item.Value); } public bool Remove(System.Collections.Generic.KeyValuePair item) { if (Contains(item)) { return Remove(item.Key); } else { return false; } } public bool Contains(System.Collections.Generic.KeyValuePair item) { if (this[item.Key] == item.Value) { return true; } else { return false; } } public void CopyTo(System.Collections.Generic.KeyValuePair[] array) { CopyTo(array, 0); } public void CopyTo(System.Collections.Generic.KeyValuePair[] array, int arrayIndex) { if (array == null) throw new ArgumentNullException("array"); if (arrayIndex < 0) throw new ArgumentOutOfRangeException("arrayIndex", "Value is less than zero"); if (array.Rank > 1) throw new ArgumentException("Multi dimensional array.", "array"); if (arrayIndex+this.Count > array.Length) throw new ArgumentException("Number of elements to copy is too large."); System.Collections.Generic.IList keyList = new System.Collections.Generic.List(this.Keys); for (int i = 0; i < keyList.Count; i++) { CSKEYTYPE currentKey = keyList[i]; array.SetValue(new System.Collections.Generic.KeyValuePair(currentKey, this[currentKey]), arrayIndex+i); } } System.Collections.Generic.IEnumerator> System.Collections.Generic.IEnumerable>.GetEnumerator() { return new $csclassnameEnumerator(this); } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return new $csclassnameEnumerator(this); } public $csclassnameEnumerator GetEnumerator() { return new $csclassnameEnumerator(this); } // Type-safe enumerator /// Note that the IEnumerator documentation requires an InvalidOperationException to be thrown /// whenever the collection is modified. This has been done for changes in the size of the /// collection but not when one of the elements of the collection is modified as it is a bit /// tricky to detect unmanaged code that modifies the collection under our feet. public sealed class $csclassnameEnumerator : System.Collections.IEnumerator, System.Collections.Generic.IEnumerator> { private $csclassname collectionRef; private System.Collections.Generic.IList keyCollection; private int currentIndex; private object currentObject; private int currentSize; public $csclassnameEnumerator($csclassname collection) { collectionRef = collection; keyCollection = new System.Collections.Generic.List(collection.Keys); currentIndex = -1; currentObject = null; currentSize = collectionRef.Count; } // Type-safe iterator Current public System.Collections.Generic.KeyValuePair Current { get { if (currentIndex == -1) throw new InvalidOperationException("Enumeration not started."); if (currentIndex > currentSize - 1) throw new InvalidOperationException("Enumeration finished."); if (currentObject == null) throw new InvalidOperationException("Collection modified."); return (System.Collections.Generic.KeyValuePair)currentObject; } } // Type-unsafe IEnumerator.Current object System.Collections.IEnumerator.Current { get { return Current; } } public bool MoveNext() { int size = collectionRef.Count; bool moveOkay = (currentIndex+1 < size) && (size == currentSize); if (moveOkay) { currentIndex++; CSKEYTYPE currentKey = keyCollection[currentIndex]; currentObject = new System.Collections.Generic.KeyValuePair(currentKey, collectionRef[currentKey]); } else { currentObject = null; } return moveOkay; } public void Reset() { currentIndex = -1; currentObject = null; if (collectionRef.Count != currentSize) { throw new InvalidOperationException("Collection modified."); } } public void Dispose() { currentIndex = -1; currentObject = null; } } #endif %} public: map(); map(const map &); typedef size_t size_type; size_type size() const; %rename(Clear) clear; void clear(); %extend { T getitem(K key) throw (std::out_of_range) { std::map::iterator i = self->find(key); if (i != self->end()) return i->second; else throw std::out_of_range("key not found"); } void setitem(K key, T x) { (*self)[key] = x; } std::vector getKeys(void) { std::vector k = std::vector(); for ( std::map::iterator iter = self->begin(); iter != self->end(); iter++ ) { k.push_back(iter->first); } return k; } bool ContainsKey(K key) { std::map::iterator iter = self->find(key); if (iter != self->end()) { return true; } return false; } void Add(K key, T val) throw (std::out_of_range) { std::map::iterator iter = self->find(key); if (iter != self->end()) { throw std::out_of_range("key already exists"); } self->insert(std::pair(key, val)); } bool Remove(K key) { std::map::iterator iter = self->find(key); if (iter != self->end()) { self->erase(iter); return true; } return false; } } %enddef %csmethodmodifiers std::map::getKeys "private" %csmethodmodifiers std::map::getValues "private" %csmethodmodifiers std::map::size "private" %csmethodmodifiers std::map::getitem "private" %csmethodmodifiers std::map::setitem "private" %define SWIG_STD_MAP_SPECIALIZED(K, T, CSKEY, CSVAL) namespace std { template<> class map { SWIG_STD_MAP_SPECIALIZED_INTERNAL(K, T, CSKEY, CSVAL) }; } %enddef %define SWIG_STD_MAP_SPECIALIZED_SIMPLE(K, T) SWIG_STD_MAP_SPECIALIZED(K, T, K, T) %enddef // exported class namespace std { // Regular impelementation template class map { SWIG_STD_MAP_MINIMAL_INTERNAL(K, T) }; } // specializations for built-ins SWIG_STD_MAP_SPECIALIZED(bool, std::string, bool, string) SWIG_STD_MAP_SPECIALIZED(char, std::string, char, string) SWIG_STD_MAP_SPECIALIZED(double, std::string, double, string) SWIG_STD_MAP_SPECIALIZED(float, std::string, float, string) SWIG_STD_MAP_SPECIALIZED(int, std::string, int, string) SWIG_STD_MAP_SPECIALIZED(std::string, bool, string, bool) SWIG_STD_MAP_SPECIALIZED(std::string, char, string, char) SWIG_STD_MAP_SPECIALIZED(std::string, double, string, double) SWIG_STD_MAP_SPECIALIZED(std::string, float, string, float) SWIG_STD_MAP_SPECIALIZED(std::string, int, string, int) SWIG_STD_MAP_SPECIALIZED(std::string, std::string, string, string) SWIG_STD_MAP_SPECIALIZED_SIMPLE(bool, bool) SWIG_STD_MAP_SPECIALIZED_SIMPLE(bool, char) SWIG_STD_MAP_SPECIALIZED_SIMPLE(bool, double) SWIG_STD_MAP_SPECIALIZED_SIMPLE(bool, float) SWIG_STD_MAP_SPECIALIZED_SIMPLE(bool, int) SWIG_STD_MAP_SPECIALIZED_SIMPLE(char, bool) SWIG_STD_MAP_SPECIALIZED_SIMPLE(char, char) SWIG_STD_MAP_SPECIALIZED_SIMPLE(char, double) SWIG_STD_MAP_SPECIALIZED_SIMPLE(char, float) SWIG_STD_MAP_SPECIALIZED_SIMPLE(char, int) SWIG_STD_MAP_SPECIALIZED_SIMPLE(double, bool) SWIG_STD_MAP_SPECIALIZED_SIMPLE(double, char) SWIG_STD_MAP_SPECIALIZED_SIMPLE(double, double) SWIG_STD_MAP_SPECIALIZED_SIMPLE(double, float) SWIG_STD_MAP_SPECIALIZED_SIMPLE(double, int) SWIG_STD_MAP_SPECIALIZED_SIMPLE(float, bool) SWIG_STD_MAP_SPECIALIZED_SIMPLE(float, char) SWIG_STD_MAP_SPECIALIZED_SIMPLE(float, double) SWIG_STD_MAP_SPECIALIZED_SIMPLE(float, float) SWIG_STD_MAP_SPECIALIZED_SIMPLE(float, int) SWIG_STD_MAP_SPECIALIZED_SIMPLE(int, bool) SWIG_STD_MAP_SPECIALIZED_SIMPLE(int, char) SWIG_STD_MAP_SPECIALIZED_SIMPLE(int, double) SWIG_STD_MAP_SPECIALIZED_SIMPLE(int, float) SWIG_STD_MAP_SPECIALIZED_SIMPLE(int, int) // add specializations here