C++ Object實體類


*暫未完成,因為無盡BUG滾滾來。

好長時間沒寫完,一是能力不夠,二是我還得給老板寫WEB的代碼。可是我不會WEB!js和PHP簡直就是世界上最好的語言,因為它們能夠讓人更快地進入極樂世界。

讓我寫一個gm后台,他說好不好看無所謂,能用就行。於是,我就寫了一個根據JSON數據自動生成頁面的代碼……哈哈,平均15分鍾完成一個功能,簡直不要太爽。懶人自然有懶辦法。當然,之前寫工具花費了大量時間。

回家還要玩激戰2刷每日,還挺忙的……

好吧,我已經沒有力氣給自己吐槽了。。。所以,直接上代碼吧,不廢話了。

 

以下代碼為C++代碼。

簡直通俗易懂。

 

接下來(預計實現):

1.結構改造:在代碼中定義一個對象的屬性關聯以及擁有屬性是不太方便的,應該將這種關系和數據放置於配置表中。甚至是接下來的事件類和故事(story)類也應當如此。

1.1 首先,需要定義基本動作,

1.2 定義動作指令

1.3 需要一個虛擬機,基於棧的或者是基於寄存器的。還是棧虛擬機實現更簡單一點,盡管效率稍差。

1.3.1  圖形界面或者腳本程序。前者對於設計師來說更加友好。

1.4 協議。應當將虛擬機生成的字節碼翻譯成數據發送給主程序處理。

1.5 存儲。生成的字節碼應該被存儲在配置表或者數據庫中。

1.6 處理。主程序應該有足夠通用、健壯和效率的算法來處理這個字節碼。

看來這是一個大工程。

 

2.集中測試(2018.11.1 19:25)

LunaObject.h

  1 #pragma once
  2 
  3 #include "stdinc.h"
  4 #include "Location.h"
  5 #include "QualitiesManager.h"
  6 #include "QualInc.h"
  7 
  8 namespace Lunacia
  9 {
 10     class ObjectInfo final
 11     {
 12     private:
 13         //info name: value pointer
 14         std::unordered_map<std::string, void*> _infos;
 15         std::unordered_map<std::string, std::function<void(const std::string&)>> _destory;
 16 
 17     public:
 18         ObjectInfo();
 19         ObjectInfo(std::unordered_map<std::string, void*>& infos);
 20         ~ObjectInfo();
 21 
 22     public:
 23         template<typename T>
 24         T& Get(const std::string& name)
 25         {
 26             if (!IsExist(name))
 27             {
 28                 ErrorThrow("The data is not exist what name is " + name);
 29             }
 30 
 31             return *(static_cast<T*>(_infos.find(name)->second));
 32         }
 33 
 34         template<typename T>
 35         void Set(const std::string& name, T& val)
 36         {
 37             auto itFound = _infos.find(name);
 38             void* pFound = nullptr;
 39             if (itFound == _infos.end())
 40             {
 41                 Add(name, val);
 42                 return;
 43             }
 44 
 45             pFound = itFound->second;
 46             T* pVal = static_cast<T*>(pFound);
 47             *pVal = val;
 48         }
 49 
 50         template<typename T>
 51         void Replace(const std::string& name, const T& value)
 52         {
 53             Delete<T>(name);
 54             Add(name, value);
 55         }
 56 
 57         template<typename T>
 58         bool Add(const std::string& name, const T& value)
 59         {
 60             T* pValue = new T();
 61             *pValue = value;
 62 
 63             std::function<void(const std::string&)> destoryFunc = [this](const std::string& n)->void
 64             {
 65                 this->Delete<T>(n);
 66             };
 67 
 68             _destory.insert(std::make_pair(name, destoryFunc));
 69             return _infos.insert(std::make_pair(name, static_cast<void*>(pValue))).second;
 70         }
 71 
 72         template<typename T>
 73         bool Delete(const std::string& name)
 74         {
 75             auto itFound = _infos.find(name);
 76             if (itFound == _infos.end())
 77             {
 78                 return false;
 79             }
 80             void* pVal = itFound->second;
 81 
 82             T* pDel = static_cast<T*>(pVal);
 83             delete pDel;
 84             pVal = nullptr;
 85 
 86             _destory.erase(name);
 87             return _infos.erase(name) == 1;
 88         }
 89 
 90         bool IsExist(const std::string& name) const;
 91     };
 92 
 93     //////////LunaObject//////////
 94     class LunaObject
 95     {
 96     public:
 97         LunaObject();
 98         virtual ~LunaObject();
 99 
100     public:
101         virtual void QualitiesCorrelation() = 0;
102 
103     public:
104         virtual ObjectInfo& Info();
105         virtual void Destory();
106 
107 #ifdef __Debug
108     public:
109 #else
110     protected:
111 #endif // __Debug
112         ObjectInfo m_info;
113         QualitiesManager m_qualManager;
114     };
115 
116     ///////////NullObject///////////
117     class NullObject: public LunaObject
118     {
119     private:
120         static NullObject* _instance;
121 
122     private:
123         NullObject();
124 
125     public:
126         friend class ObjectManager;
127         void Destory() override;
128         ObjectInfo& Info() override;
129 
130         static NullObject* const GetInstance();
131 
132     private:
133         void QualitiesCorrelation() override;
134     };
135 
136     extern NullObject NULL_OBJECT;
137     extern NullObject* const NULLPTR_OBJECT;
138 };
139 
140 //#define ADDOBJ(className)   g_ObjectManager.AddObject(#className,new className)

 

