Kigs Framework Introduction (3/8) - Attributes

 
Rate me!
5.00 (2 votes)
 
22 Feb 2020MIT
A multi-purpose, cross-platform, free and Open Source C++ framework. This article will focus on CoreModifiable attributes.
This article will focus on CoreModifiable attributes of the Kigs framework; a lightweight, fast, scalable framework. We will go over: Declaration, Types, Dynamic Attributes, Access, Attribute Modifiers, Owner Notification, and Serialization.

Kigs Logo

Table of Contents

Introduction

In the previous articles of this series (here and here), we overviewed the Kigs framework and detailed CoreModifiable class.

This article will focus on CoreModifiable attributes.

Declaration

CoreModifiable attributes can be defined at compile time as member variable of a CoreModifiable or at runtime as dynamic attributes.

When defined as member variable, two definitions/declarations are possible:

Modern C++ Style

Using one of the helper macros BASE_ATTRIBUTE or INIT_ATTIBUTE:

// declare m_val member variable as a integer CoreModifiable attribute with 
// name "IntValue" and with default value 12
maInt m_val=BASE_ATTRIBUTE(IntValue,12);
// declare m_fval member variable as a float CoreModifiable attribute with 
// name "FloatValue" and with default value -1.0f
// once owning instance is initialized, m_fval is read only ( init attribute )
maFloat m_fval=INIT_ATTRIBUTE(FloatValue,-1.0f);

Classic Constructor Init

In class declaration:

// declare m_val member variable as a integer CoreModifiable attribute
maInt m_val;
// declare m_fval member variable as a float CoreModifiable attribute
maFloat m_fval;

In class constructor:

IMPLEMENT_CONSTRUCTOR(SimpleClass)
// attribute is set as init attribute : it will be set as readonly 
// after instance was initialized
, m_val(*this,true,"IntValue",12)                    
// attribute is not set as init attribute : it will be read/write enabled
, m_fval(* this, false, "FloatValue",-1.0f)
{
    
}

Owning Class Access

The owning class can use CoreModifiable attributes directly as a member variable:

// change m_val value 
// (read only and attribute modifiers are not used when direct access is done)
m_val+=5;
// use m_val directly without getValue
float result = 5+m_val;

Types

Basic Types

Numeric Types

  • maChar: manage signed 8 bits integer
  • maShort: manage signed 16 bits integer
  • maInt: manage signed 32 bits integer
  • maLong: manage signed 64 bits integer
  • maUChar: manage unsigned 8 bits integer
  • maUShort: manage unsigned 16 bits integer
  • maUInt: manage unsigned 32 bits integer
  • maULong: manage unsigned 64 bits integer
  • maFloat: manage float value
  • maDouble: manage double value

String Types

  • maString: manage std::string value
  • maUSString: manage UTF16 string value

Other Basic Types

  • maBool: manage bool value
  • maEnum: manage an énumération. maEmum needs template parameter for the possible enumeration value count. Here is an example of maEnum declaration:
// m_RequestType enum can take values "GET", "POST", "PUT", "DELETE"
maEnum<4> m_RequestType = BASE_ATTRIBUTE(Type, "GET", "POST", "PUT", "DELETE");

Advanced Types

Array / Vector Types

  • maVect2DF: manage 2d floating point value vector
  • maVect2DI: manage 2d integer (32 bits) value vector
  • maVect3DF: manage 3d floating point value vector
  • maVect3DI: manage 3d integer (32 bits) value vector
  • maVect4DF: manage 4d floating point value vector
  • maVect16DF: manage 16 floating point value vector ( <=> 4x4 matrix )
  • maMatrix22DF: manage 2x2 floating point matrix
  • maMatrix33DF: manage 3x3 floating point matrix

Reference

  • maReference: manage a reference on a CoreModifiable instance.

A reference is generally initialized with a string giving a path to the wanted instance. Basic path can have this form:
"classtype:classname".
maReference does not change reference counting of the referenced object.

