Archives for posts with tag: oo

I worked on my object oriented extensions for CMake  and am on a much higher level of usability than before!

try it at https://github.com/toeb/oo-cmake 

and as always: Feedback is most welcome

See the following object tutorial for ease of use: (for the current version look at the readme.md on github)


### using objects
    ### =============
    # oo-cmake is very similar to javascript
    # I actually used the javascript reference to figure out how things could be done :)
    # oo-cmake is a pure object oriented language like javascript (only objects no types per se)
    # oo-cmake is currently file based and relies heavily on dynamic functions to be upfron about it:
    # oo-cmake is very slow (depending on what your doing)

    ## creating a object
    ## =================
    obj_new(myobject)
    # ${myobject} now is a reference to an object
    obj_exists(${myobject} _exists)
    assert(_exists)

    ## deleting a object
    ## =================
    # oo-cmake does not contains automatic memory management
    # you can however remove all objects by calling obj_cleanup 
    # (a very crude way of garbage collection) I would suggest calling it at the end of cmake.
    obj_new(object_to_delete)
    obj_delete(${object_to_delete})
    # object_to_delete still contains the same reference
    # but the object does not exists anymore and the following returns false
    obj_exists(${object_to_delete} _exists)
    assert(NOT _exists)

    ## setting and setting property
    ## ==================
    obj_new(myobject)
    # call obj_set passing the object reference
    # the propertyname 'key1' and the value 'val1'
    # everything beyond 'key1' is saved (as a list)
    obj_set(${myobject} key1 "val1")
    #call obj_get passing the object refernce the result variable and
    # the key which is to be gotten
    obj_get(${myobject} theValue key1)
    assert(theValue)
    assert(${theValue} STREQUAL "val1")

    ## setting a function and calling it
    ## =================================
    obj_new(obj)
    obj_set(${obj} last_name "Becker")
    obj_set(${obj} first_name "Tobias")
    #obj_setfunction accepts any function (cmake command, string function, file function, unique function (see function tutorial))
    # if you use a cmake function be sure that it will not be overwritten
    # the safest way to add a function is to use obj_declarefunction
    # you can either specify the key where it is to be stored or not
    # if you do not specify it the name of the function is used
    function(greet result)
        # in the function you have read access to all fields of the proeprty
        # as will as to 'this' which contains the reference to the object

        # this sets the variable ${result} in PARENT_SCOPE (returning values in cmake)
        set(${result} "Hello ${first_name} ${last_name}!" PARENT_SCOPE)

    endfunction()
    obj_setfunction(${obj} greet)
    obj_callmember(${obj} greet res)
    assert(res)
    assert(${res} STREQUAL "Hello Tobias Becker!")
    # alternatively you can also use obj_declarefunction
    # this is actually the easiest way to define a function in code
    obj_declarefunction(${obj} say_goodbye)
    function(${say_goodbye} result)
        set(${result} "Goodbye ${first_name} ${last_name}!" PARENT_SCOPE)
    endfunction()
    obj_callmember(${obj} say_goodbye res)
    assert(res)
    assert(res STREQUAL "Goodbye Tobias Becker!")

    ## built in methods
    ## ================
    # obj_new creates a object which automatically inherits Object
    # Object contains some functions e.g. to_string, print, ...
    # 
    obj_new(obj)
    obj_callmember(${obj} print)

    # this prints all members
    # ie
    #{
    # print: [function], 
    # to_string: [function], 
    # __ctor__: [object :object_Y3dVWkChKi], 
    # __proto__: [object :object_AztQwnKoE7], 
    #}

    ## constructors
    ## ============
    # you can easily define a object type via constructor
    function(MyObject)
        # declare a function on the prototype (which is accessible for all objects)
        # inheriting from MyObject
        obj_declarefunction(${__proto__} myMethod)
        function(${myMethod} result)
            set(${result} "myMethod: ${myValue}" PARENT_SCOPE)
            this_set(myNewProperty "this is a text ${myValue}")
        endfunction()

        # set a field for the object
        this_set(myValue "hello")
    endfunction()

    obj_new(obj MyObject)
    # type of object will now be MyObject
    obj_gettype(${obj} type)
    assert(type)
    assert(${type} STREQUAL MyObject)
    # call
    obj_callmember(${obj} myMethod res)
    assert(res)
    assert(${res} STREQUAL "myMethod: hello")
    obj_set(${obj} myValue "othervalue")
    obj_callmember(${obj} myMethod res)
    assert(res)
    assert(${res} STREQUAL "myMethod: othervalue")
    obj_get(${obj} res myNewProperty)
    assert(res)
    assert(${res} STREQUAL "this is a text othervalue")

    ## functors
    ## ========

    ## binding a function
    ## ==================
    # you can bind any function to an object without
    # setting it as property
    # causing the function to get access to 'this'
    # and all defined properties
    function(anyfunction)
        this_callmember(print)
    endfunction()
    obj_new(obj)
    obj_bindcall(${obj} anyfunction)
    ## print the object
    # alternatively you can bind the function to the object
    obj_bind(boundfu ${obj} anyfunction)
    call_function(${boundfu})
    # prints the object
    # alternatively you can bind and import then function
    # beware that you might overwrite a defined function if you append REDEFINE
    # 
    obj_bindimport(${obj} anyfunction myBoundFunc REDEFINE)
    myBoundFunc()
    # print the object

