Kigs Framework  Doc version 0.8
Open source multi purpose Rapid Application Development framework
maBuffer.h
1 #ifndef _MABUFFER_H
2 #define _MABUFFER_H
3 
5 #include "AsciiParserUtils.h"
6 #include "AttributeModifier.h"
7 #include "ModuleFileManager.h"
8 
9 // ****************************************
10 // * maBufferHeritage class
11 // * --------------------------------------
17 // ****************************************
18 template<unsigned int alignement, typename allocatedType, int notificationLevel>
19 class maBufferHeritage : public CoreModifiableAttributeData<SmartPointer<AlignedCoreRawBuffer<alignement, allocatedType>>>
20 {
21  template<int notiflevel>
23  using UnderlyingType = SmartPointer<AlignedCoreRawBuffer<alignement, allocatedType>>;
24 
25  using BaseType = CoreModifiableAttributeData<SmartPointer<AlignedCoreRawBuffer<alignement, allocatedType>>>;
26 
27  DECLARE_ATTRIBUTE_HERITAGE_NO_ASSIGN(maBufferHeritage, TemplateForPlacementNew, UnderlyingType, CoreModifiable::ATTRIBUTE_TYPE::COREBUFFER);
28 
29 
30  void InitData()
31  {
32  if (mValue.isNil())
33  {
34  mValue = OwningRawPtrToSmartPtr(new AlignedCoreRawBuffer<alignement, allocatedType>());
35  }
36  }
37 
38  void SetBufferFromString(const kstl::string& value)
39  {
40  InitData();
41 
42  // detect if value is a filename
43  if (value[0] == '#')
44  {
45  kstl::string filename = value.substr(1, value.length() - 1);
46 
47  auto ext = filename.substr(filename.find_last_of('.'));
48  u64 filelength = 0;
49  auto loaded = OwningRawPtrToSmartPtr(ModuleFileManager::LoadFile(filename.c_str(), filelength));
50  if (loaded)
51  {
52  if (ext == ".kbin")
53  {
54  CMSP& uncompressManager = KigsCore::GetSingleton("KXMLManager");
55  if (uncompressManager)
56  {
57  uncompressManager->SimpleCall("UncompressData", loaded.get(), mValue.get());
58  }
59  else
60  {
61  KIGS_ERROR("trying to uncompress kxml, but KXMLManager was not declared", 1);
62  }
63  }
64  else
65  mValue->SetBuffer(std::move(*loaded.get()));
66  }
67 
68  }
69  else
70  {
71  unsigned int readSize;
72  unsigned char* rawbuf = AsciiParserUtils::StringToBuffer(value, readSize);
73  mValue->SetBuffer(rawbuf, readSize);
74  }
75  }
76 
77 
78 public:
79 
81  maBufferHeritage(CoreModifiable& owner, bool isInitAttribute, KigsID ID, const kstl::string& value) : BaseType(owner, isInitAttribute, ID)
82  {
83  SetBufferFromString(value);
84  }
85 
87  virtual bool setValue(const char* value) override
88  {
89  kstl::string str(value);
90  return setValue(str);
91  }
92  virtual bool setValue(const kstl::string& value) override
93  {
94  if (this->isReadOnly())
95  return false;
96  SetBufferFromString(value);
97  DO_NOTIFICATION(notificationLevel);
98  return true;
99  }
100 
101  virtual bool setValue(void* value) override
102  {
103  if (this->isReadOnly())
104  return false;
105 
106  mValue = NonOwningRawPtrToSmartPtr((typename CurrentAttributeType::ValueType*)((CoreRawBuffer*)value));
107  DO_NOTIFICATION(notificationLevel);
108  return true;
109  }
110 
111 
113  virtual bool getValue(kstl::string& value) const override
114  {
115 
116  if (!mValue.isNil())
117  {
118  value = AsciiParserUtils::BufferToString((unsigned char*)mValue->buffer(), mValue->length());
119  return true;
120  }
121  value = "";
122  return true;
123  }
124 
125  virtual bool getValue(void*& value) const override { value = (CoreRawBuffer*)mValue.get(); return true; }
126 
127  auto& operator=(const kstl::string &value)
128  {
129  SetBufferFromString(value);
130  return *this;
131  }
132  auto& operator=(CoreRawBuffer* value)
133  {
134  InitData();
135  mValue->SetBuffer(*value);
136  return *this;
137  }
138 
139  char * buffer() const
140  {
141  return mValue.isNil() ? nullptr : mValue->buffer();
142  }
143  unsigned int length() const
144  {
145  return mValue.isNil() ? 0u :mValue->length();
146  }
147  void SetBuffer(void* buffer, unsigned int length, bool manageMemory = true)
148  {
149  InitData();
150  mValue->SetBuffer(buffer, length, manageMemory);
151  }
152  operator const CoreRawBuffer*() const { return mValue.get(); }
153 
154 
155 };
156 
157 
158 // ****************************************
159 // * maBuffer class
160 // * --------------------------------------
166 // ****************************************
167 
169 
170 
171 
172 #endif //_MABUFFER_H
maBufferHeritage::setValue
virtual bool setValue(const char *value) override
setValue overloads
Definition: maBuffer.h:87
CoreModifiableAttribute::isReadOnly
virtual bool isReadOnly()
Read only attributes cannot be modified with setValue.
Definition: CoreModifiableAttribute.h:276
AsciiParserUtils.h
maBufferHeritage
CoreModifiableAttributeData of reference with different notification level.
Definition: maBuffer.h:19
ModuleFileManager.h
maBufferHeritage::maBufferHeritage
maBufferHeritage(CoreModifiable &owner, bool isInitAttribute, KigsID ID, const kstl::string &value)
extra constructor with a string
Definition: maBuffer.h:81
maBufferHeritage::getValue
virtual bool getValue(kstl::string &value) const override
getValue overloads
Definition: maBuffer.h:113
CoreModifiable
Base class for Kigs framework objects. CoreModifiable class manage a list of attributes supporting re...
Definition: CoreModifiable.h:400
ModuleFileManager::LoadFile
static CoreRawBuffer * LoadFile(const char *pFilename, u64 &filelength, u64 startOffset=0, unsigned int trailing_zero=0)
Definition: ModuleFileManager.cpp:61
KigsCore::GetSingleton
static CMSP & GetSingleton(const KigsID &classname)
return an unique instance of the given class (Design Pattern Singleton)
Definition: Core.cpp:536
maBuffer
CoreModifiable managing a CoreRawBuffer.
CoreModifiableAttribute.h
Base template class for CoreModifiable attributes managing data.