3using System.Collections.Generic;
7using System.Threading.Tasks;
15 public class PropertySet : IEnumerable<KeyValuePair<string, string>>
17 Dictionary<string, string> m_rgProperties =
new Dictionary<string, string>();
18 Dictionary<string, int> m_rgPropertiesInt =
new Dictionary<string, int>();
19 Dictionary<string, byte[]> m_rgBlobs =
new Dictionary<string, byte[]>();
27 m_rgProperties = rgProp;
36 string[] rgstr = strProp.Split(
';');
38 m_rgProperties =
new Dictionary<string, string>();
40 foreach (
string strP
in rgstr)
44 int nPos = strP.IndexOf(
'=');
47 string strKey = strP.Substring(0, nPos);
48 string strVal = strP.Substring(nPos + 1);
50 if (!m_rgProperties.ContainsKey(strKey))
51 m_rgProperties.Add(strKey, strVal);
69 get {
return m_rgProperties.Keys.ToList(); }
77 get {
return m_rgBlobs.Keys.ToList(); }
85 get {
return m_rgPropertiesInt.Keys.ToList(); }
95 if (!m_rgProperties.ContainsKey(strName))
98 string strVal = m_rgProperties[strName];
99 m_rgProperties.Remove(strName);
101 using (MemoryStream ms =
new MemoryStream())
102 using (BinaryWriter bw =
new BinaryWriter(ms))
104 foreach (
char ch
in strVal)
111 m_rgBlobs.Add(strName, ms.ToArray());
123 foreach (KeyValuePair<string, string> kv
in prop.m_rgProperties)
125 if (!m_rgProperties.ContainsKey(kv.Key))
126 m_rgProperties.Add(kv.Key, kv.Value);
128 m_rgProperties[kv.Key] = kv.Value;
131 foreach (KeyValuePair<
string,
byte[]> kv
in prop.m_rgBlobs)
133 if (!m_rgBlobs.ContainsKey(kv.Key))
134 m_rgBlobs.Add(kv.Key, kv.Value);
136 m_rgBlobs[kv.Key] = kv.Value;
146 public string GetProperty(
string strName,
bool bThrowExceptions =
true)
148 if (!m_rgProperties.ContainsKey(strName))
150 if (bThrowExceptions)
151 throw new Exception(
"The property '" + strName +
"' was not found!");
156 return m_rgProperties[strName];
167 if (!m_rgPropertiesInt.ContainsKey(strName))
169 if (bThrowExceptions)
170 throw new Exception(
"The property '" + strName +
"' was not found!");
175 return m_rgPropertiesInt[strName];
186 if (!m_rgBlobs.ContainsKey(strName))
188 if (bThrowExceptions)
189 throw new Exception(
"The property '" + strName +
"' was not found!");
194 return m_rgBlobs[strName];
203 m_rgProperties.Remove(strName);
213 if (!m_rgProperties.ContainsKey(strName))
214 m_rgProperties.Add(strName, strVal);
216 m_rgProperties[strName] = strVal;
226 if (!m_rgProperties.ContainsKey(strName))
227 m_rgPropertiesInt.Add(strName, nVal);
229 m_rgPropertiesInt[strName] = nVal;
239 if (!m_rgBlobs.ContainsKey(strName))
240 m_rgBlobs.Add(strName, rg);
242 m_rgBlobs[strName] = rg;
255 if (!DateTime.TryParse(strVal, out dt))
256 throw new Exception(
"Failed to parse '" + strName +
"' as a DateTime. The value = '" + strVal +
"'");
275 if (!
bool.TryParse(strVal, out bVal))
276 throw new Exception(
"Failed to parse '" + strName +
"' as an Boolean. The value = '" + strVal +
"'");
295 if (!
int.TryParse(strVal, out nVal))
296 throw new Exception(
"Failed to parse '" + strName +
"' as an Integer. The value = '" + strVal +
"'");
316 throw new Exception(
"Failed to parse '" + strName +
"' as a Double. The value = '" + strVal +
"'");
329 foreach (KeyValuePair<string, string> kv
in m_rgProperties)
331 str += kv.Key +
"=" + kv.Value;
344 return m_rgProperties.GetEnumerator();
351 IEnumerator IEnumerable.GetEnumerator()
353 return m_rgProperties.GetEnumerator();
The BaseParameter class is the base class for all other parameter classes.
static bool TryParse(string strVal, out double df)
Parse double values using the US culture if the decimal separator = '.', then using the native cultur...
Specifies a key-value pair of properties.
string GetProperty(string strName, bool bThrowExceptions=true)
Returns a property as a string value.
List< string > PropertyIntNames
Returns the list of integer property names contained in the property set.
int GetPropertyInt(string strName, bool bThrowExceptions=true)
Returns an int property as a int value.
DateTime GetPropertyAsDateTime(string strName)
Returns a property as a DateTime value.
List< string > PropertyBlobNames
Returns the list of blob names contained in the property set.
void Merge(PropertySet prop)
Merge a given property set into this one.
PropertySet(Dictionary< string, string > rgProp)
The constructor, initialized with a dictionary of properties.
void DeleteProperty(string strName)
Delete a property by name.
void SetPropertyBlob(string strName, byte[] rg)
Sets a property in the blob set to a byte array if it exists, otherwise it adds the new blob.
byte[] GetPropertyBlob(string strName, bool bThrowExceptions=true)
Returns a property blob as a byte array value.
void SetPropertyInt(string strName, int nVal)
Sets an int property in the property set to a value if it exists, otherwise it adds the new property.
int GetPropertyAsInt(string strName, int nDefault=0)
Returns a property as an integer value.
IEnumerator< KeyValuePair< string, string > > GetEnumerator()
Returns an enumerator of the key/value pairs.
PropertySet()
The constructor.
bool GetPropertyAsBool(string strName, bool bDefault=false)
Returns a property as a boolean value.
bool MovePropertyToBlob(string strName)
Move the property from the list of properties to the list of blobs, storing the blob as a 0 terminate...
double GetPropertyAsDouble(string strName, double dfDefault=0)
Returns a property as an double value.
PropertySet(string strProp)
The constructor, initialized with a string containing a set of ';' separated key-value pairs.
List< string > PropertyNames
Returns the list of property names contained in the property set.
override string ToString()
Returns the string representation of the properties.
void SetProperty(string strName, string strVal)
Sets a property in the property set to a value if it exists, otherwise it adds the new property.
The MyCaffe.basecode contains all generic types used throughout MyCaffe.