Annotation of nono/lib/object.cpp, revision 1.1.1.9

1.1       root        1: //
                      2: // nono
1.1.1.3   root        3: // Copyright (C) 2020 nono project
                      4: // Licensed under nono-license.txt
1.1       root        5: //
                      6: 
1.1.1.7   root        7: //
1.1.1.8   root        8: // 基本オブジェクト
1.1.1.7   root        9: //
1.1.1.8   root       10: // オブジェクトはログ出力機能 (とそのための自身のオブジェクト名) を持つ。
                     11: 
                     12: #include "object.h"
                     13: #include "logger.h"
                     14: #include "mainapp.h"
                     15: 
1.1       root       16: // コンストラクタ
1.1.1.9 ! root       17: Object::Object(int objid_)
1.1       root       18: {
1.1.1.9 ! root       19:        objid = objid_;
        !            20:        assertmsg(OBJ_NONE <= objid && objid < idname.size(),
        !            21:                "objid=%d, size=%zd", objid, idname.size());
        !            22: 
        !            23:        // オブジェクト名(ログ表示名)は ID から引く。
        !            24:        objname = idname[objid];
1.1.1.7   root       25: 
                     26:        // コンストラクタで指定した場合はエイリアスにも登録しておく。
1.1.1.9 ! root       27:        // ただし OBJ_NONE ならログを出すことも想定していないはずなので
        !            28:        // エイリアスも追加しない。
        !            29:        if (objid != OBJ_NONE) {
        !            30:                AddAlias(objname);
        !            31:        }
1.1       root       32: 
                     33:        // 自分自身をリストに繋ぐ
1.1.1.9 ! root       34:        gMainApp.RegistObject(this);
1.1       root       35: }
                     36: 
                     37: // デストラクタ
                     38: Object::~Object()
                     39: {
1.1.1.7   root       40:        // 自分自身をリストから削除する
1.1.1.9 ! root       41:        gMainApp.UnregistObject(this);
1.1.1.8   root       42: }
                     43: 
                     44: // ログレベルを設定する。
                     45: void
                     46: Object::SetLogLevel(int loglevel_)
                     47: {
                     48:        loglevel = loglevel_;
1.1       root       49: }
                     50: 
                     51: // メッセージ表示
                     52: // 常に表示 (あらかじめ loglevel の評価をした後で呼び出す用)
                     53: void
1.1.1.6   root       54: Object::putmsgn(const char *fmt, ...) const
1.1       root       55: {
1.1.1.2   root       56:        char buf[1024];
1.1.1.6   root       57:        va_list ap;
1.1.1.8   root       58:        int len;
1.1       root       59: 
1.1.1.7   root       60:        // 名前だけ生表示されても分かりづらいので括弧でもつけておく
                     61:        len = snprintf(buf, sizeof(buf), "(%s) ", GetName().c_str());
1.1.1.8   root       62: 
1.1.1.6   root       63:        va_start(ap, fmt);
1.1.1.2   root       64:        vsnprintf(buf + len, sizeof(buf) - len, fmt, ap);
1.1.1.6   root       65:        va_end(ap);
                     66: 
1.1.1.8   root       67:        WriteLog(buf);
1.1       root       68: }
                     69: 
1.1.1.8   root       70: // ログ表示 (継承側で用意すること)
1.1       root       71: void
1.1.1.6   root       72: Object::putlogn(const char *fmt, ...) const
1.1       root       73: {
1.1.1.8   root       74:        PANIC("Object::putlogn called");
                     75: }
1.1       root       76: 
1.1.1.8   root       77: // ロガーに出力する共通部分
                     78: void
                     79: Object::WriteLog(const char *buf) const
                     80: {
                     81:        logger->Write(buf);
1.1       root       82: }
1.1.1.9 ! root       83: 
        !            84: // このオブジェクトの ID を文字列にして返す
        !            85: const char *
        !            86: Object::GetIdStr() const
        !            87: {
        !            88:        return GetIdStr(GetId());
        !            89: }
        !            90: 
        !            91: // 指定のオブジェクト ID を文字列にして返す
        !            92: /*static*/ const char *
        !            93: Object::GetIdStr(int target_id)
        !            94: {
        !            95:        if (OBJ_NONE < target_id && target_id < idname.size()) {
        !            96:                return idname[target_id];
        !            97:        }
        !            98:        return "unknownID?";
        !            99: }
        !           100: 
        !           101: // 指定のオブジェクト ID を持つオブジェクトを返す。
        !           102: /*static*/ Object *
        !           103: Object::GetObject(int target_id)
        !           104: {
        !           105:        return gMainApp.GetObject(target_id);
        !           106: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.