using System;
namespace UnityEditor.Performance.ProfileAnalyzer
{
///
/// Individual Thread details
///
internal struct ThreadIdentifier
{
/// Thread name with index combined. A profiler analyzer specific unique thread representation
public string threadNameWithIndex { get; private set; }
/// Thread name (may not be unique)
public string name { get; private set; }
/// Thread index. A 1 based id for threads with matching names. -1 indicates all threads with the same name, 0 if there is only one thread with this name
public int index { get; private set; }
/// Thread index id which means all threads of the same name
public static int kAll = -1;
/// Thread index id use when there is only one thread with this name
public static int kSingle = 0;
/// Initialise ThreadIdentifier
/// The thread name
/// The thread index
public ThreadIdentifier(string name, int index)
{
this.name = name;
this.index = index;
if (index == kAll)
threadNameWithIndex = string.Format("All:{0}", name);
else
threadNameWithIndex = string.Format("{0}:{1}", index, name);
}
/// Initialise ThreadIdentifier from another ThreadIdentifier
/// The other ThreadIdentifier
public ThreadIdentifier(ThreadIdentifier threadIdentifier)
{
name = threadIdentifier.name;
index = threadIdentifier.index;
threadNameWithIndex = threadIdentifier.threadNameWithIndex;
}
/// Initialise ThreadIdentifier from a unique name
/// The unique name string (name with index)
public ThreadIdentifier(string threadNameWithIndex)
{
this.threadNameWithIndex = threadNameWithIndex;
string[] tokens = threadNameWithIndex.Split(':');
if (tokens.Length >= 2)
{
name = tokens[1];
string indexString = tokens[0];
if (indexString == "All")
{
index = kAll;
}
else
{
int intValue;
if (Int32.TryParse(tokens[0], out intValue))
index = intValue;
else
index = kSingle;
}
}
else
{
index = kSingle;
name = threadNameWithIndex;
}
}
void UpdateThreadNameWithIndex()
{
if (index == kAll)
threadNameWithIndex = string.Format("All:{0}", name);
else
threadNameWithIndex = string.Format("{0}:{1}", index, name);
}
/// Set the name of the thread
/// The name (without index)
public void SetName(string newName)
{
name = newName;
UpdateThreadNameWithIndex();
}
/// Set the index of the thread name
/// The index of the thread with the same name
public void SetIndex(int newIndex)
{
index = newIndex;
UpdateThreadNameWithIndex();
}
/// Sets the index to indicate we want all threads (used for filtering against other ThreadIdentifiers)
public void SetAll()
{
SetIndex(kAll);
}
}
}