Example #1
0
void Slice::ChecksumVisitor::visitEnum(const EnumPtr& p)
{
  if (p->isLocal()) {
    return;
  }

  ostringstream ostr;

  ostr << "enum " << p->name() << endl;

  EnumeratorList enums = p->getEnumerators();
  for (EnumeratorList::iterator q = enums.begin(); q != enums.end(); ++q) {
    ostr << (*q)->name() << endl;
  }

  updateMap(p->scoped(), ostr.str());
}
Example #2
0
void
Slice::Ruby::CodeVisitor::visitEnum(const EnumPtr& p)
{
    string scoped = p->scoped();
    string name = fixIdent(p->name(), IdentToUpper);
    EnumeratorList enums = p->getEnumerators();
    EnumeratorList::iterator q;
    int i;

    _out << sp << nl << "if not defined?(" << getAbsolute(p, IdentToUpper) << ')';
    _out.inc();
    _out << nl << "class " << name;
    _out.inc();
    _out << nl << "include Comparable";
    _out << sp << nl << "def initialize(val)";
    _out.inc();
    {
        ostringstream assertion;
        assertion << "fail(\"invalid value #{val} for " << name << "\") unless(val >= 0 and val < "
                  << enums.size() << ')';
        _out << nl << assertion.str();
    }
    _out << nl << "@val = val";
    _out.dec();
    _out << nl << "end";

    //
    // from_int
    //
    {
        _out << sp << nl << "def " << name << ".from_int(val)";
        ostringstream sz;
        sz << enums.size() - 1;
        _out.inc();
        _out << nl << "raise IndexError, \"#{val} is out of range 0.." << sz.str() << "\" if(val < 0 || val > "
             << sz.str() << ')';
        _out << nl << "@@_values[val]";
        _out.dec();
        _out << nl << "end";
    }

    //
    // to_s
    //
    _out << sp << nl << "def to_s";
    _out.inc();
    _out << nl << "@@_names[@val]";
    _out.dec();
    _out << nl << "end";

    //
    // to_i
    //
    _out << sp << nl << "def to_i";
    _out.inc();
    _out << nl << "@val";
    _out.dec();
    _out << nl << "end";

    //
    // <=>
    //
    _out << sp << nl << "def <=>(other)";
    _out.inc();
    _out << nl << "other.is_a?(" << name << ") or raise ArgumentError, \"value must be a " << name << "\"";
    _out << nl << "@val <=> other.to_i";
    _out.dec();
    _out << nl << "end";

    //
    // hash
    //
    _out << sp << nl << "def hash";
    _out.inc();
    _out << nl << "@val.hash";
    _out.dec();
    _out << nl << "end";

    //
    // inspect
    //
    _out << sp << nl << "def inspect";
    _out.inc();
    _out << nl << "@@_names[@val] + \"(#{@val})\"";
    _out.dec();
    _out << nl << "end";

    //
    // each
    //
    _out << sp << nl << "def " << name << ".each(&block)";
    _out.inc();
    _out << nl << "@@_values.each(&block)";
    _out.dec();
    _out << nl << "end";

    _out << sp << nl << "@@_names = [";
    for(q = enums.begin(); q != enums.end(); ++q)
    {
        if(q != enums.begin())
        {
            _out << ", ";
        }
        _out << "'" << (*q)->name() << "'";
    }
    _out << ']';

    _out << nl << "@@_values = [";
    for(EnumeratorList::size_type j = 0; j < enums.size(); ++j)
    {
        if(j > 0)
        {
            _out << ", ";
        }
        ostringstream idx;
        idx << j;
        _out << name << ".new(" << idx.str() << ')';
    }
    _out << ']';

    //
    // Constant for each enumerator.
    //
    _out << sp;
    for(q = enums.begin(), i = 0; q != enums.end(); ++q, ++i)
    {
        ostringstream idx;
        idx << i;
        _out << nl << fixIdent((*q)->name(), IdentToUpper) << " = @@_values[" << idx.str() << "]";
    }

    _out << sp << nl << "private_class_method :new";

    _out.dec();
    _out << nl << "end"; // End of class.

    //
    // Emit the type information.
    //
    _out << sp << nl << "T_" << name << " = ::Ice::__defineEnum('" << scoped << "', " << name << ", [";
    for(q = enums.begin(); q != enums.end(); ++q)
    {
        if(q != enums.begin())
        {
            _out << ", ";
        }
        _out << name << "::" << fixIdent((*q)->name(), IdentToUpper);
    }
    _out << "])";

    _out.dec();
    _out << nl << "end"; // if not defined?()
}