+const char*
+Kumu::Version()
+{
+ return PACKAGE_VERSION;
+}
+
+
+//------------------------------------------------------------------------------------------
+
+// Result_t Internals
+
+struct map_entry_t
+{
+ int rcode;
+ Kumu::Result_t* result;
+};
+
+
+// WIN32 does not init this in time for use with Result_t(...) below, so it is
+// now a pointer that Result_t(...) fills in when it needs it.
+static Kumu::Mutex* s_MapLock = 0;
+
+static ui32_t s_MapSize = 0;
+static const ui32_t MapMax = 2048;
+static struct map_entry_t s_ResultMap[MapMax];
+
+
+//
+const Kumu::Result_t&
+Kumu::Result_t::Find(int v)
+{
+ if ( v == 0 )
+ return RESULT_OK;
+
+ assert(s_MapLock);
+ AutoMutex L(*s_MapLock);
+
+ for ( ui32_t i = 0; i < s_MapSize; ++i )
+ {
+ if ( s_ResultMap[i].rcode == v )
+ return *s_ResultMap[i].result;
+ }
+
+ return RESULT_UNKNOWN;
+}
+
+//
+Kumu::Result_t
+Kumu::Result_t::Delete(int v)
+{
+ if ( v < -99 || v > 99 )
+ {
+ DefaultLogSink().Error("Cannot delete core result code: %ld\n", v);
+ return RESULT_FAIL;
+ }
+
+ assert(s_MapLock);
+ AutoMutex L(*s_MapLock);
+
+ for ( ui32_t i = 0; i < s_MapSize; ++i )
+ {
+ if ( s_ResultMap[i].rcode == v )
+ {
+ for ( ++i; i < s_MapSize; ++i )
+ s_ResultMap[i-1] = s_ResultMap[i];
+
+ --s_MapSize;
+ return RESULT_OK;
+ }
+ }
+
+ return RESULT_FALSE;
+}
+
+//
+unsigned int
+Kumu::Result_t::End()
+{
+ return s_MapSize;
+}
+
+//
+const Kumu::Result_t&
+Kumu::Result_t::Get(unsigned int i)
+{
+ return *s_ResultMap[i].result;
+}
+
+//
+Kumu::Result_t::Result_t(int v, const std::string& s, const std::string& l) : value(v), symbol(s), label(l)
+{
+ assert(!l.empty());
+ assert(!s.empty());
+
+ if ( v == 0 )
+ return;
+
+ // This may seem tricky, but it is certain that the static values declared in KM_error.h will
+ // be created (and thus this method will be called) before main(...) is called. It is not
+ // until then that threads could be created, thus the mutex will exist before multi-threaded
+ // access could occur.
+ if ( s_MapLock == 0 )
+ s_MapLock = new Kumu::Mutex;
+
+ assert(s_MapLock);
+ AutoMutex L(*s_MapLock);
+
+ for ( ui32_t i = 0; i < s_MapSize; ++i )
+ {
+ if ( s_ResultMap[i].rcode == v )
+ return;
+ }
+
+ assert(s_MapSize+1 < MapMax);
+
+ s_ResultMap[s_MapSize].rcode = v;
+ s_ResultMap[s_MapSize].result = this;
+ ++s_MapSize;
+
+ return;
+}
+
+
+Kumu::Result_t::Result_t(const Result_t& rhs)
+{
+ value = rhs.value;
+ symbol = rhs.symbol;
+ label = rhs.label;
+ message = rhs.message;
+}
+
+Kumu::Result_t::~Result_t() {}
+
+//
+const Kumu::Result_t&
+Kumu::Result_t::operator=(const Result_t& rhs)
+{
+ value = rhs.value;
+ symbol = rhs.symbol;
+ label = rhs.label;
+ message = rhs.message;
+ return *this;
+}
+
+//
+const Kumu::Result_t
+Kumu::Result_t::operator()(const std::string& message) const
+{
+ Result_t result = *this;
+ result.message = message;
+ return result;
+}
+
+static int const MESSAGE_BUF_MAX = 2048;
+
+//
+const Kumu::Result_t
+Kumu::Result_t::operator()(const int& line, const char* filename) const
+{
+ assert(filename);
+ char buf[MESSAGE_BUF_MAX];
+ snprintf(buf, MESSAGE_BUF_MAX-1, "%s, line %d", filename, line);
+
+ Result_t result = *this;
+ result.message = buf;
+ return result;
+}
+
+//
+const Kumu::Result_t
+Kumu::Result_t::operator()(const std::string& message, const int& line, const char* filename) const
+{
+ assert(filename);
+ char buf[MESSAGE_BUF_MAX];
+ snprintf(buf, MESSAGE_BUF_MAX-1, "%s, line %d", filename, line);
+
+ Result_t result = *this;
+ result.message = message + buf;
+ return result;
+}
+
+
+//------------------------------------------------------------------------------------------
+// DTrace internals
+
+static int s_DTraceSequence = 0;
+
+Kumu::DTrace_t::DTrace_t(const char* Label, Kumu::Result_t* Watch, int Line, const char* File)
+ : m_Label(Label), m_Watch(Watch), m_Line(Line), m_File(File)
+{
+ m_Sequence = s_DTraceSequence++;
+ DefaultLogSink().Debug("@enter %s[%d] (%s at %d)\n", m_Label, m_Sequence, m_File, m_Line);
+}
+
+Kumu::DTrace_t::~DTrace_t()
+{
+ if ( m_Watch != 0 )
+ DefaultLogSink().Debug("@exit %s[%d]: %s\n", m_Label, m_Sequence, m_Watch->Label());
+ else
+ DefaultLogSink().Debug("@exit %s[%d]\n", m_Label, m_Sequence);
+}
+