LunaObject.cpp

 1 #include "LunaObject.h"
 2 
 3 namespace Lunacia
 4 {
 5     LunaObject::LunaObject()
 6     {
 7     }
 8 
 9     LunaObject::~LunaObject()
10     {
11         Destory();
12     }
13 
14     ObjectInfo & LunaObject::Info()
15     {
16         return m_info;
17     }
18 
19     void LunaObject::Destory()
20     {
21         m_qualManager.Clear();
22     }
23 
24     /**
25     *    @class: NullObject.
26     *  @lazy initializate: false.
27     *  @thread safe: true.
28     *
29     */
30     NullObject* NullObject::_instance = new NullObject();
31 
32     static NullObject* const NULLPTR_OBJECT = NullObject::GetInstance();
33     static NullObject NULL_OBJECT = *NULLPTR_OBJECT;
34 
35     NullObject::NullObject()
36     {
37     }
38 
39     NullObject* const NullObject::GetInstance()
40     {
41         return _instance;
42     }
43 
44     void NullObject::Destory() 
45     {
46         ErrorThrow("Destory Function: It is null object!");
47     }
48 
49     ObjectInfo & NullObject::Info()
50     {
51         ErrorThrow("GetObjectInfo Function: It is null object!");
52         return m_info;
53     }
54 
55     void NullObject::QualitiesCorrelation() 
56     {
57         ErrorThrow("QualitiesCorrelation Function: It is null object!");
58     }
59 
60     //////////////ObjectInfo//////////////
61     ObjectInfo::ObjectInfo()
62     {
63     }
64 
65     ObjectInfo::ObjectInfo(std::unordered_map<std::string, void*>& infos):
66         _infos(infos)
67     {
68     }
69 
70     ObjectInfo::~ObjectInfo()
71     {
72         std::unordered_map<std::string, void*>::iterator it;
73         while (!_infos.empty())
74         {
75             it = _infos.begin();
76 
77             const std::string& name = it->first;
78             (_destory[name])(name);
79         } 
80 
81         _infos.clear();
82         _destory.clear();
83     }
84 
85     bool ObjectInfo::IsExist(const std::string & name) const
86     {
87         if (_infos.find(name) == _infos.end())
88         {
89             return false;
90         }
91         return true;
92     }
93 };

 

ObjectManager.h

 1 #pragma once
 2 
 3 #include "LunaObject.h"
 4 #include "UniqueID.h"
 5 
 6 namespace Lunacia
 7 {
 8     class ObjectManager final
 9     {
10     public:
11         ObjectManager();
12         ~ObjectManager();
13 
14         void Clear();
15 
16     public:
17         int Add(LunaObject * obj);
18 
19         template<class T, typename  std::enable_if <std::is_base_of<LunaObject, T>::value, T> ::type * = nullptr >
20         int Add();
21 
22         template<class T, typename  std::enable_if <std::is_base_of<LunaObject, T>::value, T> ::type * = nullptr >
23         LunaObject* Create();
24 
25         void Remove(int id);
26         LunaObject*const Find(int id);
27         const LunaObject*const Find(int id) const;
28 
29     private:
30         std::map<int, LunaObject*> m_objects;
31     };
32     ///////////////
33 
34     template<class T, typename  std::enable_if <std::is_base_of<LunaObject, T>::value, T> ::type *>
35     inline int ObjectManager::Add()
36     {
37         LunaObject* pObj = Create<T>();
38         return Add(pObj);
39     }
40 
41     template<class T, typename  std::enable_if <std::is_base_of<LunaObject, T>::value, T> ::type *>
42     inline LunaObject * ObjectManager::Create()
43     {
44         return new T();
45     }
46 
47 };

 

ObjectManager.cpp

#include "ObjectManager.h"

namespace Lunacia
{
    ObjectManager::ObjectManager()
    {
    }
    
    ObjectManager::~ObjectManager()
    {
    }

