将收到的可变数量的参数传递给另一个函数

时间:2015-06-29 20:13:08

标签: c function arguments ansi-c

在你说它重复之前,我已经读过这个:How to pass variable number of arguments from one function to another?

我有这样的功能:

:before

这样的另一个函数,不是我的,我在第三方库中使用它:

.item-selector-button {
  position: relative;
  text-align: center;
  cursor: pointer;
  border: 1px solid #cecece;
  padding: 15px;
  border-radius: 0;
  color: #000;
  width: 160px;
  height: 120px;
  transition: all ease-in-out 0.1s, box-shadow ease-in-out 0.1s;
}

.item-selector-button .title {
  color: #3e53a4;
  font-size: 12px;
  margin: 0;
  padding-top: -3px;
  font-family: "PrecisionSans_W_Md", "Helvetica Neue", Arial, sans-serif;
}

.item-selector-button .divider {
  height: 1px;
  width: 20px;
  background-color: #cecece;
  margin: 4px auto 10px;
}

.item-selector-button .image {
  background: #fff url("../images/box.png") center center no-repeat;
  width: 64px;
  height: 57px;
  margin: 4px auto;
}

.item-selector-button:hover, .item-selector-button.hover {
  padding: 14px;
}

.item-selector-button:hover:after, .item-selector-button.hover:after {
  content: "";
  position: absolute;
  bottom: -12px;
  left: 68px;
  border-width: 12px 12px 0;
  border-style: solid;
  border-color: #fff transparent;
  transition-delay: 0.3s;
}

.item-selector-button:hover:before, .item-selector-button.hover:before {
  content: "";
  position: absolute;
  bottom: -15px;
  left: 65px;
  border-width: 15px 15px 0;
  border-style: solid;
  border-color: #3e53a4 transparent;
  transition-delay: 0.3s;
}

.item-selector-button:active, .item-selector-button.active {
  border-width: 2px;
  border-color: #3e53a4;
  background-color: #3e53a4;
}

.item-selector-button:active:before, .item-selector-button.active:before {
  content: "";
  position: absolute;
  bottom: -15px;
  left: 65px;
  border-width: 15px 15px 0;
  border-style: solid;
  border-color: #3e53a4 transparent;
  transition-delay: 0.3s;
}

.item-selector-button:active .title, .item-selector-button.active .title {
  color: #fff;
}

.item-selector-button:active .divider, .item-selector-button.active .divider {
  background-color: #fff;
}

.item-selector-button:active .image, .item-selector-button.active .image {
  background-color: #3e53a4;
}

.item-selector-button:active:hover, .item-selector-button.active:hover {
  padding: 15px;
  box-shadow: none;
}

.item-selector-button:active:hover:after, .item-selector-button.active:hover:after {
  content: "";
  position: absolute;
  bottom: -12px;
  left: 68px;
  border-width: 12px 12px 0;
  border-style: solid;
  border-color: #3e53a4 transparent;
  transition-delay: 0.3s;
}

.item-selector-button.disabled {
  pointer-events: none;
  cursor: not-allowed;
}

.item-selector-button.disabled .title {
  color: #c3c3c3;
}

.item-selector-button.disabled .image {
  background-image: url("../images/box-disabled.png");
}

.item-selector-button.disabled:hover {
  padding: 15px;
  border: 1px solid #cecece;
  box-shadow: none;
}

正如您所看到的,我想要做的只是为此函数添加一个互斥锁以使其成为线程安全的,我知道我可以使用void tlog_function(t_log* logger, const char* message_template, ...) { pthread_mutex_lock(&loggerLock); log_function(logger, message_template, ...); // What I want to do.. pthread_mutex_unlock(&loggerLock); } 但在这种情况下我无法更改第二个代码函数,因为它在库中,我只有.h文件。

那么,有没有办法实现这个目标?

2 个答案:

答案 0 :(得分:6)

如果您无法更改代码并且没有Zip版本的库函数,您可以使用我所知道的最接近的事情是使用宏和##__VA_ARGS__which is a GCC only extension )。

你会想要这样的东西:

va_list

答案 1 :(得分:2)

你不能编写一个函数,将其变量参数列表转发给另一个使用标准C提供的工具的可变参数函数。的确,....InvalidQueryException: unconfigured columnfamily my_custom_table恰好存在,因为这是不可能的。 (正如esm的回答中所讨论的,可以做到这一点。)

如果可以选择其他第三方库,libffi可以说服你想做的事情。 (你问,libffi是如何工作的?Hand-written assembly language。)