1 /* 2 Copyright (c) 2013 Andrey Penechko 3 4 Boost Software License - Version 1.0 - August 17th, 2003 5 6 Permission is hereby granted, free of charge, to any person or organization 7 obtaining a copy of the software and accompanying documentation covered by 8 this license the "Software" to use, reproduce, display, distribute, 9 execute, and transmit the Software, and to prepare derivative works of the 10 Software, and to permit third-parties to whom the Software is furnished to 11 do so, all subject to the following: 12 13 The copyright notices in the Software and this entire statement, including 14 the above license grant, this restriction and the following disclaimer, 15 must be included in all copies of the Software, in whole or in part, and 16 all derivative works of the Software, unless such copies or derivative 17 works are solely in the form of machine-executable object code generated by 18 a source language processor. 19 20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 DEALINGS IN THE SOFTWARE. 27 */ 28 29 module anchovy.utils.flexibleobject.flexibleobject; 30 31 import std.variant : Variant; 32 33 import anchovy.utils.flexibleobject.flexibleproperty : IProperty, ValueProperty; 34 import anchovy.utils.flexibleobject.flexibleaccess; 35 import anchovy.utils.signal : Signal; 36 37 // Object that can have any number of properties 38 class FlexibleObject 39 { 40 override int opCmp(Object o) { return cast(int)cast(void*)this - cast(int)cast(void*)o; } 41 42 package IProperty[string] properties; 43 44 Variant opIndex(string propName) 45 { 46 return properties.get(propName, new ValueProperty(this, Variant(null))).value; 47 } 48 49 Variant opIndexAssign(V)(V value, string propName) 50 { 51 static if (is(V:IProperty) && !is(V == typeof(null))) 52 { 53 return properties[propName] = value; 54 } 55 else static if (is(V:Variant)) 56 { 57 if (auto property = propName in properties) 58 { 59 return property.value = value; 60 } 61 else 62 { 63 return properties[propName] = new ValueProperty(this, value); 64 } 65 } 66 else 67 { 68 if (auto property = propName in properties) 69 { 70 return property.value = Variant(value); 71 } 72 else 73 { 74 return properties[propName] = new ValueProperty(this, Variant(value)); 75 } 76 } 77 } 78 79 Variant opIndexOpAssign(string op, V)(V value, string propName) 80 { 81 IProperty property = properties.get(propName, new ValueProperty(this, Variant(null))); 82 83 mixin("property.value " ~ op ~ "= value;"); 84 85 return property.value; 86 } 87 88 Variant remove(string propName) 89 { 90 Variant result = properties.get(propName, new ValueProperty(this, Variant(null))).value; 91 92 properties.remove(propName); 93 94 return result; 95 } 96 97 IProperty property(string propName) 98 { 99 if (propName !in properties) 100 { 101 properties[propName] = new ValueProperty(this, Variant(null)); 102 } 103 104 return properties[propName]; 105 } 106 }