Owning class can retrieve pointer on the referenced instance with a cast on CoreModifiable:

CoreModifiable* other = (CoreModifiable*)m_Ref;

getValue can also be used:

CoreModifiable* other;
instance->getValue("Reference",other);

Raw Buffer

  • maBuffer: manage raw data (binary) buffer

CoreItem

  • maCoreItem: manage JSon style object structures

CoreItem class will be detailed in a future article.

Computed Numeric Parameter

  • maComputedNumeric: call given methods on CoreModifiable owner instance to compute parameter when getValue or setValue are called. The parameter is not stored but recomputed at each call.

Dynamic Attributes

Attributes can be added to an instance at runtime:

instance2->AddDynamicAttribute
(CoreModifiable::ATTRIBUTE_TYPE::STRING, "DynamicAttribute", "initValue");
AddDynamicAttribute(CoreModifiable::ATTRIBUTE_TYPE::UINT, "DynamicAttribute", 1250);

and then accessed like other attributes (as described in the next section).

Of course, dynamic attributes can also be removed:

// remove "DynamicAttribute" from this
RemoveDynamicAttribute("DynamicAttribute");

Access

CoreModifiable attributes can be accessed by their names using setValue and getValue methods on owning instance like this:

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

instance->setValue("IntValue", 16);
// retrieve value using templated "getValue" method
int v = instance->getValue<int>("IntValue");
// or with reference parameter
int v1;
if(!instance->getValue("IntValue",v1))
{
    // instance does not own "IntValue" parameter
}

Array Access

vector and matrix can be accessed using getValue and setValue:

// accessing vector
v4f vect;
instance1->getValue("Vector", vect);
std::cout << "vector values : {" << vect.x << "," << vect.y << "," 
          << vect.z << "," << vect.w << "}" << std::endl;
// set value with string
instance1->setValue("Vector", "{2.0,1.0,0.0,-1.0}");

or with setArrayValue / getArrayValue:

// access with array of values
float arrayv[4];
instance1->getArrayValue("Vector", arrayv, 4);
std::cout << "vector values : {" << arrayv[0] << "," << arrayv[1] << "," 
          << arrayv[2] << "," << arrayv[3] << "}" << std::endl;
arrayv[2] = 10.0f;
instance1->setArrayValue("Vector", arrayv,4);

or for a given element with getArrayElementValue / setArrayElementValue:

// by element
instance1->getArrayElementValue("Vector", arrayv[0], 0,2);
std::cout << "vector values : {" << arrayv[0] << "," << arrayv[1] << "," 
          << arrayv[2] << "," << arrayv[3] << "}" << std::endl;

Attribute Modifiers

getValue or setValue modifiers can be added to attributes. Only "CoreItemOperatorModifier" type is available at the moment.
Generally, CoreItemOperatorModifier is defined directly in the XML file used to initialize (import) an instance.

Here is a sample code to attach a getter modifier on an integer attribute:

// manually add modifier
auto& instanceMap = KigsCore::Instance()->GetDefaultCoreItemOperatorConstructMap();
auto itfound = instanceMap.find("CoreItemOperatorModifier");
if (itfound != instanceMap.end())
{
    AttachedModifierBase* toAdd = (AttachedModifierBase*)(*itfound).second();
    CoreModifiableAttribute* attr = instance1->getAttribute("IntValue");
    if (toAdd && attr)
    {
        // define getter modifier multiplying by two the value retrieve with getValue
        toAdd->Init(attr, true, "input*2");
        attr->attachModifier(toAdd);
    }
}

// getValue now returns 2 * the stored value
std::cout << "Int Value with modifier : " << instance1->getValue<int>("IntValue") << std::endl;

Here, the modifier definition is simply "input*2" where input is the reserved keyword for the attribute value.

More complex calculations can be set using CoreItemOperator syntax.
Detailed CoreItem features will be explained in a future article.

