[{"id":2750,"web_url":"https://patchwork.libcamera.org/comment/2750/","msgid":"<20191003190050.GE1322@bigcity.dyn.berto.se>","date":"2019-10-03T19:00:50","subject":"Re: [libcamera-devel] [PATCH v2 02/13] libcamera: controls: Make\n\tControlValue get/set accessors template methods","submitter":{"id":5,"url":"https://patchwork.libcamera.org/api/people/5/","name":"Niklas Söderlund","email":"niklas.soderlund@ragnatech.se"},"content":"Hi  Laurent,\n\nThanks for your work.\n\nOn 2019-09-29 22:02:43 +0300, Laurent Pinchart wrote:\n> The ControlValue get accessors are implemented with functions of\n> different names, whlie the set accessors use polymorphism to support\n> different control types. This isn't very consistent and intuitive. Make\n> the API clearer by using template methods. This will also have the added\n> advantage that support for the new types will only require adding\n> template specialisations, without adding new methods.\n> \n> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n> Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>\n\nReviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n\n> ---\n>  include/libcamera/controls.h        |  11 ++-\n>  src/libcamera/controls.cpp          | 101 +++++++++++++---------------\n>  src/libcamera/pipeline/uvcvideo.cpp |  10 +--\n>  src/libcamera/pipeline/vimc.cpp     |   6 +-\n>  test/controls/control_info.cpp      |   4 +-\n>  test/controls/control_list.cpp      |  16 ++---\n>  test/controls/control_value.cpp     |  12 ++--\n>  7 files changed, 74 insertions(+), 86 deletions(-)\n> \n> diff --git a/include/libcamera/controls.h b/include/libcamera/controls.h\n> index ffba880a66ff..0886370f0901 100644\n> --- a/include/libcamera/controls.h\n> +++ b/include/libcamera/controls.h\n> @@ -36,13 +36,10 @@ public:\n>  \tControlType type() const { return type_; };\n>  \tbool isNone() const { return type_ == ControlTypeNone; };\n>  \n> -\tvoid set(bool value);\n> -\tvoid set(int value);\n> -\tvoid set(int64_t value);\n> -\n> -\tbool getBool() const;\n> -\tint getInt() const;\n> -\tint64_t getInt64() const;\n> +\ttemplate<typename T>\n> +\tconst T &get() const;\n> +\ttemplate<typename T>\n> +\tvoid set(const T &value);\n>  \n>  \tstd::string toString() const;\n>  \n> diff --git a/src/libcamera/controls.cpp b/src/libcamera/controls.cpp\n> index 9960a30dfa03..88aab88db327 100644\n> --- a/src/libcamera/controls.cpp\n> +++ b/src/libcamera/controls.cpp\n> @@ -90,76 +90,67 @@ ControlValue::ControlValue(int64_t value)\n>   */\n>  \n>  /**\n> - * \\brief Set the value with a boolean\n> - * \\param[in] value Boolean value to store\n> + * \\fn template<typename T> const T &ControlValue::get() const\n> + * \\brief Get the control value\n> + *\n> + * The control value type shall match the type T, otherwise the behaviour is\n> + * undefined.\n> + *\n> + * \\return The control value\n>   */\n> -void ControlValue::set(bool value)\n> +\n> +/**\n> + * \\fn template<typename T> void ControlValue::set(const T &value)\n> + * \\brief Set the control value to \\a value\n> + * \\param[in] value The control value\n> + */\n> +\n> +#ifndef __DOXYGEN__\n> +template<>\n> +const bool &ControlValue::get<bool>() const\n> +{\n> +\tASSERT(type_ == ControlTypeBool);\n> +\n> +\treturn bool_;\n> +}\n> +\n> +template<>\n> +const int &ControlValue::get<int>() const\n> +{\n> +\tASSERT(type_ == ControlTypeInteger || type_ == ControlTypeInteger64);\n> +\n> +\treturn integer_;\n> +}\n> +\n> +template<>\n> +const int64_t &ControlValue::get<int64_t>() const\n> +{\n> +\tASSERT(type_ == ControlTypeInteger || type_ == ControlTypeInteger64);\n> +\n> +\treturn integer64_;\n> +}\n> +\n> +template<>\n> +void ControlValue::set<bool>(const bool &value)\n>  {\n>  \ttype_ = ControlTypeBool;\n>  \tbool_ = value;\n>  }\n>  \n> -/**\n> - * \\brief Set the value with an integer\n> - * \\param[in] value Integer value to store\n> - */\n> -void ControlValue::set(int value)\n> +template<>\n> +void ControlValue::set<int>(const int &value)\n>  {\n>  \ttype_ = ControlTypeInteger;\n>  \tinteger_ = value;\n>  }\n>  \n> -/**\n> - * \\brief Set the value with a 64 bit integer\n> - * \\param[in] value 64 bit integer value to store\n> - */\n> -void ControlValue::set(int64_t value)\n> +template<>\n> +void ControlValue::set<int64_t>(const int64_t &value)\n>  {\n>  \ttype_ = ControlTypeInteger64;\n>  \tinteger64_ = value;\n>  }\n> -\n> -/**\n> - * \\brief Get the boolean value\n> - *\n> - * The value type must be Boolean.\n> - *\n> - * \\return The boolean value\n> - */\n> -bool ControlValue::getBool() const\n> -{\n> -\tASSERT(type_ == ControlTypeBool);\n> -\n> -\treturn bool_;\n> -}\n> -\n> -/**\n> - * \\brief Get the integer value\n> - *\n> - * The value type must be Integer or Integer64.\n> - *\n> - * \\return The integer value\n> - */\n> -int ControlValue::getInt() const\n> -{\n> -\tASSERT(type_ == ControlTypeInteger || type_ == ControlTypeInteger64);\n> -\n> -\treturn integer_;\n> -}\n> -\n> -/**\n> - * \\brief Get the 64-bit integer value\n> - *\n> - * The value type must be Integer or Integer64.\n> - *\n> - * \\return The 64-bit integer value\n> - */\n> -int64_t ControlValue::getInt64() const\n> -{\n> -\tASSERT(type_ == ControlTypeInteger || type_ == ControlTypeInteger64);\n> -\n> -\treturn integer64_;\n> -}\n> +#endif /* __DOXYGEN__ */\n>  \n>  /**\n>   * \\brief Assemble and return a string describing the value\n> diff --git a/src/libcamera/pipeline/uvcvideo.cpp b/src/libcamera/pipeline/uvcvideo.cpp\n> index 8965210550d2..81c548af2c64 100644\n> --- a/src/libcamera/pipeline/uvcvideo.cpp\n> +++ b/src/libcamera/pipeline/uvcvideo.cpp\n> @@ -235,24 +235,24 @@ int PipelineHandlerUVC::processControls(UVCCameraData *data, Request *request)\n>  \n>  \t\tswitch (ci->id()) {\n>  \t\tcase Brightness:\n> -\t\t\tcontrols.add(V4L2_CID_BRIGHTNESS, value.getInt());\n> +\t\t\tcontrols.add(V4L2_CID_BRIGHTNESS, value.get<int>());\n>  \t\t\tbreak;\n>  \n>  \t\tcase Contrast:\n> -\t\t\tcontrols.add(V4L2_CID_CONTRAST, value.getInt());\n> +\t\t\tcontrols.add(V4L2_CID_CONTRAST, value.get<int>());\n>  \t\t\tbreak;\n>  \n>  \t\tcase Saturation:\n> -\t\t\tcontrols.add(V4L2_CID_SATURATION, value.getInt());\n> +\t\t\tcontrols.add(V4L2_CID_SATURATION, value.get<int>());\n>  \t\t\tbreak;\n>  \n>  \t\tcase ManualExposure:\n>  \t\t\tcontrols.add(V4L2_CID_EXPOSURE_AUTO, 1);\n> -\t\t\tcontrols.add(V4L2_CID_EXPOSURE_ABSOLUTE, value.getInt());\n> +\t\t\tcontrols.add(V4L2_CID_EXPOSURE_ABSOLUTE, value.get<int>());\n>  \t\t\tbreak;\n>  \n>  \t\tcase ManualGain:\n> -\t\t\tcontrols.add(V4L2_CID_GAIN, value.getInt());\n> +\t\t\tcontrols.add(V4L2_CID_GAIN, value.get<int>());\n>  \t\t\tbreak;\n>  \n>  \t\tdefault:\n> diff --git a/src/libcamera/pipeline/vimc.cpp b/src/libcamera/pipeline/vimc.cpp\n> index f26a91f86ec1..3e34e7a0edbf 100644\n> --- a/src/libcamera/pipeline/vimc.cpp\n> +++ b/src/libcamera/pipeline/vimc.cpp\n> @@ -288,15 +288,15 @@ int PipelineHandlerVimc::processControls(VimcCameraData *data, Request *request)\n>  \n>  \t\tswitch (ci->id()) {\n>  \t\tcase Brightness:\n> -\t\t\tcontrols.add(V4L2_CID_BRIGHTNESS, value.getInt());\n> +\t\t\tcontrols.add(V4L2_CID_BRIGHTNESS, value.get<int>());\n>  \t\t\tbreak;\n>  \n>  \t\tcase Contrast:\n> -\t\t\tcontrols.add(V4L2_CID_CONTRAST, value.getInt());\n> +\t\t\tcontrols.add(V4L2_CID_CONTRAST, value.get<int>());\n>  \t\t\tbreak;\n>  \n>  \t\tcase Saturation:\n> -\t\t\tcontrols.add(V4L2_CID_SATURATION, value.getInt());\n> +\t\t\tcontrols.add(V4L2_CID_SATURATION, value.get<int>());\n>  \t\t\tbreak;\n>  \n>  \t\tdefault:\n> diff --git a/test/controls/control_info.cpp b/test/controls/control_info.cpp\n> index 8cda860b9fe9..faefaaa444d9 100644\n> --- a/test/controls/control_info.cpp\n> +++ b/test/controls/control_info.cpp\n> @@ -32,7 +32,7 @@ protected:\n>  \t\t\treturn TestFail;\n>  \t\t}\n>  \n> -\t\tif (info.min().getInt() != 0 || info.max().getInt() != 0) {\n> +\t\tif (info.min().get<int>() != 0 || info.max().get<int>() != 0) {\n>  \t\t\tcout << \"Invalid control range for Brightness\" << endl;\n>  \t\t\treturn TestFail;\n>  \t\t}\n> @@ -50,7 +50,7 @@ protected:\n>  \t\t\treturn TestFail;\n>  \t\t}\n>  \n> -\t\tif (info.min().getInt() != 10 || info.max().getInt() != 200) {\n> +\t\tif (info.min().get<int>() != 10 || info.max().get<int>() != 200) {\n>  \t\t\tcout << \"Invalid control range for Contrast\" << endl;\n>  \t\t\treturn TestFail;\n>  \t\t}\n> diff --git a/test/controls/control_list.cpp b/test/controls/control_list.cpp\n> index f1d79ff8fcfd..0402e7c23dba 100644\n> --- a/test/controls/control_list.cpp\n> +++ b/test/controls/control_list.cpp\n> @@ -96,7 +96,7 @@ protected:\n>  \t\t\treturn TestFail;\n>  \t\t}\n>  \n> -\t\tif (list[Brightness].getInt() != 255) {\n> +\t\tif (list[Brightness].get<int>() != 255) {\n>  \t\t\tcout << \"Incorrest Brightness control value\" << endl;\n>  \t\t\treturn TestFail;\n>  \t\t}\n> @@ -125,8 +125,8 @@ protected:\n>  \t\t/*\n>  \t\t * Test control value retrieval and update through ControlInfo.\n>  \t\t */\n> -\t\tif (list[brightness].getInt() != 64 ||\n> -\t\t    list[contrast].getInt() != 128) {\n> +\t\tif (list[brightness].get<int>() != 64 ||\n> +\t\t    list[contrast].get<int>() != 128) {\n>  \t\t\tcout << \"Failed to retrieve control value\" << endl;\n>  \t\t\treturn TestFail;\n>  \t\t}\n> @@ -134,8 +134,8 @@ protected:\n>  \t\tlist[brightness] = 10;\n>  \t\tlist[contrast] = 20;\n>  \n> -\t\tif (list[brightness].getInt() != 10 ||\n> -\t\t    list[contrast].getInt() != 20) {\n> +\t\tif (list[brightness].get<int>() != 10 ||\n> +\t\t    list[contrast].get<int>() != 20) {\n>  \t\t\tcout << \"Failed to update control value\" << endl;\n>  \t\t\treturn TestFail;\n>  \t\t}\n> @@ -185,9 +185,9 @@ protected:\n>  \t\t\treturn TestFail;\n>  \t\t}\n>  \n> -\t\tif (newList[Brightness].getInt() != 10 ||\n> -\t\t    newList[Contrast].getInt() != 20 ||\n> -\t\t    newList[Saturation].getInt() != 255) {\n> +\t\tif (newList[Brightness].get<int>() != 10 ||\n> +\t\t    newList[Contrast].get<int>() != 20 ||\n> +\t\t    newList[Saturation].get<int>() != 255) {\n>  \t\t\tcout << \"New list contains incorrect values\" << endl;\n>  \t\t\treturn TestFail;\n>  \t\t}\n> diff --git a/test/controls/control_value.cpp b/test/controls/control_value.cpp\n> index 778efe5c115f..397c43f799ad 100644\n> --- a/test/controls/control_value.cpp\n> +++ b/test/controls/control_value.cpp\n> @@ -27,12 +27,12 @@ protected:\n>  \t\t     << \" Bool: \" << boolean.toString()\n>  \t\t     << endl;\n>  \n> -\t\tif (integer.getInt() != 1234) {\n> +\t\tif (integer.get<int>() != 1234) {\n>  \t\t\tcerr << \"Failed to get Integer\" << endl;\n>  \t\t\treturn TestFail;\n>  \t\t}\n>  \n> -\t\tif (boolean.getBool() != true) {\n> +\t\tif (boolean.get<bool>() != true) {\n>  \t\t\tcerr << \"Failed to get Boolean\" << endl;\n>  \t\t\treturn TestFail;\n>  \t\t}\n> @@ -45,19 +45,19 @@ protected:\n>  \t\t\treturn TestFail;\n>  \t\t}\n>  \n> -\t\tvalue.set(true);\n> +\t\tvalue.set<bool>(true);\n>  \t\tif (value.isNone()) {\n>  \t\t\tcerr << \"Failed to set an empty object\" << endl;\n>  \t\t\treturn TestFail;\n>  \t\t}\n>  \n> -\t\tif (value.getBool() != true) {\n> +\t\tif (value.get<bool>() != true) {\n>  \t\t\tcerr << \"Failed to get Booleans\" << endl;\n>  \t\t\treturn TestFail;\n>  \t\t}\n>  \n> -\t\tvalue.set(10);\n> -\t\tif (value.getInt() != 10) {\n> +\t\tvalue.set<int>(10);\n> +\t\tif (value.get<int>() != 10) {\n>  \t\t\tcerr << \"Failed to get Integer\" << endl;\n>  \t\t\treturn TestFail;\n>  \t\t}\n> -- \n> Regards,\n> \n> Laurent Pinchart\n> \n> _______________________________________________\n> libcamera-devel mailing list\n> libcamera-devel@lists.libcamera.org\n> https://lists.libcamera.org/listinfo/libcamera-devel","headers":{"Return-Path":"<niklas.soderlund@ragnatech.se>","Received":["from mail-lf1-x143.google.com (mail-lf1-x143.google.com\n\t[IPv6:2a00:1450:4864:20::143])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id ED06460BE8\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu,  3 Oct 2019 21:00:52 +0200 (CEST)","by mail-lf1-x143.google.com with SMTP id q11so2642889lfc.11\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 03 Oct 2019 12:00:52 -0700 (PDT)","from localhost (h-93-159.A463.priv.bahnhof.se. [46.59.93.159])\n\tby smtp.gmail.com with ESMTPSA id\n\th2sm716500ljm.26.2019.10.03.12.00.51\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tThu, 03 Oct 2019 12:00:51 -0700 (PDT)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=ragnatech-se.20150623.gappssmtp.com; s=20150623;\n\th=date:from:to:cc:subject:message-id:references:mime-version\n\t:content-disposition:content-transfer-encoding:in-reply-to\n\t:user-agent; bh=BB/AqJEIid09end7fK7bqD8IKdiStpWFvMe++pAOT7A=;\n\tb=W08aNynLcd+QN7EJt6jYESmwJRR+kbXxqrQEwbplGN8z7GTU1TVvS83s0waqNDxMog\n\t5QB98c2kbTK1eRJ86zlS5x/mguse6O7wM+2vQyLSSqav8JIjwGMdXu+5WQAycEYsUV3F\n\t12W6TUHUBpGpxO1PxSWA6sKVBW3PlTRt3V1XhcNEkPQ7yRu+XG+3arSwm26mREKI6D7m\n\tbApfMp5EzDNWtjA9JCuvACDBhSt2KigIuNDIQjqpZ114CKCN0zNrC8uEL3FdvjG5Zshu\n\tU+js5bshZkVDmxTlTRvDTE1h+5scx2mNz1Yk3WeiMHIRADrVcLDUeqrZ84ZmGioTyJSE\n\tEqow==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20161025;\n\th=x-gm-message-state:date:from:to:cc:subject:message-id:references\n\t:mime-version:content-disposition:content-transfer-encoding\n\t:in-reply-to:user-agent;\n\tbh=BB/AqJEIid09end7fK7bqD8IKdiStpWFvMe++pAOT7A=;\n\tb=d1ZjEeMK2cuxgf8lbAgWRnLQZQSGHfUvEzoee8qgBU5HXBOMfQ9pQweMJ2TD2icTRj\n\t4W5kvp+JCVzSHpJ9wpC4lm17oEX/BcYhYlLLZ4nVmIEdw2DCkEADVnjqvCbOPeKvHQCH\n\tOu/7kIO+3sQ2jl4lnWV/MJZHqgECa6GWT5p5sbtm5CcTEakoF5rc9xd3ijUXtCwkNkhl\n\tarx3Gyi0zOCCDgeXmzC10hsIr0UoHeroOOqAwKmm8i7QMcMepVnnYth9Kd0XNUAqDsUw\n\tcEmuvmzScAiYyaLlMrBH69HwLd5Y18ur6dnYPjydVMEU7h/2bp/5ly5ACksH+9xfm2NB\n\tv5KA==","X-Gm-Message-State":"APjAAAXDIMDHi6++1x1Fkc3b+g1X0W5V2AhxMbzEfm2DPIbmMGFJy7sF\n\tSm3osUGdj71utFKG7f5CyKOoQQ==","X-Google-Smtp-Source":"APXvYqxfPRe1PicIQ6ckOL9RA3iClbb2LYZmrmDOd84/iJMawWn4bli4NB6HSlpNN0zWk7QrR5EZCQ==","X-Received":"by 2002:ac2:5483:: with SMTP id t3mr6721040lfk.39.1570129252213; \n\tThu, 03 Oct 2019 12:00:52 -0700 (PDT)","Date":"Thu, 3 Oct 2019 21:00:50 +0200","From":"Niklas =?iso-8859-1?q?S=F6derlund?= <niklas.soderlund@ragnatech.se>","To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org","Message-ID":"<20191003190050.GE1322@bigcity.dyn.berto.se>","References":"<20190929190254.18920-1-laurent.pinchart@ideasonboard.com>\n\t<20190929190254.18920-3-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=iso-8859-1","Content-Disposition":"inline","Content-Transfer-Encoding":"8bit","In-Reply-To":"<20190929190254.18920-3-laurent.pinchart@ideasonboard.com>","User-Agent":"Mutt/1.12.1 (2019-06-15)","Subject":"Re: [libcamera-devel] [PATCH v2 02/13] libcamera: controls: Make\n\tControlValue get/set accessors template methods","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","X-List-Received-Date":"Thu, 03 Oct 2019 19:00:53 -0000"}}]