Get it at https://github.com/toeb/oo-cmake and tell me if you like it

For more complex build system features oo programming would be a major boon. So I took it upon myself to create a usable and tested oo-extension for cmake (in pure CMake) so I could continue developing and extending my plattform independent tooling support (cutil) .

It’s too bad that Kitware decided to use their own language (with very little high level language features) for their build system product because CMakes project generation capabilties are unparalleled .  I can understand that it seemed like a good idea at first ( the CMake scripting Language is platform independent and easier to use than make or bash or cmd also it was not intended to do complicated things). I however strongly believe that building a programm should be as easy as possible and therefore a high level language should have been used. CMake’s creators Bill Hoffman and Kenneth Martin have said that they would have done this differently if they had to do it again (http://www.aosabook.org/en/cmake.html)

Of course CMake does not offer language features for a very sleak usage but the with these functions you can use prototypical inheritance, maps, methods, objects, lists, properties, references, etc. which are at the basis of every oo language.

Even if you do not want to do inheritance in CMake you can use these functions for Maps (Dictionaries)

People familiar with JavaScript will be familiar with this approach to object orientation.

Here is a simple example

cmake_minimum_required(VERSION 2.8)
# include standalone version of oo-cmake
# when not using cutil this  includes the function files as well as
# the dependent functions
include("../oo-cmake.cmake")

message("creating a simple inheritance example")

#create animal object
obj_create(animal)

# set object method eat
# this method prints out which food the animal is eating
# existing functions can also be appliead via obj_bindcall(${ref} func arg1 arg2 arg3)
obj_set(
    ${animal}   #reference to animal object
    "eat"       #name of property
    RAW         # RAW indictates that the data is to be treated as a raw string not a list (hinders evaluation)
    "function(eat this)
      obj_get(\${this} food food) 
      message(\"I am eating \${food}\") 
     endfunction()"
)

# create mamal object
obj_create(mamal)
obj_setprototype(${mamal} ${animal})

#create bird object
obj_create(bird)                    #create instance
obj_setprototype(${bird} ${animal}) #set prototype animal
obj_set(${bird} food Birdfood)      #set bird's food property to "Birdfood"

#create dog object
obj_create(dog)                     #create instance
obj_setprototype(${dog} ${mamal})   #set prototype mamal
obj_set(${dog} food Dogfood)        #set dogs food property to "Dogfood"

#create cat object
obj_create(cat)                     #create instance
obj_setprototype(${cat} ${mamal})   #set prototype mamal
obj_set(${cat} food Catfood)        #set cat's food property to "Catfood"

# call eat function for different objects
obj_callmember(${bird} eat) 
obj_callmember(${dog} eat)
obj_callmember(${cat} eat)

#output should be
#I am eating Birdfood
#I am eating Dogfood
#I am eating Catfood

You can use the following commands

 
obj_create(out_ref [objectpath]) #returns a ref to a new object. this object is persistent and will exist until obj_delete is called, internally the object is represented by a folder
obj_delete(in_ref) # deletes a object (deletes the folder internally)
obj_find(refOrId) # searches for a named object or id
obj_get(ref value key) # gets a property ${value} = ref[key] the prototype chain is considered
obj_set(ref key value) # sets a property ref[key] = value
obj_getref(ref prop_ref key) # returns the reference to a property (a file)
obj_getownproperty(ref value key) # returns the value for property if the property belongs to ref 
obj_setownproperty(ref key value) # sets the own property ref[key] =value
obj_has(ref result key) # sets result to true if ref[key] exists
obj_hasownproperty(ref result key) # sets result to true if ref[key] exists and is owned by ref
obj_callmember(ref methodname args...) # calls a stored memberfunction (first argument passed is ref)
obj_bindcall(ref methodname args...) # calls a defined function setting first argument to ref
obj_setprototype(ref proto_ref) # sets the prototype for ref
obj_getprototype(ref proto_ref) # returns the prototyp for ref
obj_settype(ref type) # sets the type for ref
obj_gettype(ref type) # gets the type for ref
obj_getkeys(ref keys) # returns all defined properties for ref
obj_getownkeys(ref keys) # returns all defined properties that ref owns