    void ObjectManager::Clear()
    {
        for (auto& itEach : m_objects)
        {
            LunaObject* lo = itEach.second;
            lo->Destory();

            delete lo;
            lo = NULLPTR_OBJECT;
        }
    }

    int ObjectManager::Add(LunaObject * obj)
    {
        int id = static_cast<int>(UniqueID::Get());
        obj->Info().Set("id", id);

        return m_objects.insert(std::make_pair(id, obj)).second ? id : -1;
    }

    void ObjectManager::Remove(int id)
    {
        auto itFound = m_objects.find(id);
        if (itFound != m_objects.end())
        {
            return;
        }
        LunaObject* & pObjFound = itFound->second;
        pObjFound->Destory();

        delete pObjFound;
        pObjFound = NULLPTR_OBJECT;
    }

    LunaObject * const ObjectManager::Find(int id)
    {
        const ObjectManager* pSelf = this;

        return const_cast<LunaObject * const>(pSelf->Find(id));
    }

    const LunaObject * const ObjectManager::Find(int id) const
    {
        auto itFound = m_objects.find(id);
        if (itFound == m_objects.end())
        {
            return NULLPTR_OBJECT;
        }
        return itFound->second;
    }
};

 

Human.h(測試用) (將會被宏代替(一句宏生成一個類))

 1 #pragma once
 2 #include "LunaObject.h"
 3 
 4 namespace Lunacia
 5 {
 6     class Human : public LunaObject
 7     {
 8     public:
 9         Human();
10         ~Human();
11 
12     public:
13         void QualitiesCorrelation() override;
14     };
15 };

 

Human.cpp(測試用)

 1 #include "Human.h"
 2 
 3 namespace Lunacia
 4 {
 5     Human::Human()
 6     {
 7     }
 8 
 9     Human::~Human()
10     {
11     }
12 
13     void Human::QualitiesCorrelation()
14     {
15         //TODO: rebuild at config file.
16         PtrQuality ptrHeal = m_qualManager.AddQuality<QualHealth>();
17         PtrQuality ptrLifeIn = m_qualManager.AddQuality<QualLifeInstinct>();
18         PtrQuality ptrCour = m_qualManager.AddQuality<QualCourage>();
19 
20         ptrHeal->SetLimit(1000);
21         ptrHeal->SetValue(ptrHeal->GetLimit());
22 
23         ptrLifeIn->SetLimit(1000);
24         ptrHeal->SetValue(ptrLifeIn->GetLimit());
25 
26         ptrCour->SetLimit(1000);
27         ptrHeal->SetValue(ptrCour->GetLimit());
28 
29         ptrLifeIn->AddPassive(ptrHeal);
30         ptrHeal->AddPassive(ptrCour);
31 
32         return;
33     }
34 
35 }

 

UniqueID.h(初版,用於測試)

 1 #pragma once
 2 #include "stdinc.h"
 3 
 4 namespace Lunacia
 5 {
 6     //Unique ID Local, not global.
 7     class UniqueID final
 8     {
 9     public:
10         static int64_t Get(int8_t suffix_8bit = 0);
11         static void Load(void* pData);
12 
13     private:
14         static void ReInitPool();
15         
16     private:
17         static const size_t _count = 5000;    //TODO:  Rebuild it.    //The larger the number, the more uniform the distribution.
18         static int _curSize;
19         static int _curMax;    //TODO:  Rebuild it.
20         static std::array<int64_t, _count> _idsPool;
21     };
22 };

 

UniqueID.cpp

 1 #include "UniqueID.h"
 2 #include "Random.h"
 3 
 4 namespace Lunacia
 5 {
 6     int UniqueID::_curMax = 100;
 7     std::array<int64_t, UniqueID::_count> UniqueID::_idsPool;
 8     int UniqueID::_curSize = UniqueID::_count;
 9 
10     int64_t UniqueID::Get(int8_t suffix_8bit)
11     {    
12         if (_curSize >= _idsPool.size() - 1)
13         {
14             ReInitPool();
15         }
16 
17         return (_idsPool[_curSize++] << 8) | suffix_8bit;
18     }
19 
20     void UniqueID::Load(void* pData)
21     {
22     
23     }
24 
25     void UniqueID::ReInitPool()
26     {
27         int curMin = _curMax;
28         _curMax += _count;
29         for (int i = 0; i < _count; ++i)
30         {
31             _idsPool[i] = i + curMin;
32         }
33 
34         for (int n = 0; n < _count - 1; ++n)
35         {
36             uint64_t ran = RandomAvg::GetRandNum(n + 1, _count);
37             std::swap(_idsPool[n], _idsPool[ran]);
38         }
39 
40         _curSize = 0;
41     }
42 };

 

 Encounter.h(測試類)

 1 #include "stdinc.h"
 2 #include "QualDefine.h"
 3 
 4 #pragma once
 5 
 6 namespace Lunacia
 7 {
 8     class Encounter
 9     {
10     public:
11         struct QualInfluence 
12         {
13             QualityType type;
14             int32_t influence;
15         };
16 
17     public:
18         Encounter();
19         ~Encounter();
20 
21     private:
22         QualityType GetRandomType() const;
23         int32_t GetRamdomInfluence() const;
24 
25     public:
26         void GetRandomInfluence(QualInfluence& __out res) const;
27     };
28 
29 };

