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

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

unix.superglobalmegacorp.com

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