{"id":16002893,"url":"https://github.com/soreing/var-cpp","last_synced_at":"2025-10-15T16:47:19.616Z","repository":{"id":110409828,"uuid":"387241320","full_name":"Soreing/var-cpp","owner":"Soreing","description":"Dynamic and flexible type var implementation in c++","archived":false,"fork":false,"pushed_at":"2021-08-01T11:27:17.000Z","size":59,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-10T09:43:55.060Z","etag":null,"topics":["cpp","var","variables"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Soreing.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-07-18T18:15:23.000Z","updated_at":"2024-06-15T08:06:59.000Z","dependencies_parsed_at":null,"dependency_job_id":"77ab4d72-f4ab-43b3-b497-e06e32ef214d","html_url":"https://github.com/Soreing/var-cpp","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Soreing%2Fvar-cpp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Soreing%2Fvar-cpp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Soreing%2Fvar-cpp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Soreing%2Fvar-cpp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Soreing","download_url":"https://codeload.github.com/Soreing/var-cpp/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247274141,"owners_count":20912054,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cpp","var","variables"],"created_at":"2024-10-08T10:04:45.390Z","updated_at":"2025-10-15T16:47:14.575Z","avatar_url":"https://github.com/Soreing.png","language":"C++","readme":"# var-cpp\n\n# Description\n\nvar-cpp is a C++ library for creating JavaScript-like var variables that allow the assignment of data or functions with different types at runtime. The library is complete with relational and arithmetic operators, as well as functions for working with arrays.\n\n# Installation\nInclude `varcpp.h` in your project for defining the var class. If you want to compile the library from source, include the cpp files for the implementation, all following the format `v_*.cpp`. Alternatively, you can compile the source code to a static library and include it that way.\n\n# Usage\n## Creating Variables\nYou can declare a var object and assign it data. Internally the object has the following types: boolean, integer, real number, text, array, object, binary object, functions, undefined.\n```c++\nvar bol = true;\nvar num = 5;\nvar dec = 10.2;\nvar txt = \"Sample Text\";\n```\n\nAfter the definition of the variable, you can re-assign the data in the variable, even with a different type from what was assigned originally\n```c++\nvar number = 10;\nnumber = \"Ten\";\n```\n\n## Casting to Basic Types\nNumerical type vars can be explicitly casted to some basic C++ types using C-style casting. Currently you can cast to `bool`, `int` and `double`.\n```c++\nvar vdec = 10.2;\ndouble cdec = (double)vdec;\n```\nAdditionally, if a string value represents a value that can be converted to a number, it is possible to cast it to another type to do do simple text to int conversion\n```c++\nvar txt = \"10.2\";\ndouble dec = (double)txt; \n```\n\n## Printing and Displaying\nYou can use the toString() function which returns some string representation of the variable, or you an also use the ostream operator, which is overloaded to call the toString() function.\n```c++\nvar num = 5;\nstring str = num.toString();\ncout \u003c\u003c num \u003c\u003c \"  \" \u003c\u003c num.toString() \u003c\u003c endl; \n```\n```\nOutput:\n5  5\n```\n\n## Working with Arrays\nArray type vars use vectors to store elements and resize dynamically. Elements in a var array can have different types. To create an array, you can use the `array` macro.\n```c++\nvar emp = array{} //Creates an empty array\nvar arr = array{true, 5, 10.2, \"Sample Text\"};\n```\n\nArrays can even contain other arrays and more complex types\n```c++\nvar complex = array{\"Two Arrays\", array{1,2,3}, array{true, 10.2}};\n```\n\nTo access the elements of an array, you can use the square bracket operator `[]` with an index. The elements are returned by reference, so you can make changes to them. If you supply an index too large, the array is resized to be valid.\n```c++\nvar arr = array{1,2,3};\narr[0] = 10.2;\narr[4] = \"Sample Text\";\n\ncout \u003c\u003c  arr \u003c\u003c endl;\n```\n```\nOutput:\n[ 10.2, 2, 3, undfined, Sample Text ]\n```\n\n## Working with Objects\nObject type vars use a vector of attributes, where each attribute has a string name and a var value. To create an object, you can use the `object` macro.\n```c++\nvar emp = object{} //Creates an empty object\nvar obj = object{{\"Width\", 4}, {\"Height\", 5}, {\"Price\", 10.2}};\n```\nSimilarly to arrays, objects can contain other complex attributes.\n```c++\nvar person = object{\n    {\"name\", \"John Smith\"},\n    {\"age\", 25},\n    {\"contacts\", array{\n        object{\n            {\"type\", \"home\"},\n            {\"number\", \"212 555-1234\"}\n        },\n        object{\n            {\"type\", \"office\"},\n            {\"number\", \"646 555-4567\"}\n        }\n    }},\n};\n```\n\nTo access the attributes of an object, you can use the square bracket operator [] with a string. The elements are returned by reference, so you can make changes to them. If you use a name that does not match an attribute, a new attribute is created and returned. **Attribute names are case sensitive!**\n```c++\nvar obj = object{{\"Width\", 4}, {\"Height\", 5}, {\"Price\", 10.2}};\nobj[\"Width\"] = \"Four\";\nobj[\"Quantity\"] = 10;\n\ncout \u003c\u003c obj \u003c\u003c endl;\ncout \u003c\u003c obj[\"Something\"] \u003c\u003c endl;\n```\n```\nOutput:\n{ Width:Four, Height:5, Price:10.2, Quantity:10 }\nundefined\n```\n\n## Binary Objects and Byte Streams\nAny C/C++ object or byte stream can be stored as a blob type. To create a Binary Object, you can use the blob structure, which expects a char pointer `(char*)` and number of bytes.\n```c++\nclass Box{\npublic:\n    int w, h, l;\n    Box(int w, int h, int l)\n        : w(w), h(h), l(l)\n    {} \n};\n\nvoid main()\n{\n    Box b(10, 25, 100);\n    char bytes[6] = {0, 1, 2, 3, 4, 5};\n\n    var bx = blob{(char*)\u0026b, sizeof(Box)};\n    var ar = blob{bytes, 6};\n\n    cout \u003c\u003c bx \u003c\u003c endl;\n    cout \u003c\u003c ar \u003c\u003c endl;\n}\n```\n```\nOutput:\n0x0a0000001900000064000000\n0x000102030405\n```\nIf you want to retrieve a pointer to the object, you can use the getter function `getData()`, then cast it to the appropriate pointer from `void*`\n\n## Functions as Variables\nYou can assign functions to var objects and call them with arguments later. Functions **must** use only var return type and arguments, otherwise the behavior is unpredicrable.\n```c++\nvar add(var left, var right){\n    return left + right;\n}\n\nvoid main(){\n    var foo = add;\n    cout \u003c\u003c foo(1, 3) \u003c\u003c endl;\n}\n```\nDue to limitations, you can only have up to 8 arguments for any function. If you supply more or less arguments than the function requires to be called, the function is not called and undefined is returned.\n```c++\ncout \u003c\u003c foo(1) \u003c\u003c endl;\ncout \u003c\u003c foo(1, 2, 3) \u003c\u003c endl;\n```\n```\nOutput:\nundefined\nundefined\n```\n\n## Logical and Relational Operators\nLogical operators are overloaded to explicitly cast vars to booleans. For numeric types, any value that is non zero is `true`. For other types, any value is `true`. undefined is always `false`.\n```c++\nvar tru = true;\nvar fls = false;\ncout\u003c\u003c (tru || fls) \u003c\u003c \" \" \u003c\u003c (tru \u0026\u0026 fls) \u003c\u003c endl;\n```\n```\nOutput:\n1 0\n```\nRelational operators compare variables similar to JavaScript\n- Numerical comparison if both values are numbers or one is a number and the other converts to a number\n- Alphabetical comparison if both values are string or at least one object does not convert to a number. Complex types are converted to string using toString()\n- Functions can be compared for equality, but not less/greater than\n\n```c++\nvar num = 5;\nvar tx1 = \"123\";\nvar tx2 = \"5\";\n\ncout \u003c\u003c (num \u003c tx1);   //true\ncout \u003c\u003c (tx2 \u003c tx1);   //false\ncout \u003c\u003c (num == tx2);  //true\n```\n\nTo check if the values are strictly the same, you can compare the variables to internal types. Internal types include `boolean_t`, `integer_t`, `real_t`, `text_t`, `array_t`, `object_t`, `blob_t`, `function_t`, `undefined_t`.\n```c++\nvar txt = \"5\";\nif(txt == integer_t \u0026\u0026 txt == 5){\n    cout \u003c\u003c \"Same\" \u003c\u003c endl;\n} else {\n    cout \u003c\u003c \"Different\" \u003c\u003c endl;\n}\n```\n```\nOutput:\nDifferent\n```\nYou can also compare if two variables have the same type with the `getType()` getter function.\n```c++\nvar tx1 = \"Sample Text\";\nvar tx2 = \"5\";\nif(tx1.getType() == tx2.getType()){\n    cout \u003c\u003c \"Same\" \u003c\u003c endl;\n} else {\n    cout \u003c\u003c \"Different\" \u003c\u003c endl;\n}\n```\n```\nOutput:\nSame\n```\n\n## Arithmetic Operators\nAll basic math operations are implemented for numerical types, which includes `boolean_t`, `integer_t` and `real_t`. When two different types are used, the result always matches the more complex type.\n```c++\nvar num = 50;\nvar dec = 10.5;\ncout \u003c\u003c (num + dec) \u003c\u003c endl;\ncout \u003c\u003c (num - dec) \u003c\u003c endl;\ncout \u003c\u003c (num * dec) \u003c\u003c endl;\ncout \u003c\u003c (num / dec) \u003c\u003c endl;\ncout \u003c\u003c (num % dec) \u003c\u003c endl;\n```\n```\nOutput:\n60.5\n39.5\n525\n4.761905\n8.0\n```\n\nWhen arithmetic operators are used together with assignment, such as `+=`, `-=`, etc.. the original type of the variable is preserved.\n```c++\nvar num = 50;\nvar dec = 10.5;\nnum /= dec;\ncout \u003c\u003c num \u003c\u003c endl;\n```\n```\nOutput:\n4\n```\n\nThe `+` operator also joins together objects with strings if at least one operand is a string.\n```c++\nvar txt = \"Sample Text\";\nvar num = 5;\ncout \u003c\u003c (txt + num) \u003c\u003c endl;\n```\n```\nOutput:\nSample Text5\n```\n\nWhen used between two arrays or objects, the `+` operator creates a new array or object that contains all the elements or attributes from each operand. the `+=` operator merges the elements/attributes of the right into the left hand operand.\n```c++\nvar ar1 = array{true, 5, 10.2};\nvar ar2 = array{\"Sample Text\", 99.75};\nar1 += ar2;\n\ncout \u003c\u003c ar1 \u003c\u003c endl;\n```\n```\nOutput:\n[ true, 5, 10.2, Sample Text, 99.75 ]\n```\n\n## Array Functions\nSeveral functions from JavaScript and C++ vectors are implemented to work with vars of `array_t` type.\n\n`erase`, `every`, `fill`, `filter`, `find`, `findIndex`, `forEach`, `includes`, `indexOf`, `insert`, `join`, `length`, `pop`, `push`, `reverse`, `shift`, `slice`, `some`, `splice`, `unshift`\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoreing%2Fvar-cpp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsoreing%2Fvar-cpp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoreing%2Fvar-cpp/lists"}