This repository has been archived on 2023-10-09. You can view files and clone it, but cannot push or open issues or pull requests.
Files
blender-archive/source/blender/blenlib/BLI_string_utils.h
Clément Foucault a2c1c368af BLI: Refactor vector types & functions to use templates
This patch implements the vector types (i.e:float2) by making heavy
usage of templating. All vector functions are now outside of the vector
classes (inside the blender::math namespace) and are not vector size
dependent for the most part.

In the ongoing effort to make shaders less GL centric, we are aiming
to share more code between GLSL and C++ to avoid code duplication.

Motivations:
- We are aiming to share UBO and SSBO structures between GLSL and C++.
  This means we will use many of the existing vector types and others we
  currently don't have (uintX, intX). All these variations were asking
  for many more code duplication.
- Deduplicate existing code which is duplicated for each vector size.
- We also want to share small functions. Which means that vector functions
  should be static and not in the class namespace.
- Reduce friction to use these types in new projects due to their
  incompleteness.
- The current state of the BLI_(float|double|mpq)(2|3|4).hh is a bit of a
  let down. Most clases are incomplete, out of sync with each others with
  different codestyles, and some functions that should be static are not
  (i.e: float3::reflect()).

Upsides:
- Still support .x, .y, .z, .w for readability.
- Compact, readable and easilly extendable.
- All of the vector functions are available for all the vectors types and
  can be restricted to certain types. Also template specialization let us
  define exception for special class (like mpq).
- With optimization ON, the compiler unroll the loops and performance is
  the same.

Downsides:
- Might impact debugability. Though I would arge that the bugs are rarelly
  caused by the vector class itself (since the operations are quite trivial)
  but by the type conversions.
- Might impact compile time. I did not saw a significant impact since the
  usage is not really widespread.
- Functions needs to be rewritten to support arbitrary vector length. For
  instance, one can't call len_squared_v3v3 in math::length_squared() and
  call it a day.
- Type cast does not work with the template version of the math:: vector
  functions. Meaning you need to manually cast float * and (float *)[3] to
  float3 for the function calls.
  i.e: math::distance_squared(float3(nearest.co), positions[i]);
- Some parts might loose in readability:
  float3::dot(v1.normalized(), v2.normalized())
  becoming
  math::dot(math::normalize(v1), math::normalize(v2))
  But I propose, when appropriate, to use
  using namespace blender::math; on function local or file scope to
  increase readability. dot(normalize(v1), normalize(v2))

Consideration:
- Include back .length() method. It is quite handy and is more C++
  oriented.
- I considered the GLM library as a candidate for replacement.
  It felt like too much for what we need and would be difficult to
  extend / modify to our needs.
- I used Macros to reduce code in operators declaration and potential
  copy paste bugs. This could reduce debugability and could be reverted.
- This touches delaunay_2d.cc and the intersection code. I would like to
  know @Howard Trickey (howardt) opinion on the matter.
- The noexcept on the copy constructor of mpq(2|3) is being removed.
  But according to @Jacques Lucke (JacquesLucke) it is not a real problem
  for now.

I would like to give a huge thanks to @Jacques Lucke (JacquesLucke) who
helped during this and pushed me to reduce the duplication further.

Reviewed By: brecht, sergey, JacquesLucke

Differential Revision: http://developer.blender.org/D13791
2022-01-12 12:19:39 +01:00

171 lines
6.4 KiB
C++