Owner Notification

The owner of an attribute can ask to be notified when a setValue is called:

// NotifyUpdate method will be called when a setValue occur on "StringValue"
m_StringValue.changeNotificationLevel(Owner);

The owner class must overload NotifyUpdate method:

void SimpleClass::NotifyUpdate(const u32 labelid)
{
    if (labelid == m_StringValue.getID())
    {
        std::cout << "StringValue new value is : " <<  m_StringValue.const_ref() << std::endl;
    }
}

Serialization

In XML files, attributes are defined like this for class attributes:

<Attr N="IntValue" V="10"/>

or like this for dynamic attributes:

<Attr N="FloatValue" T="float" V="5.5" Dyn="yes"/>

The initial value can be set directly in V tag like above, or be evaluated from an expression like this:

<Attr N="FloatValue" V="eval(32.4*4.2)"/>

More complex expressions are possible and will be explained in a future article about CoreItem.

Find all the sample code from this article in Sample3 project (browse the code).

Already Published in this Series

  1. Kigs Framework Introduction (1/8) - Overview
  2. Kigs Framework Introduction (2/8) - CoreModifiable
  3. Kigs Framework Introduction (3/8) - Attributes
  4. Kigs Framework Introduction (4/8) - Methods
  5. Kigs Framework Introduction (5/8) - CoreItem

History

  • 7th February, 2020: Initial version
  • 14th February, 2020: Article (4/8) added to the series
  • 21st February, 2020: Article (5/8) added to the series and small bug fix in code

[출처] https://www.codeproject.com/Articles/5257412/Kigs-framework-introduction-3-8-Attributes

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
20 [visual c++] 오류 D8016 '/ZI'과(와) '/Gy-' 명령줄 옵션이 호환되지 않습니다. file 졸리운_곰 2023.11.19 322
19 [visual studio] Bring Your MFC Application to the Web mfc 어플리케인 web 구동 file 졸리운_곰 2023.03.20 279
18 [visual studio] [borland c] Using the WinBGIm Graphics Library with Visual Studio 2005/2008 2010 file 졸리운_곰 2023.02.05 315
17 [Visual Studio][boost c++] [C++] boost 설치 및 visual studio 설정 file 졸리운_곰 2021.11.18 464
16 winbgi Borland Turbo C routines in MS Visual C++ file 졸리운_곰 2017.03.04 290
15 VS 프로젝트 이름 변경 졸리운_곰 2017.02.12 355
14 Retrieving CPU Load Percent total in Windows with C++ 졸리운_곰 2016.12.31 487
13 Cpu 사용율 알아내는 소스 졸리운_곰 2016.12.31 471
12 [MFC] Find local network hosts ip, 로컬네트워크의 IP 주소 알아내기 졸리운_곰 2014.08.27 766
11 (Unicode) CString <==> const char* 졸리운_곰 2014.08.27 640
10 Creating a Child Process with Redirected Input and Output 졸리운_곰 2014.08.17 573
9 C++로 짠 간단한 윈도우 서비스 file 졸리운_곰 2014.05.27 1003
8 VC++ 파일에서 한 라인씩 읽기 졸리운_곰 2014.03.02 1199
7 Search Title Of Opened Windows file 졸리운_곰 2014.02.27 978
6 MFC VC++ 파일의 존재유무 체크 졸리운_곰 2014.02.18 1587
5 MFC VC++ : windows tcpip socket Send() function example 졸리운_곰 2014.02.18 1593
4 Check whether one specific process is running on windows with C++ 졸리운_곰 2014.02.18 1099
3 VC++ : Win32 console App run as to be hide (background) 졸리운_곰 2014.02.18 1245
2 VC++ : Win32 console App run as to be hide (background) 졸리운_곰 2014.02.18 1256
1 VC++ : windows : TCPIP SOCKET SERVER and CLIENT 졸리운_곰 2014.02.18 1097
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED