Ivan Mahonin 6e237b
#pragma once
Ivan Mahonin 6e237b
Ivan Mahonin 6e237b
#ifndef TSTRINGID_INCLUDED
Ivan Mahonin 6e237b
#define TSTRINGID_INCLUDED
Ivan Mahonin 6e237b
Ivan Mahonin 6e237b
#include <tcommon.h>
Ivan Mahonin 6e237b
Ivan Mahonin 6e237b
#include <string>
Ivan Mahonin 6e237b
#include <vector>
Ivan Mahonin 6e237b
#include <map>
Ivan Mahonin 6e237b
Ivan Mahonin 6e237b
#undef DVAPI
Ivan Mahonin 6e237b
#undef DVVAR
Ivan Mahonin 6e237b
#ifdef TSTRINGID_EXPORTS
Ivan Mahonin 6e237b
#define DVAPI DV_EXPORT_API
Ivan Mahonin 6e237b
#define DVVAR DV_EXPORT_VAR
Ivan Mahonin 6e237b
#else
Ivan Mahonin 6e237b
#define DVAPI DV_IMPORT_API
Ivan Mahonin 6e237b
#define DVVAR DV_IMPORT_VAR
Ivan Mahonin 6e237b
#endif
Ivan Mahonin 6e237b
Ivan Mahonin 6e237b
//-------------------------------------------------------------------
Ivan Mahonin 6e237b
Ivan Mahonin 6e237b
class DVAPI TStringId {
Ivan Mahonin 6e237b
public:
Ivan Mahonin 6e237b
  typedef std::map<std::string, int> Map;
Ivan Mahonin 6e237b
  typedef Map::iterator Iterator;
Ivan Mahonin 6e237b
Ivan Mahonin 6e237b
  struct StaticData;
Ivan Mahonin 6e237b
  static const Iterator& none();
Ivan Mahonin 6e237b
  static Iterator genIter(const std::string &str);
Ivan Mahonin 6e237b
  static Iterator findIter(int id);
Ivan Mahonin 6e237b
  static Iterator findIter(const std::string &str);
Ivan Mahonin 6e237b
Ivan Mahonin 6e237b
private:
Ivan Mahonin 6e237b
  Iterator iter;
Ivan Mahonin 6e237b
Ivan Mahonin 6e237b
  inline explicit TStringId(const Iterator &iter):
Ivan Mahonin 6e237b
    iter(iter) { }
Ivan Mahonin 6e237b
Ivan Mahonin 6e237b
public:
Ivan Mahonin 6e237b
  inline TStringId():
Ivan Mahonin 6e237b
    iter(none()) { }
Ivan Mahonin 6e237b
  inline explicit TStringId(const std::string &str):
Ivan Mahonin 6e237b
    iter(genIter(str)) { }
Ivan Mahonin 6e237b
  inline void reset()
Ivan Mahonin 6e237b
    { iter = none(); }
Ivan Mahonin 6e237b
  inline void set(const std::string &str)
Ivan Mahonin 6e237b
    { if (iter->first != str) iter = genIter(str); }
Ivan Mahonin 6e237b
Ivan Mahonin 6e237b
  inline int id() const
Ivan Mahonin 6e237b
    { return iter->second; }
Ivan Mahonin 6e237b
  inline const std::string& str() const
Ivan Mahonin 6e237b
    { return iter->first; }
Ivan Mahonin 6e237b
Ivan Mahonin 6e237b
  inline operator bool () const
Ivan Mahonin 16421e
    { return id() != 0; }
Ivan Mahonin 6e237b
  inline bool operator== (const TStringId &other) const
Ivan Mahonin 6e237b
    { return id() == other.id(); }
Ivan Mahonin 6e237b
  inline bool operator!= (const TStringId &other) const
Ivan Mahonin 6e237b
    { return id() != other.id(); }
Ivan Mahonin 6e237b
  inline bool operator< (const TStringId &other) const
Ivan Mahonin 6e237b
    { return id() < other.id(); }
Ivan Mahonin 6e237b
Ivan Mahonin 6e237b
  inline static TStringId find(int id)
Ivan Mahonin 6e237b
    { return TStringId(findIter(id)); }
Ivan Mahonin 6e237b
  inline static TStringId find(const std::string &str)
Ivan Mahonin 6e237b
    { return TStringId(findIter(str)); }
Ivan Mahonin 6e237b
};
Ivan Mahonin 6e237b
Ivan Mahonin 6e237b
#endif