C++ Object实体类

*暂未完成,因为无尽BUG滚滚来。

好长时间没写完,一是能力不够,二是我还得给老板写WEB的代码。可是我不会WEB!js和PHP简直就是世界上最好的语言,因为它们能够让人更快地进入极乐世界。

让我写一个gm后台,他说好不好看无所谓,能用就行。于是,我就写了一个根据JSON数据自动生成页面的代码……哈哈,平均15分钟完成一个功能,简直不要太爽。懒人自然有懒办法。当然,之前写工具花费了大量时间。

回家还要玩激战2刷每日,还挺忙的……

好吧,我已经没有力气给自己吐槽了。。。所以,直接上代码吧,不废话了。

以下代码为C++代码。

简直通俗易懂。

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 ObjectInfo& Info();
102 
103     protected:
104         virtual void QualitiesCorrelation() = 0;
105 
106     public:
107         virtual void Destory();
108 
109     protected:
110         ObjectInfo m_info;
111         QualitiesManager m_qualManager;
112     };
113 
114     ///////////NullObject///////////
115     class NullObject: public LunaObject
116     {
117     private:
118         static NullObject* _instance;
119 
120     private:
121         NullObject();
122 
123     public:
124         friend class ObjectManager;
125         void Destory() override;
126         ObjectInfo& Info() override;
127 
128         static NullObject* const GetInstance();
129 
130     private:
131         void QualitiesCorrelation() override;
132     };
133 
134     extern NullObject NULL_OBJECT;
135     extern NullObject* const NULLPTR_OBJECT;
136 };

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     private:
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         PtrQuality ptrHeal = m_qualManager.AddQuality<QualHealth>();
16         PtrQuality ptrLifeIn = m_qualManager.AddQuality<QualLifeInstinct>();
17         PtrQuality ptrCour = m_qualManager.AddQuality<QualCourage>();
18 
19         ptrHeal->SetLimit(1000);
20         ptrHeal->SetValue(ptrHeal->GetLimit());
21 
22         ptrLifeIn->SetLimit(1000);
23         ptrHeal->SetValue(ptrLifeIn->GetLimit());
24 
25         ptrCour->SetLimit(1000);
26         ptrHeal->SetValue(static_cast<int32_t>(g_rn.GetRandNum() * ptrCour->GetLimit()));
27 
28         ptrLifeIn->AddPassive(ptrHeal);
29         return;
30     }
31 
32 }

_main.cpp(测试用主函数)

#include "Tilee.h"
#include "Location.h"
#include "stdinc.h"
#include "Random.h"
#include "Rational.h"

#include "ObjectManager.h"
#include "QualHealth.h"
#include "QualLifeInstinct.h"

#include "LunaObject.h"
#include "Human.h"

#include "UniqueID.h"

using namespace Lunacia;

struct Item 
{
    uint32_t weight;
    int id;
};

void func(const std::string& name, LunaObject* obj)
{
    Location lt1(1, 2, 3);
    obj->Info().Set<Location<int>>("location", lt1);

    Item item = {12, 23};
    obj->Info().Add<Item>("item", item);

    delete obj;
    obj = nullptr;
}

int main(void)
{
    ObjectManager om;
    int id = om.Add<Human>();
    LunaObject* obj = om.Find(id);

    int idd = obj->Info().Get<int>("id");
    std::cout << idd << std::endl;

    func("123", obj);

    //Location lt2 = obj->Info().Get<Location<int>>("location");
    //Item item = obj->Info().Get<Item>("item");

    /*RandomNorm rn(0.6, 0.15);
    for (size_t i = 0; i < 100; i++)
    {
        std::cout << rn.GetRandNum() << std::endl;
    }*/

    Human hA;
    LunaObject* lo = NULLPTR_OBJECT;
    lo = &hA;

    lo = NULLPTR_OBJECT;

    /*for (int i = 0; i < 30000; i++)
    {
        std::string str = std::to_string(UniqueID::Get());
        std::cout << str;
        for (int sk = 0; sk < 10 - str.size(); sk++)
        {
            std::cout << " ";
        }

        if (i % 8 == 0)
        {
        std::cout << std::endl;
        }
    }*/
    
    system("pause");
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/rkexy/p/9852444.html