/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2017 by the Blender Foundation.
* All rights reserved.
*/
#pragma once
/** \file
* \ingroup bli
*/
#include <stdarg.h>
#include "BLI_compiler_attrs.h"
#include "BLI_utildefines.h"
#ifdef __cplusplus
extern "C" {
#endif
struct ListBase;
typedef bool (*UniquenameCheckCallback)(void *arg, const char *name);
/**
* Looks for a numeric suffix preceded by `delim` character on the end of
* name, puts preceding part into *left and value of suffix into *nr.
* Returns the length of *left.
*
* Foo.001 -> "Foo", 1
* Returning the length of "Foo"
*
* \param left: Where to return copy of part preceding `delim`.
* \param nr: Where to return value of numeric suffix`.
* \param name: String to split`.
* \param delim: Delimiter character`.
* \return Length of \a left.
*/
size_t BLI_split_name_num(char *left, int *nr, const char *name, char delim);
bool BLI_string_is_decimal(const char *string) ATTR_NONNULL();
/**
* Based on `BLI_split_dirfile()` / `os.path.splitext()`,
* `"a.b.c"` -> (`"a.b"`, `".c"`).
*/
void BLI_string_split_suffix(const char *string, char *r_body, char *r_suf, const size_t str_len);
/**
* `"a.b.c"` -> (`"a."`, `"b.c"`).
*/
void BLI_string_split_prefix(const char *string, char *r_pre, char *r_body, const size_t str_len);
/**
* Join strings, return newly allocated string.
*/
char *BLI_string_join_array(char *result,
size_t result_len,
const char *strings[],
uint strings_len) ATTR_NONNULL();
/**
* A version of #BLI_string_join that takes a separator which can be any character including '\0'.
*/
char *BLI_string_join_array_by_sep_char(char *result,
size_t result_len,
char sep,
const char *strings[],
uint strings_len) ATTR_NONNULL();
/**
* Join an array of strings into a newly allocated, null terminated string.
*/
char *BLI_string_join_arrayN(const char *strings[], uint strings_len) ATTR_WARN_UNUSED_RESULT
ATTR_NONNULL();
/**
* A version of #BLI_string_joinN that takes a separator which can be any character including '\0'.
*/
char *BLI_string_join_array_by_sep_charN(char sep,
const char *strings[],
uint strings_len) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
/**
* A version of #BLI_string_join_array_by_sep_charN that takes a table array.
* The new location of each string is written into this array.
*/
char *BLI_string_join_array_by_sep_char_with_tableN(char sep,
char *table[],
const char *strings[],
uint strings_len) ATTR_NONNULL();
/**
* Take multiple arguments, pass as (array, length).
*/
#define BLI_string_join(result, result_len, ...) \
BLI_string_join_array( \
result, result_len, ((const char *[]){__VA_ARGS__}), VA_NARGS_COUNT(__VA_ARGS__))
#define BLI_string_joinN(...) \
BLI_string_join_arrayN(((const char *[]){__VA_ARGS__}), VA_NARGS_COUNT(__VA_ARGS__))
#define BLI_string_join_by_sep_charN(sep, ...) \
BLI_string_join_array_by_sep_charN( \
sep, ((const char *[]){__VA_ARGS__}), VA_NARGS_COUNT(__VA_ARGS__))
#define BLI_string_join_by_sep_char_with_tableN(sep, table, ...) \
BLI_string_join_array_by_sep_char_with_tableN( \
sep, table, ((const char *[]){__VA_ARGS__}), VA_NARGS_COUNT(__VA_ARGS__))
/**
* Finds the best possible flipped (left/right) name.
* For renaming; check for unique names afterwards.
*
* \param r_name: flipped name,
* assumed to be a pointer to a string of at least \a name_len size.
* \param from_name: original name,
* assumed to be a pointer to a string of at least \a name_len size.
* \param strip_number: If set, remove number extensions.
* \return The number of bytes written into \a r_name.
*/
size_t BLI_string_flip_side_name(char *r_name,
const char *from_name,
bool strip_number,
const size_t name_len);
/**
* Ensures name is unique (according to criteria specified by caller in unique_check callback),
* incrementing its numeric suffix as necessary. Returns true if name had to be adjusted.
*
* \param unique_check: Return true if name is not unique
* \param arg: Additional arg to unique_check--meaning is up to caller
* \param defname: To initialize name if latter is empty
* \param delim: Delimits numeric suffix in name
* \param name: Name to be ensured unique
* \param name_len: Maximum length of name area
* \return true if there if the name was changed
*/
bool BLI_uniquename_cb(UniquenameCheckCallback unique_check,
void *arg,
const char *defname,
char delim,
char *name,
size_t name_len);
/**
* Ensures that the specified block has a unique name within the containing list,
* incrementing its numeric suffix as necessary. Returns true if name had to be adjusted.
*
* \param list: List containing the block
* \param vlink: The block to check the name for
* \param defname: To initialize block name if latter is empty
* \param delim: Delimits numeric suffix in name
* \param name_offset: Offset of name within block structure
* \param name_len: Maximum length of name area
*/
bool BLI_uniquename(struct ListBase *list,
void *vlink,
const char *defname,
char delim,
int name_offset,
size_t name_len);
#ifdef __cplusplus
}
#endif