Encounter.cpp
 1 #include <bitset>
 2 
 3 #include "Encounter.h"
 4 #include "Random.h"
 5 
 6 namespace Lunacia
 7 {
 8 
 9     Encounter::Encounter()
10     {
11         
12     }
13 
14     Encounter::~Encounter()
15     {
16 
17     }
18 
19     QualityType Encounter::GetRandomType() const
20     {
21         int rand = static_cast<int>(RandomAvg::GetRandNum(1, static_cast<uint32_t>(QualityType::__QualityType_MAX)));
22         return static_cast<QualityType>(rand);
23     }
24 
25     int32_t Encounter::GetRamdomInfluence() const
26     {
27 
28         return g_rn.GetRandNum<int32_t>() * static_cast<int>(std::pow(-1, RandomAvg::GetRandNum() % 2));
29     }
30 
31     void Encounter::GetRandomInfluence(QualInfluence& res) const
32     {
33         res.type = GetRandomType();
34         res.influence = GetRamdomInfluence();
35     }
36 };

 

 

_main.cpp(測試用主函數)

  1 #include "Tilee.h"
  2 #include "Location.h"
  3 #include "stdinc.h"
  4 #include "Random.h"
  5 #include "Rational.h"
  6 
  7 #include "ObjectManager.h"
  8 #include "QualHealth.h"
  9 #include "QualLifeInstinct.h"
 10 
 11 #include "LunaObject.h"
 12 #include "Human.h"
 13 #include "UniqueID.h"
 14 #include "UClock.h"
 15 
 16 #include "Encounter.h"
 17 
 18 using namespace Lunacia;
 19 
 20 ObjectManager* pOm = new ObjectManager();
 21 std::vector<LunaObject*> g_objs;
 22 
 23 RandomItem<LunaObject*> * g_ri;
 24 Encounter g_ec;
 25 
 26 const std::function<uint64_t(const LunaObject* const &)>& GetWeightFunc = [](const LunaObject*const & obj) -> uint64_t
 27 {
 28     const Quality* const res = obj->m_qualManager.GetQuality(QualityType::HEALTH);
 29     if (res == nullptr)
 30     {
 31         return 0l;
 32     }
 33 
 34     return res->GetValue();
 35 };
 36 
 37 void loop() 
 38 {
 39     Encounter::QualInfluence eqi;
 40     g_ec.GetRandomInfluence(eqi);
 41 
 42     LunaObject* const pObj = g_ri->GetRandItem();
 43     Quality* const res = pObj->m_qualManager.FindQuality(eqi.type);
 44 
 45     if (res == nullptr)
 46     {
 47         return;
 48     }
 49     res->AddValue(eqi.influence);
 50 
 51     std::cout<<"ID: " << pObj->Info().Get<int>("id") << "    " << static_cast<int64_t>(eqi.type) << " :  "<< eqi.influence << std::endl;
 52 }
 53 
 54 void init(int count)
 55 {
 56     for (size_t i = 0; i < count; i++)
 57     {
 58         LunaObject* h = pOm->Create<Human>();
 59         h->QualitiesCorrelation();
 60 
 61         int id = pOm->Add(h);
 62 
 63         g_objs.push_back(h);
 64     }
 65 
 66     g_ri = new RandomItem<LunaObject*>(g_objs, GetWeightFunc);
 67 }
 68 
 69 int main(void)
 70 {    
 71     init(100);
 72     const int64_t RateBase = 10000;
 73 
 74     int64_t Rate = RateBase / 100;
 75     UClock uck;
 76     
 77     int64_t pre = GetCurrentTime();
 78     int64_t e = 0;
 79     int64_t cur = 0;
 80     uint64_t i = 0;
 81 
 82     //TEST: Loop
 83     while (true) 
 84     {
 85         cur = GetCurrentTime();
 86         e += cur - pre;
 87         pre = cur;
 88 
 89         while (e >= Rate)
 90         {
 91             loop();
 92             e -= Rate;
 93             i++;
 94             //std::cout << e << std::endl;
 95         }
 96 
 97         if (i >= 10000)
 98         {
 99             i = 0;
100             delete g_ri;
101             g_ri = new RandomItem<LunaObject*>(g_objs, GetWeightFunc);
102             system("cls");
103         }
104     }
105 
106     system("pause");
107     return 0;
108 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM